Create a new Map/reduce project under Eclipse and add the following jar to the build path:
Program code:
Packagethathbase;Importjava.io.IOException;ImportJava.util.Random;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.hbase.HBaseConfiguration;ImportOrg.apache.hadoop.hbase.HColumnDescriptor;ImportOrg.apache.hadoop.hbase.HTableDescriptor;Importorg.apache.hadoop.hbase.client.Connection;Importorg.apache.hadoop.hbase.client.ConnectionFactory;Importorg.apache.hadoop.hbase.client.HBaseAdmin;Importorg.apache.hadoop.hbase.client.HTable;ImportOrg.apache.hadoop.hbase.client.Put; Public classHellohbase {StaticConfiguration conf; Statichbaseadmin Admin; StaticConnection Conn; StaticHtabledescriptor Tabledescriptor; Statichtable table; StaticPut PutRow1; StaticPut PutRow2; StaticRandom Rand =NewRandom (25); Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stubinit (); CreateTable (); Inserttable (); } Private Static voidInit ()throwsException {conf=hbaseconfiguration.create (); Conf.set ("Hbase.zookeeper.property.clientPort", "2181"); Conf.set ("Hbase.zookeeper.quorum", "Master,slave03,slave04"); Conf.set ("Hbase.master", "master:60000"); Conn=connectionfactory.createconnection (conf); Admin=(Hbaseadmin) conn.getadmin (); } @SuppressWarnings ("Deprecation") Private Static voidCreateTable ()throwsexception{if(!admin.tableexists ("scores") ) {Tabledescriptor=NewHtabledescriptor ("scores". GetBytes ()); Tabledescriptor.addfamily (NewHcolumndescriptor ("fam1")); Admin.createtable (Tabledescriptor); } Else{System.out.println ("Table already exists!"); } table=NewHtable (conf, "scores"); } @SuppressWarnings ("Deprecation") Private Static voidInserttable ()throwsioexception{PutRow1=NewPut ("Row1". GetBytes ()); PutRow2=NewPut ("Row2". GetBytes ()); for(inti = 0; i<1000; i++){ if(Rand1 () = = 1) {Putrow1.add ("Fam1". GetBytes (), "col1". GetBytes (), String.valueof (Rand2 ()). GetBytes ()); Table.put (PUTROW1); System.out.println (i+ ": Insert into col1."); } Else{Putrow2.add ("Fam1". GetBytes (), "col2". GetBytes (), String.valueof (Rand2 ()). GetBytes ()); Table.put (PUTROW2); System.out.println (i+ ": Insert into Col2."); } } } Private Static intRand1 () {intr = Rand.nextint (100); if(R < 50) return1; Else return2; } Private Static intRand2 () {intr = Rand.nextint (10000); returnR; }}
The above program calls HBase API, implements a new table, and randomly inserts data into the table.
Eclipse Remote Connection HBase