Official Document: Http://hbase.apache.org/book.html
Table for Java simple Operation HBase
1 Importorg.apache.hadoop.conf.Configuration;2 ImportOrg.apache.hadoop.hbase.*;3 Importorg.apache.hadoop.hbase.client.*;4 ImportOrg.apache.hadoop.hbase.filter.CompareFilter;5 Importorg.apache.hadoop.hbase.filter.FilterList;6 ImportOrg.apache.hadoop.hbase.filter.SingleColumnValueFilter;7 ImportOrg.junit.Before;8 Importorg.junit.Test;9 Ten Importjava.io.IOException; One Importjava.util.ArrayList; A ImportJava.util.Iterator; - ImportJava.util.Random; - the /** - * Created by Edward on 2016/6/19. - */ - Public classTesthbase { + - Public StaticConfiguration conf =NULL; + Public StaticTableName table = tablename.valueof ("Phone"); A Public StaticRandom random =NewRandom (); at - @Before - Public voidSetup () - { - //access HBase through the zookeeper cluster -conf =hbaseconfiguration.create (); inConf.set ("Hbase.zookeeper.quorum", "Node1,node2,node3"); -SYSTEM.OUT.PRINTLN ("Setup"); to } + - @Test the Public voidCreateTable ()throwsIOException { *Hbaseadmin hbaseadmin =Newhbaseadmin (conf); $ if(hbaseadmin.tableexists (table)) {Panax Notoginseng hbaseadmin.disabletable (table); - hbaseadmin.deletetable (table); the } + //Table AHtabledescriptor Htabledescriptor =Newhtabledescriptor (table); the //Column Family Cf1 +Hcolumndescriptor Hcolumndescriptor =NewHcolumndescriptor ("CF1"); -Hcolumndescriptor.setblockcacheenabled (true); $Hcolumndescriptor.setblocksize (128000); $Hcolumndescriptor.setmaxversions (10); - //table Add column family - htabledescriptor.addfamily (hcolumndescriptor); the hbaseadmin.createtable (htabledescriptor); - }Wuyi the - @Test Wu /** - * Insert Data About */ $ Public voidInsert ()throwsIOException { - - //Create a Htable object -Htable htable =NewHtable (Conf, This. table); Aarraylist<put> list =NewArraylist<put>(); + the for(inti = 0; i<1000; i++) - { $ //Row_key thePut put =NewPut (string.valueof (i). GetBytes ()); the //Column,value thePut.add ("Cf1". GetBytes (), "name". GetBytes (), "LS". GetBytes ()); thePut.add ("Cf1". GetBytes (), "Age". GetBytes (), string.valueof (i%100). GetBytes ()); - //Put each Put object in the list in List.add (put); the } the //Put the information in the table About htable.put (list); the } the the + @Test - /** the * EnquiryBayi */ the Public voidSearch ()throwsIOException the { - -Htable htable =NewHtable (Conf, This. table); the //Create a Row_key corresponding Get object theGet get =NewGet ("123". GetBytes ()); the //Get Get Results theResult result =Htable.get (get); - //get column Latest cell theCell Columnlatestcell = Result.getcolumnlatestcell ("Cf1". GetBytes (), "name". GetBytes ()); the //get a value using Cellutil the byte[] bytes =Cellutil.clonevalue (Columnlatestcell);94System.out.println (NewString (bytes)); the } the the 98 @Test About /** - * Data acquisition via scan method101 * **/102 Public voidSearch1 ()throwsIOException103 {104Htable htable =NewHtable (Conf, This. table); the //Set Scan Range106Scan Scan =NewScan ("GetBytes"), "450". GetBytes ());107 //get result scanner by scan108Resultscanner scanner =Htable.getscanner (scan);109 //using iterators theIterator<result> Iterator =scanner.iterator ();111 the while(Iterator.hasnext ())113 { theResult result=Iterator.next (); theCell Columnlatestcell = Result.getcolumnlatestcell ("Cf1". GetBytes (), "age". GetBytes ()); the //get the values for columns in a column family117 byte[] bytes =Cellutil.clonevalue (Columnlatestcell);118 //Get Row_key119 byte[] Bytes1 =Cellutil.clonerow (Columnlatestcell); -System.out.println (NewString (bytes) + "" +NewString (bytes1));121 }122 }123}
Simple Java Operations for HBase