Environment Introduction
64-bit Ubuntu14.10,hadoop 2.5.0, HBase 0.99.0
Prepare the Environment
1 Installing Hadoop 2.5.0, refer to Http://www.cnblogs.com/liuchangchun/p/4097286.html
2 Installing HBase 0.99.0, refer to Http://www.cnblogs.com/liuchangchun/p/4096891.html
3 Installing Ecliose
New Java Project
1 run Eclipse, create a new Java project "Myhbase", right-click the project root directory, select "Properties", "Java Build Path", "Library", "Add External JARs" ", add hbase to the root directory under the Lib subdirectory under all the jar packages are added to the classpath of this project.
2. Follow the steps in step 1 to add the configuration file Hbase-site.xml of the hbase you are connected to in the classpath of this project, as shown below as an example of a configuration file:
Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.hbase.HBaseConfiguration;ImportOrg.apache.hadoop.hbase.HColumnDescriptor;ImportOrg.apache.hadoop.hbase.HTableDescriptor;ImportOrg.apache.hadoop.hbase.KeyValue;Importorg.apache.hadoop.hbase.MasterNotRunningException;Importorg.apache.hadoop.hbase.ZooKeeperConnectionException;ImportOrg.apache.hadoop.hbase.client.Delete;ImportOrg.apache.hadoop.hbase.client.Get;Importorg.apache.hadoop.hbase.client.HBaseAdmin;Importorg.apache.hadoop.hbase.client.HTable;ImportOrg.apache.hadoop.hbase.client.Put;ImportOrg.apache.hadoop.hbase.client.Result;ImportOrg.apache.hadoop.hbase.client.ResultScanner;ImportOrg.apache.hadoop.hbase.client.Scan;Importorg.apache.hadoop.hbase.util.Bytes; Public classHbasetest {Private StaticConfiguration conf =NULL; /*** Initialize Configuration*/ Static{conf=hbaseconfiguration.create (); } /*** Create a table*/ Public Static voidCreattable (String tableName, string[] familys)throwsException {hbaseadmin admin=Newhbaseadmin (conf); if(Admin.tableexists (tableName)) {System.out.println ("Table already exists!"); } Else{htabledescriptor Tabledesc=NewHtabledescriptor (tableName); for(inti=0; i<familys.length; i++) {tabledesc.addfamily (NewHcolumndescriptor (Familys[i])); } admin.createtable (TABLEDESC); System.out.println ("CREATE TABLE" + TableName + "OK."); } } /*** Delete Table*/ Public Static voidDeletetable (String tableName)throwsException {Try{hbaseadmin Admin=Newhbaseadmin (conf); Admin.disabletable (TableName); Admin.deletetable (TableName); System.out.println ("Delete Table" + TableName + "OK."); } Catch(masternotrunningexception e) {e.printstacktrace (); } Catch(zookeeperconnectionexception e) {e.printstacktrace (); } } /*** Insert a row of records*/ Public Static voidAddRecord (String tableName, String RowKey, String family, string qualifier, String value)throwsexception{Try{htable table=Newhtable (conf, tableName); Put put=NewPut (Bytes.tobytes (RowKey)); Put.add (Bytes.tobytes (family), bytes.tobytes (qualifier), bytes.tobytes (value)); Table.put (Put); System.out.println ("Insert Recored" + RowKey + "to table" + TableName + "OK."); } Catch(IOException e) {e.printstacktrace (); } } /*** Delete a row of records*/ Public Static voidDelrecord (String tableName, String RowKey)throwsioexception{htable Table=Newhtable (conf, tableName); List List=NewArrayList (); Delete del=NewDelete (Rowkey.getbytes ()); List.add (DEL); Table.delete (list); System.out.println ("Del recored" + RowKey + "OK."); } /*** Find a row of records*/ Public Static voidGetonerecord (String tableName, String RowKey)throwsioexception{htable Table=Newhtable (conf, tableName); Get Get=NewGet (Rowkey.getbytes ()); Result RS=Table.get (GET); for(KeyValue Kv:rs.raw ()) {System.out.print (NewString (Kv.getrow ()) + "" ); System.out.print (NewString (kv.getfamily ()) + ":" ); System.out.print (NewString (Kv.getqualifier ()) + "" ); System.out.print (Kv.gettimestamp ()+ " " ); System.out.println (NewString (Kv.getvalue ())); } } /*** Show All data*/ Public Static voidGetallrecord (String tableName) {Try{htable table=Newhtable (conf, tableName); Scan s=NewScan (); Resultscanner SS=Table.getscanner (s); for(Result r:ss) { for(KeyValue Kv:r.raw ()) {System.out.print (NewString (Kv.getrow ()) + ""); System.out.print (NewString (kv.getfamily ()) + ":"); System.out.print (NewString (Kv.getqualifier ()) + ""); System.out.print (Kv.gettimestamp ()+ " "); System.out.println (NewString (Kv.getvalue ())); } } } Catch(IOException e) {e.printstacktrace (); } } Public Static voidmain (String [] agrs) {Try{String tablename= "Scores"; String[] Familys= {"Grade", "course"}; Hbasetest.creattable (tablename, familys); //Add record zkbHbasetest.addrecord (tablename, "ZKB", "Grade", "", "5"); Hbasetest.addrecord (tablename,"ZKB", "Course", "" "," 90 "); Hbasetest.addrecord (tablename,"ZKB", "Course", "math", "97"); Hbasetest.addrecord (tablename,"ZKB", "course", "Art", "87"); //Add record BaoniuHbasetest.addrecord (tablename, "Baoniu", "Grade", "", "4"); Hbasetest.addrecord (tablename,"Baoniu", "Course", "math", "89"); System.out.println ("===========get One record========"); Hbasetest.getonerecord (tablename,"ZKB"); System.out.println ("===========show All record========"); Hbasetest.getallrecord (tablename); System.out.println ("===========del One record========"); Hbasetest.delrecord (tablename,"Baoniu"); Hbasetest.getallrecord (tablename); System.out.println ("===========show All record========"); Hbasetest.getallrecord (tablename); } Catch(Exception e) {e.printstacktrace (); } } }
3 before running to start Hadoop and hbase, no problem, will print out the following
Log4j:warn No appenders could be found forLogger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory). Log4j:warn Please initialize the log4j system Properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.CREATE TABLE scores ok.insert recored ZKB to table scores Ok.insert recored ZKB to table scores Ok.insert recored ZKB To table scores Ok.insert recored ZKB to table scores Ok.insert recored baoniu to table scores Ok.insert recored Baoniu to Table scores ok.===========get One record========ZKB Course:1416917870482 90ZKB Course:art1416917872071 87ZKB Course:math1416917871719 97ZKB Grade:1416917869799 5===========show All record========Baoniu Course:math1416917874500 89Baoniu Grade:1416917874139 4ZKB Course:1416917870482 90ZKB Course:art1416917872071 87ZKB Course:math1416917871719 97ZKB Grade:1416917869799 5===========del One record========del recored baoniu ok.zkb course:1416917870482 90ZKB Course:art1416917872071 87ZKB Course:math1416917871719 97ZKB Grade:1416917869799 5===========show All record========ZKB Course:1416917870482 90ZKB Course:art1416917872071 87ZKB Course:math1416917871719 97ZKB Grade:1416917869799 5
Ubuntu 14.10 under Eclipse operations HBase