eclipse,hbase version is not currently found to require special designation
1: Copy a hbase deployment file from the HBase cluster and place it in a directory on the development side (for example, under the/app/hadoop/hbase096 directory). 2: Create a new Java project hbase in Eclipse, then select the project Properties, Libraries->add External JARs ..., and then select the associated jar package under/app/hadoop/hbase096/lib. If it's just a test, it's simpler to select all the jars.
3: Add a Folder conf under the project HBase, copy the HBase cluster profile hbase-site.xml to the directory, and then select the item attributes in Libraries->add Class folder, which will be selected for the newly added conf directory.
4: Add a Chapter12 package to the HBase project, then add a Hbasetestcase class, and then copy the code from the Lu Jiaheng Teacher's 2nd edition of Hadoop, and make the appropriate changes, code as follows:
Package chapter12;
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.client.Put;
Import Org.apache.hadoop.hbase.client.Result;
Import Org.apache.hadoop.hbase.client.ResultScanner;
Import Org.apache.hadoop.hbase.client.Scan;
Import org.apache.hadoop.hbase.util.Bytes; public class Hbasetestcase {//hbaseconfiguration static configuration Configuration cfg=hbaseconfiguration.cr
Eate (); Create a table with Hbaseadmin htabledescriptor to create public static void Creat (String tablename,string columnfamily) throws Excepti
on {hbaseadmin admin = new hbaseadmin (CFG); if (admin.tableexists (tablename)) {System.out.println ("Table exists!");
System.exit (0);
} else{Htabledescriptor Tabledesc = new Htabledescriptor (tablename);
Tabledesc.addfamily (New Hcolumndescriptor (columnfamily));
Admin.createtable (TABLEDESC);
SYSTEM.OUT.PRINTLN ("CREATE Table success!"); }//Add a piece of data, add data public static void put (string tablename,string row, string Columnfami by Htable put to an existing table)
Ly,string column,string data) throws Exception {htable table = new htable (CFG, tablename);
Put P1=new put (bytes.tobytes (row));
P1.add (Bytes.tobytes (columnfamily), bytes.tobytes (column), bytes.tobytes (data);
Table.put (p1);
System.out.println ("Put" "+row+" ', ' "+columnfamily+": "+column+", "" +data+ ""); public static void Get (String tablename,string row) throws ioexception{htable table=new htable (cfg,t
Ablename);
Get G=new Get (bytes.tobytes (row)); Result Result=tAble.get (g);
System.out.println ("Get:" +result); ///Display all data, htable scan to get information about existing tables public static void scan (String tablename) throws exception{htable tabl
E = new htable (CFG, tablename);
Scan s = new Scan ();
Resultscanner rs = Table.getscanner (s);
for (result r:rs) {System.out.println ("Scan:" +r); } public static Boolean delete (String tablename) throws ioexception{Hbaseadmin ad
Min=new hbaseadmin (CFG); if (admin.tableexists (tablename)) {try {Admin.disablet
Able (tablename);
Admin.deletetable (tablename);
}catch (Exception ex) {ex.printstacktrace ();
return false;
} return true; public static void Main (String [] AGRs {String tablename= "HBASE_TB";
String columnfamily= "CF";
try {hbasetestcase.creat (tablename, columnfamily);
Hbasetestcase.put (tablename, "Row1", columnfamily, "CL1", "data");
Hbasetestcase.get (tablename, "Row1");
Hbasetestcase.scan (tablename); /* IF (True==hbasetestcase.delete (tablename)) System.out.println ("Delete table: +tablename+" s
Uccess! ");
*/} catch (Exception e) {e.printstacktrace (); }
}
}
5: Set the Run configuration, and then run. Start the HBase cluster before running.
6: Check, use hbase Shell to view hbase, found that table HBASE_TB has been established.
During the period I encountered some problems:
1. The introduction of JAR package, two packages do not have read permissions, resulting in the project appears red exclamation point
2. Because I'm a virtual machine, I have to restart Hadoop and hbase every time, so don't forget to set Hadoop as a non safe mode.