Insert Data into HTable
- Create Put instance with Row Key "rowkey1"
Put put = new Put(Bytes.toBytes("rowkey1"));
- Insert data with Colume Family with "sig" & "botnet", then the value as "sobic"
put.add(Bytes.toBytes("sig"), Bytes.toBytes("botnet"), Bytes.toBytes("sobic"));
- insert data into htable
htable.put(put);
Source code
package org.hadoop.dna;
import java.io.IOException;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class SnortInsert {
/**
* hbase table write example
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try {
Configuration config = HBaseConfiguration.create();
config.addResource(new Path("/usr/local/hbase/conf/hbase-default.xml"));
config.addResource(new Path("/usr/local/hbase/conf/hbase-site.xml"));
// Load Hbase table
HTable htable = new HTable(config, "SnortTable");
// Put
// rowkey1 : sig:botnet -> sobic
Put put = new Put(Bytes.toBytes("rowkey1"));
put.add(Bytes.toBytes("sig"), Bytes.toBytes("botnet"), Bytes.toBytes("sobic"));
htable.put(put);
// Put
// rowkey1 : priority:1 -> 1
Put put2 = new Put(Bytes.toBytes("rowkey1"));
put2.add(Bytes.toBytes("priority"), Bytes.toBytes("1"), Bytes.toBytes("1"));
htable.put(put2);
} catch (IOException e) {
System.out.println("IOExeption: cannot insert table");
e.printStackTrace();
}
}
}