Previous cm5.4 and cdh5.4 installation (http://my.oschina.net/penngo/blog/517223), this document uses HBase's Java Client API to manipulate HBase.
The package that needs to be used can be found in/opt/cloudera/parcels/cdh-5.4.7-1.cdh5.4.7.p0.3/jars, hbase version 1.0.0
Hbasetest3.java code Example
import java.io.ioexception;import org.apache.hadoop.conf.configuration;import org.apache.hadoop.hbase.cell;import org.apache.hadoop.hbase.cellutil;import org.apache.hadoop.hbase.hbaseconfiguration;import org.apache.hadoop.hbase.hcolumndescriptor;import org.apache.hadoop.hbase.htabledescriptor;import org.apache.hadoop.hbase.keyvalue;import org.apache.hadoop.hbase.masternotrunningexception;import org.apache.hadoop.hbase.tablename;import org.apache.hadoop.hbase.zookeeperconnectionexception;import org.apache.hadoop.hbase.client.connection; import org.apache.hadoop.hbase.client.connectionfactory;import org.apache.hadoop.hbase.client.get; Import org.apache.hadoop.hbase.client.hbaseadmin;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. Client. table;import org.apache.hadoop.hbase.filter.binaryprefixcomparator;import org.apache.hadoop.hbase.filter.filter;import org.apache.hadoop.hbase.filter.rowfilter;import org.apache.hadoop.hbase.filter.singlecolumnvaluefilter;import org.apache.hadoop.hbase.filter.comparefilter.compareop;import org.apache.hadoop.hbase.filter.substringcomparator;import org.apache.hadoop.hbase.filter.valuefilter; Import org.apache.hadoop.hbase.util.bytes;public class hbasetest3 {private connection connection = null;public hbasetest3 () {}public void createtable (String tablename) {try {system.out.println ("START CREATE TABLE  ..."); Connection con = this.getconnection (); hbaseadmin hbaseadmin = (Hbaseadmin) con.getadmin ();if (HBaseAdmin.tableExists ( TableName) {// If there is a table to create, delete it first, then create hbaseadmin.disabletable (tableName); Hbaseadmin.deletEtable (TableName); System.out.println (tablename + " is exist,detele ..."); Htabledescriptor tabledescriptor = new htabledescriptor (TableName.valueOf (TableName)); Tabledescriptor.addfamily (New hcolumndescriptor ("Column1")); Tabledescriptor.addfamily (new Hcolumndescriptor ("Column2")); Tabledescriptor.addfamily (New hcolumndescriptor ("Column3")); Hbaseadmin.createtable (Tabledescriptor); System.out.println ("create table success ..."); catch (masternotrunningexception e) {e.printstacktrace ();} catch (zookeeperconnectionexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();}} Public void insertdata (String tablename) {system.out.println ("Start insert data &NBSP); try {connection con = this.getconnection (); Table table = con.gettable (tablename.valueof (TableName));// htablepool&Nbsp;pool = new htablepool (configuration, 1000);// htable table = ( htable) pool.gettable (tableName); Put put = new put ("112233BBBCCCC". GetBytes ());// A put represents a row of data, and a new put represents the second row of data, Each line is a unique Rowkey, where Rowkey is the value passed in the Put construction method Put.addcolumn ("Column1". GetBytes (), null, "Aaa2". GetBytes ());// The first column of the bank's data Put.addcolumn ("Column2". GetBytes (), null, "Bbb2". GetBytes ());// The third column of the bank's data Put.addcolumn ("Column3". GetBytes (), null, "Ccc2". GetBytes ();// The third column of our data table.put (put) ;// pool.gettable (TableName). put (put);} catch (ioexception e) {e.printstacktrace ();} System.out.println ("END INSERT DATA  ..."); Public void queryall (String tablename) {// HTablePool pool = new Htablepool (configuration, 1000);// htable table = (htable) pool.getTable ( TableName); try {connection con = this.gEtconnection (); Table table = con.gettable (tablename.valueof (TableName)); Resultscanner rs = table.getscanner (New scan ());for (RESULT R : RS) {system.out.println ("Get to Rowkey:" + new string (R.getrow ()));for (keyvalue Keyvalue : r.raw ()) {system.out.println ("column:" + new string (keyValue.getFamily ()) + "= = value:" + new string (Keyvalue.getvalue ()));}}} catch (ioexception e) {e.printstacktrace ();}} Public void querybycondition1 (String tablename) {try {connection con = this.getconnection (); Table table = con.gettable (tablename.valueof (TableName)); Get scan = new get ("112233BBBCCCC". GetBytes ());// based on Rowkey query result r = table.get (scan); System.out.println ("Get to Rowkey:" + new string (R.getrow ()));for (keyvalue keyvalue : r.raw ())   {SYSTEM.OUT.PRINTLN ("column:" + new string (keyvalue.getfamily ()) + "= = value:" + new string (Keyvalue.getvalue ()));}} catch (ioexception e) {e.printstacktrace ();}} Public void querybycondition2 (String tablename) {try {connection con = this.getconnection (); Table table = con.gettable (tablename.valueof (TableName)); Filter filter = new singlecolumnvaluefilter (Bytes.tobytes ("Column1"), null, Compareop.equal,bytes.tobytes ("Aaa2")); // query when column Column1 has a value of AAA Scan s = new scan (); S.setfilter (filter); Resultscanner rs = table.getscanner (s);for (RESULT R : RS) { System.out.println ("Get to Rowkey:" + new string (R.getrow ()));for (keyvalue keyvalue : r.raw ()) {system.out.println ("column:" + new string (keyvalue.getfamily ()) + "= = = = Value: " + new sTring (Keyvalue.getvalue ()));}}} catch (exception e) {e.printstacktrace ();}} Public void querybycondition3 (String tablename) {try {connection con = this.getconnection (); Table table = con.gettable (tablename.valueof (TableName));//extract Rowkey with CCCC end data//filter Filter1 = new rowfilter (Compareop.equal,new regexstringcomparator (". *cccc$"));//// Extract the Rowkey to contain the BBB data//filter filter1 = new rowfilter (compareop.equal,new Substringcomparator ("BBB"));//extract Rowkey data starting with 123 Filter filter1 = new rowfilter ( Compareop.equal,new binaryprefixcomparator ("1122". GetBytes ()));//filter filter1 = new rowfilter (Compareop.equal,//new binarycomparator (Bytes.tobytes (Rowkey))); Scan scan = new scan (); Scan.setfilter (Filter1); Resultscanner rs = table.getscanner (Scan);for (result r : rs) { System.out.priNtln ("Get to Rowkey:" + new string (R.getrow ()));for (Cell cell : r.rawcells ())  {SYSTEM.OUT.PRINTLN ("Column:" + new string (cellutil.clonefamily (cell)) + "= = value:" + New string (Cellutil.clonevalue (cell)));}} Rs.close ();} catch (exception e) {e.printstacktrace ();}} Public void querybycondition4 (String tablename) {try {connection con = this.getconnection (); Table table = con.gettable (tablename.valueof (TableName)); Filter filter1 = new valuefilter (Compareop.equal,new substringcomparator ("Aaa2")); Scan scan = new scan (); Scan.setfilter (Filter1); Resultscanner rs = table.getscanner (Scan);for (result r : rs) { System.out.println ("Get to Rowkey:" + new string (R.getrow ()));for (cell cell : R.rawcells ()) {system.out.println ("column:" + new string (CellUtil.clonefamily (cell)) + "= = value:" + new string (Cellutil.clonevalue (cell));}} Rs.close ();} catch (exception e) {e.printstacktrace ();}} Public void close () throws IOException {if (connection != null) { Connection.close ();}} Public connection getconnection () throws IOException {Configuration Configuration = hbaseconfiguration.create (); Configuration.set ("Hbase.zookeeper.property.clientPort", "2181") Configuration.set ("Hbase.zookeeper.quorum", "192.168.17.108");if (connection == null) {connection = connectionfactory.createconnection (configuration);} Return connection;} Public static void main (String[] args) {hbasetest3 hbasetest = new hbasetest3 ();try {string tablename = "Test1"; hbasetest.createtable ("test1"); hbasetest.insertdata ("Test1"); hbasetesT.queryall ("Test1");// hbasetest.querybycondition1 (tableName);// hbasetest.querybycondition2 ( TableName);//hbasetest.querybycondition3 (tableName);//hbasetest.querybycondition4 (tableName); Hbasetest.close () ;} catch (exception e) {e.printstacktrace ();}}
Operation Result:
Habse Java API Usage actions