Package Testhbase;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 TestHBase {static Configuration cfg = hbaseconfiguration.create (); Get config object public static void Create (String tableName, String columnfamily) throws exception{hbaseadmin admin = new HBASEADMI N (CFG); Get Hbaseadmin object for Operation Hbaseif (Admin.tableexists (tableName)) {System.out.println ("Table" + tableName + "exists!"); System.exit (0);} Else{htabledescriptor Tabledesc = new Htabledescriptor (tableName);//Get Table Description object used when creating table TabledeSc.addfamily (New Hcolumndescriptor (columnfamily)); Hcolumndescriptor Column Family Description Object admin.createtable (TABLEDESC); CREATE TABLE System.out.println ("table" + TableName + "success!");}} static void put (String tablename,string row,string columnfamily,string column,string data) throws Exception{htable table = new Htable (cfg,tablename); Get the Table object put put = new put (bytes.tobytes (row));//Insert Data Put.add (Bytes.tobytes (columnfamily), bytes.tobytes (column), Bytes.tobytes (data)); Table.put (put); System.out.println ("put" + row + "," + "columnfamily:" + column + "," + data "); static void Get (String tablename,string row) throws exception{htable table = new htable (cfg,tablename); Get get = new Get (bytes.tobytes (row));//Gets the row data result = Table.get (get); System.out.println ("Get:" + result); static void Scan (String tableName) throws exception{htable table = new htable (cfg,tablename); Scan scan = new Scan (bytes.tobytes (tableName)); Scan Full table Resultscanner rs = table.getscanner (scan), for (Result r:rs) {System.out.println ("Scan:"+ R);}} Static Boolean Delete (String tableName) throws exception{hbaseadmin admin = new Hbaseadmin (CFG), if (Admin.tableexists ( TableName)) {admin.disabletable (tableName); First DisableSystem.out.println ("Disable Table" + tableName + "success!"); Admin.deletetable (TableName); Delete System.out.println ("delete Table" + tableName + "success!");} return true;} public static void Main (string[] args) throws Exception {Cfg.set ("Hbase.master", "hadoop:60000"); Cfg.set (" Hbase.rootdir "," hdfs://hadoop:9000/hbase "); Cfg.set (" Hbase.zookeeper.quorum "," Hadoop "); String tableName = "Hbase_test"; String columnfamily = "CF"; Create (TableName, columnfamily);p ut (tableName, "Row1", columnfamily, "C1", "Data"), scan ( TableName); get (TableName, "Row1");d elete (TableName);}}
HBase Javaapi Operation Example