Reference: http://www.cnblogs.com/NicholasLee/archive/2012/09/14/2684815.html
I. Put operations
Package hbase; import Java. io. ioexception; import Org. apache. hadoop. conf. configuration; import Org. apache. hadoop. hbase. hbaseconfiguration; import Org. apache. hadoop. hbase. hcolumndescriptor; import Org. apache. hadoop. hbase. htabledescriptor; import Org. apache. hadoop. hbase. client. get; import Org. apache. hadoop. hbase. client. hbaseadmin; import Org. apache. hadoop. hbase. client. htable; import Org. apache. hadoop. hbase. cl Ient. htablepool; import Org. apache. hadoop. hbase. client. put; import Org. apache. hadoop. hbase. client. result; import Org. apache. hadoop. hbase. util. bytes; public class hbaseclient {// declare static configuration conf = NULL; static final htablepool tablepool; static {conf = hbaseconfiguration. create (); Conf. set ("hbase. zookeeper. quorum "," libin2 "); tablepool = new htablepool (Conf, 10);}/** create table ** @ tabl Ename table name ** @ Family column family list */public static void creattable (string tablename, string [] Family) throws exception {hbaseadmin admin = new hbaseadmin (CONF ); htabledescriptor DESC = new htabledescriptor (tablename); For (INT I = 0; I <family. length; I ++) {DESC. addfamily (New hcolumndescriptor (family [I]);} If (Admin. tableexists (tablename) {system. out. println ("table exists! "); System. Exit (0);} else {Admin. createtable (DESC); system. Out. println (" create table success! ") ;}} Public static void putdata (string tablename) throws ioexception {htable table = (htable) tablepool. gettable (tablename); // obtain the table system. out. println ("auto flush:" + table. isautoflush (); table. setautoflush (false); Put put1 = new put (bytes. tobytes ("rowkey1"); put1.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), bytes. tobytes ("val1"); table. put (put1); Put put2 = new put (bytes. tobytes ("rowkey2"); put2.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), bytes. tobytes ("val1"); table. put (put2); get = new get (bytes. tobytes ("rowkey1"); Result RES1 = table. get (get); system. out. println ("Result:" + RES1); Put put3 = new put (bytes. tobytes ("rowkey2"); put3.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual2"), bytes. tobytes ("val2"); table. put (put3); table. flushcommits (); Result RES2 = table. get (get); system. out. println ("Result:" + RES2);} public static void main (string [] ARGs) throws exception {// string tablename = "libinhtable "; string [] Family = {"colfam1"}; // creattable (tablename, family); putdata ("libinhtable ");}}
Auto flush: True
Result: keyvalues = none
Result: keyvalues = {rowkey1/colfam1: qual1/1354092615995/Put/vlen = 4}
You can call htable. the setautoflush (false) method can disable automatic flush of the htable write client, so that data can be written to hbase in batches, instead of executing an update with a put statement, write requests are actually sent to the hbase server only when put fills up the client's write cache. Auto flush is enabled by default.
RES1 is none because the write buffer on the client is still in the memory and is not sent to the server.
Batch put Operation
Public static void putlistdata (string tablename) throws ioexception {htable table = (htable) tablepool. gettable (tablename); // obtain the table list <put> puts = new arraylist <put> (); Put put1 = new put (bytes. tobytes ("rowkey1"); put1.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), bytes. tobytes ("val1"); puts. add (put1); Put put2 = new put (bytes. tobytes ("rowkey2"); put2.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), bytes. tobytes ("val1"); puts. add (put2); table. put (puts );}
If an error occurs on the server side, these put processes will be stored in the local write buffer. You can use the getwritebuffer () method to access these put processes. The timestamps of batch inserted data are the same.
With Batch put, we cannot control the order of put on the server side.
Batch put error handling:
Public static void putlistdata (string tablename) throws ioexception {htable table = (htable) tablepool. gettable (tablename); // obtain the table list <put> puts = new arraylist <put> (); Put put1 = new put (bytes. tobytes ("rowkey1"); put1.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), bytes. tobytes ("val1"); puts. add (put1); Put put2 = new put (bytes. tobytes ("rowkey2"); // put2.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), // bytes. tobytes ("val1"); puts. add (put2); Put put3 = new put (bytes. tobytes ("rowkey3"); put3.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), bytes. tobytes ("val1"); puts. add (put3); try {table. put (puts);} catch (exception e) {system. out. println ("error:" + E); table. flushcommits ();}}
Table. Put (puts); puts put in table first. If an error occurs, flush is automatically submitted. For example, if put2 is null, the data before put2 can be put to the table:
Error: Java. Lang. illegalargumentexception: No columns to insert
Checkandput:
Public static void putandcheck (string tablename) throws ioexception {htable table = (htable) tablepool. gettable (tablename); // get the table put put1 = new put (bytes. tobytes ("rowkey1"); put1.add (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), bytes. tobytes ("val1"); Boolean RES1 = table. checkandput (bytes. tobytes ("rowkey1"), bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), null, put1); system. out. println ("put applied:" + RES1); Boolean RES2 = table. checkandput (bytes. tobytes ("rowkey1"), bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), null, put1); system. out. println ("put applied:" + RES2); Boolean RES3 = table. checkandput (bytes. tobytes ("rowkey2"), bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"), null, put1); system. out. println ("put applied:" + RES3 );}
Put applied: True
Put applied: false
Exception in thread "Main" org. Apache. hadoop. hbase. donotretryioexception: org. Apache. hadoop. hbase. donotretryioexception: Action's getrow must match the passed row
During the first put operation, put1 does not exist and can be inserted. The second put operation fails because put1 already exists. The third error is reported because check and put are for the same row, that is, the same rowkey.
2. Get operation
Public static void getdata (string tablename, string rowkey) throws ioexception {htable table = (htable) tablepool. gettable (tablename); // get table get = new get (bytes. tobytes (rowkey); get. addColumn (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"); Result result = table. get (get); byte [] val = result. getvalue (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1"); system. out. println ("value:" + bytes. tostring (VAL); system. out. println (bytes. tostring (result. getrow (); system. out. println (bytes. tostring (result. value (); system. out. println (result. size (); For (keyValue KV: result. list () {system. out. println ("Family:" + bytes. tostring (KV. getfamily (); system. out. println ("qualifier:" + bytes. tostring (KV. getqualifier (); system. out. println ("value:" + bytes. tostring (KV. getvalue (); system. out. println ("timestamp:" + kv. gettimestamp ();} system. out. println (result. getcolumn (bytes. tobytes ("colfam1"), bytes. tobytes ("qual1 ")));}
Value: val1
Rowkey1
Val1
1
Family: colfam1
Qualifier: qual1
Value: val1
Timestamp: 1354167940755
[Rowkey1/colfam1: qual1/1354167940755/Put/vlen = 4]
Byte [] values return the first row in alphabetical order;
Size () returns the number of keyvalues. You can traverse the keyValue to obtain detailed information about the result;
Getcolumn returns the information of the completed column. If any get error (such as a column that does not exist in get) is reported during batch get operations, the entire execution terminal will not return any results.