configuration steps for developing HBase programs using Eclipse
1. Create a new generic Java project.
2. --java build path--libraries--add external jars, add hadoop installation directory hbase-0.90.5.jar hbase-0.90.5-tests.jar, hbase installation directory lib All in the directory jar file.
3. Create a new conf directory under the project root directory and copy the conf directory under the hbase installation directory . Hbase-site.xml to the folder.
4. in the project's properties --java build Path--libraries--add class folder, the newly created conf The directory is added in . The development environment is now complete.
5. Create a new Java class and add the following code:
Package com.hadoop.hbase.test;
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 Hbasetest {
public static void Main (string[] args) throws Exception {
Configuration config = hbaseconfiguration.create ();
CREATE table
Hbaseadmin admin = new hbaseadmin (config);
Htabledescriptor HTD = new Htabledescriptor ("Student");
Hcolumndescriptor HCD = new Hcolumndescriptor ("address");
Hcolumndescriptor hcd2 = new Hcolumndescriptor ("info");
Htd.addfamily (HCD);
Htd.addfamily (HCD2);
Admin.createtable (HTD);
byte[] TableName = Htd.getname ();
htabledescriptor[] tables = Admin.listtables ();
if (tables.length! = 1 && bytes.equals (TableName, Tables[0].getname ())) {
throw new Exception ("Failed CREATE TABLE!!!");
}
Run some operations--a put, a get, a scan--against the table
htable table = new htable (config, tableName);
byte[] Row1 = bytes.tobytes ("John");
Put p1 = new put (ROW1);
byte[] Databytes = bytes.tobytes ("Address");
P1.add (Databytes, Bytes.tobytes ("City"), Bytes.tobytes ("WuHan"));
P1.add (Databytes, Bytes.tobytes ("province"), Bytes.tobytes ("Hubei"));
Table.put (p1);
Get g = new Get (ROW1);
Result result = Table.get (g);
SYSTEM.OUT.PRINTLN ("Result:" + result);
Scan scan = new scan ();
Resultscanner scanner = Table.getscanner (scan);
for (Result Scannerresult:scanner) {
System.out.println ("Scan:" + scannerresult);
}
Drop the table
Admin.disabletable (TableName);
Admin.deletetable (TableName);
SYSTEM.OUT.PRINTLN ("----done----");
}
}
6. Enter the hbase shelland use the list command to see the newly created table, using scan ' Student ' You can see the inserted data.
At this point, the first hbase program development is completed.
Hadoop-06-using eclipse to develop hbase programs