Recently, after reviewing the relevant information, we combed the table creation, data insert, and delete table of HBase using Java, the code is as follows:
1. Required JAR Package:
Commons-codec-1.4.jar
Commons-logging-1.0.4.jar
Hadoop-0.20.2-core.jar
Hbase-0.20.6.jar
Log4j-1.2.15.jar
Zookeeper-3.2.2.jar
2, code.
1 Packageorg.myhbase;2 3 Importjava.io.IOException;4 5 Importorg.apache.hadoop.conf.Configuration;6 Importorg.apache.hadoop.hbase.HBaseConfiguration;7 ImportOrg.apache.hadoop.hbase.HColumnDescriptor;8 ImportOrg.apache.hadoop.hbase.HTableDescriptor;9 ImportOrg.apache.hadoop.hbase.KeyValue;Ten Importorg.apache.hadoop.hbase.client.HBaseAdmin; One Importorg.apache.hadoop.hbase.client.HTable; A ImportOrg.apache.hadoop.hbase.client.Result; - ImportOrg.apache.hadoop.hbase.client.ResultScanner; - ImportOrg.apache.hadoop.hbase.client.Scan; the Importorg.apache.hadoop.hbase.io.BatchUpdate; - - Public classHBASEBASIC02 { - StaticHbaseconfiguration hbaseconfig=NULL; + Static{ -Configuration config=NewConfiguration (); +Config.set ("Hbase.zookeeper.quorum", "192.168.10.149,192.168.10.44,192.168.10.49"); AConfig.set ("Hbase.zookeeper.property.clientPort", "2181"); athbaseconfig=Newhbaseconfiguration (config); - } - - /** - * Create a table - * @throwsIOException in */ - Public Static voidCreateTable (String tablename)throwsioexception{ toHbaseadmin admin =Newhbaseadmin (hbaseconfig); + if(Admin.tableexists (tablename)) { -System.out.println ("Table Exists!!!"); the}Else{ *Htabledescriptor Tabledesc =NewHtabledescriptor (tablename); $Tabledesc.addfamily (NewHcolumndescriptor ("Name:"));Panax Notoginseng admin.createtable (TABLEDESC); -SYSTEM.OUT.PRINTLN ("CREATE table OK.")); the } + } A the /** + * Delete a table - * @throwsIOException $ */ $ Public Static voidDroptable (String tablename)throwsioexception{ -Hbaseadmin admin =Newhbaseadmin (hbaseconfig); - admin.disabletable (tablename); the admin.deletetable (tablename); -SYSTEM.OUT.PRINTLN ("drop table OK."));Wuyi } the - Wu /** - * Add a piece of data About * @throwsIOException $ */ - Public Static voidAddData (String tablename)throwsioexception{ -Htable table=Newhtable (hbaseconfig,tablename); -BatchUpdate update=NewBatchUpdate ("Huangyi"); AUpdate.put ("Name:java", "http://www.sun.com". GetBytes ()); + table.commit (update); theSYSTEM.OUT.PRINTLN ("Add data ok.")); - } $ the the /** the * Show All data the * @throwsIOException - */ in Public Static voidGetalldata (String tablename)throwsioexception{ theHtable table=Newhtable (hbaseconfig,tablename); theScan s=NewScan (); AboutResultscanner rs=Table.getscanner (s); the for(Result r:rs) { the for(KeyValue Kv:r.raw ()) { theSystem.out.println ("Rowkey:" +NewString (Kv.getrow ())); +System.out.println (NewString (Kv.getcolumn ()) + "=" +NewString (Kv.getvalue ())); - } the }Bayi } the the Public Static voidMain (String [] args)throwsioexception{ -String tablename= "Table_1"; - hbasebasic02.createtable (tablename); the Hbasebasic02.adddata (tablename); the Hbasebasic02.getalldata (tablename); the hbasebasic02.droptable (tablename); the } - the the}Click to view
(GO) Java Implementation HBase table creation, data insertion, delete table