HBase provides access to the Java API, which is as important as JDBC when using RDBMS with Java applications
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.keyvalue;import Org.apache.hadoop.hbase.client.delete;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.htablepool;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 HBase {//declares statically configured static Configuration conf = null; static {conf = Hbaseconfiguration.create (); Conf.set ("Hbase.zookeeper.quorum", "localhost"); }/* * CREATE TABLE * * @tableName table name * * @family column Family list */public static void CREAttable (String tableName, string[] family) throws Exception {hbaseadmin admin = new hbaseadmin (conf); Htabledescriptor desc = new Htabledescriptor (tableName); for (int i = 0; i < family.length; i++) {desc.addfamily (new Hcolumndescriptor (Family[i])); } if (Admin.tableexists (TableName)) {System.out.println ("table exists!"); System.exit (0); } else {admin.createtable (desc); SYSTEM.OUT.PRINTLN ("CREATE Table success!"); }/* * Add data to the table (for fixed tables that know how many column families are) * * @rowKey rowKey * * @tableName table name * * @column1 first column Family List * * @value1 the list of values for the first column * * @column2 second column family list * * @value2 a list of values for the second column */public static VO ID addData (String rowKey, String tableName, string[] column1, string[] value1, string[] column2, string[] Value 2) throws IOException {put put = new put (Bytes.tobytes (RowKey));//Set RowKey htable table = new htable (conf, bytes.tobytes (tableName));//Htabel responsible for record-related operations such as additions and deletions, etc. Get table hcolumndescriptor[] columnfamilies = Table.gettabledescriptor ()//Get All the column families. Getcolumnfamilies (); for (int i = 0; i < columnfamilies.length; i++) {String familyname = columnfamilies[i].getnameasstring (); /Gets the column family name if (familyname.equals ("article")) {//Article column family put data for (int j = 0; J < Column1.len Gth J + +) {Put.add (Bytes.tobytes (familyname), Bytes.tobytes (Column1[j]), BYTES.T Obytes (Value1[j])); }} if (Familyname.equals ("author")) {//Author column family put data for (int j = 0; J < column 2.length; J + +) {Put.add (Bytes.tobytes (familyname), Bytes.tobytes (Column2[j]), BYTES.T Obytes (Value2[j])); } }} table.put (Put); SYSTEM.OUT.PRINTLN ("Add Data success!"); }/* * Based on rwokey query * * @rowKey rowKey * * @tableName table name */public static Result GetResult (S Tring TableName, String RowKey) throws IOException {Get get = new Get (Bytes.tobytes (RowKey)); htable table = new htable (conf, bytes.tobytes (tableName));//Get table result = Table.get (get); For (KeyValue kv:result.list ()) {System.out.println ("family:" + bytes.tostring (kv.getfamily ())); System.out. println ("qualifier:" + bytes.tostring (Kv.getqualifier ())); System.out.println ("Value:" + bytes.tostring (Kv.getvalue ())); System.out.println ("Timestamp:" + kv.gettimestamp ()); System.out.println ("-------------------------------------------"); } return result; }/* * Traversal query hbase table * * @tableName table name */public static void Getresultscann (StRing TableName) throws IOException {Scan scan = new scan (); Resultscanner rs = null; htable table = new htable (conf, bytes.tobytes (tableName)); try {rs = Table.getscanner (scan); for (Result r:rs) {KeyValue kv:r.list ()) {System.out.println ("row:" + bytes.t Ostring (Kv.getrow ())); System.out.println ("Family:" + bytes.tostring (kv.getfamily ())); System.out.println ("qualifier:" + bytes.tostring (Kv.getqualifier ())); System.out. println ("value:" + bytes.tostring (Kv.getvalue ())); System.out.println ("timestamp:" + kv.gettimestamp ()); System.out. println ("-------------------------------------------"); }}} finally {Rs.close (); }}/* * Traverse query HBase table * * @tableName table name */public static void Getresultscann (String tableName, String Start_rowkey, String Stop_rowkey) throws IOException {Scan scan = new scan (); Scan.setstartrow (Bytes.tobytes (Start_rowkey)); Scan.setstoprow (Bytes.tobytes (Stop_rowkey)); Resultscanner rs = null; htable table = new htable (conf, bytes.tobytes (tableName)); try {rs = Table.getscanner (scan); for (Result r:rs) {KeyValue kv:r.list ()) {System.out.println ("row:" + bytes.t Ostring (Kv.getrow ())); System.out.println ("Family:" + bytes.tostring (kv.getfamily ())); System.out.println ("qualifier:" + bytes.tostring (Kv.getqualifier ())); System.out. println ("value:" + bytes.tostring (Kv.getvalue ())); System.out.println ("Timestamp:" +Kv.gettimestamp ()); System.out. println ("-------------------------------------------"); }}} finally {Rs.close (); }/* * Query a column in the table * * @tableName table name * * @rowKey RowKey */public static void GETRESULTB Ycolumn (String tableName, String RowKey, String familyname, String columnName) throws IOException {Htab Le table = new htable (conf, bytes.tobytes (tableName)); Get get = new Get (Bytes.tobytes (RowKey)); Get.addcolumn (Bytes.tobytes (familyname), Bytes.tobytes (ColumnName)); Gets the column that corresponds to the specified column family and column modifier, result = Table.get (get); For (KeyValue kv:result.list ()) {System.out.println ("family:" + bytes.tostring (kv.getfamily ())); System.out. println ("qualifier:" + bytes.tostring (Kv.getqualifier ())); System.out.println ("Value:" + bytes.tostring (Kv.getvalue ())); SyStem.out.println ("Timestamp:" + kv.gettimestamp ()); System.out.println ("-------------------------------------------"); }/* * Update a column in the table * * @tableName table name * * @rowKey rowKey * * @familyName column Family name * * @columnName Column Name * * @value Updated value */public static void UpdateTable (String tableName, String RowKey, String familyname, String columnName, String value) throws IOException {htable table = new HTABL E (Conf, bytes.tobytes (tableName)); Put put = new put (Bytes.tobytes (RowKey)); Put.add (Bytes.tobytes (familyname), Bytes.tobytes (ColumnName), bytes.tobytes (value)); Table.put (Put); SYSTEM.OUT.PRINTLN ("Update table success!"); * * * Query Multiple versions of a column of data * * @tableName table name * * @rowKey rowKey * * @familyName Column Family name * * @columnName Column name */public static void Getresultbyversion (String tableName, String RowKey, String familyname, String columnName) throws IOException {htable table = new htable (conf, bytes.tobytes (Tablenam e)); Get get = new Get (Bytes.tobytes (RowKey)); Get.addcolumn (Bytes.tobytes (familyname), Bytes.tobytes (ColumnName)); Get.setmaxversions (5); Result result = Table.get (get); For (KeyValue kv:result.list ()) {System.out.println ("family:" + bytes.tostring (kv.getfamily ())); System.out. println ("qualifier:" + bytes.tostring (Kv.getqualifier ())); System.out.println ("Value:" + bytes.tostring (Kv.getvalue ())); System.out.println ("Timestamp:" + kv.gettimestamp ()); System.out.println ("-------------------------------------------"); }/* * list<?> results = table.get (GET). List (); iterator<?> it = * Results.iterator (); while (It.hasnext ()) {* SYSTEM.OUT.PRINTLN (It.next (). toString ());} */}/* Delete the specified column * * @tableName Table name * * @rowKey rowKey * * @familyName Column Family name * * @columnName Column name * * Pub Lic static void DeleteColumn (String tableName, String RowKey, String falilyname, String columnName) throws Ioex ception {htable table = new htable (conf, bytes.tobytes (tableName)); Delete DeleteColumn = new Delete (Bytes.tobytes (RowKey)); Deletecolumn.deletecolumns (Bytes.tobytes (Falilyname), Bytes.tobytes (ColumnName)); Table.delete (DeleteColumn); System.out.println (Falilyname + ":" + ColumnName + "is deleted!");} /* * Delete the specified column * * @tableName table name * * @rowKey RowKey */public static void Deleteallcolumn (String TableName, String RowKey) throws IOException {htable table = new htable (conf, bytes.tobytes (TABLENAME) ); Delete DeleteAll = new Delete (Bytes.tobytes (RowKey)); Table.delete (DELETEALL); System.out.println ("All columns is deleted!"); }/* * Delete table * * @tableName table name */public static void Deletetable (String tableName) throws Ioexcept Ion {hbaseadmin admin = new hbaseadmin (conf); Admin.disabletable (TableName); Admin.deletetable (TableName); System.out.println (TableName + "is deleted!"); } public static void Main (string[] args) throws Exception {//create TABLE String tableName = "BLOG2"; String[] Family = {"article", "Author"}; Creattable (TableName, family); Add data to the table string[] Column1 = {"title", "Content", "tag"}; String[] value1 = {"Head first HBase", "HBase is the Hadoop database." Use it if you need random, realtime Read/write access to your Big Data. "," Hadoop,hbase,nosql "}; String[] Column2 = {"Name", "nickname"}; String[] value2 = {"Nicholas", "Lee"}; AddData ("Rowkey1", "blog2", Column1, Value1, Column2, value2); AddData ("Rowkey2", "Blog2", Column1, Value1, Column2, value2); AddData ("Rowkey3", "blog2", Column1, Value1, Column2, value2); Traverse Query Getresultscann ("Blog2", "Rowkey4", "Rowkey5"); Traverse Query Getresultscann ("Blog2", "Rowkey4", "Rowkey5") according to the row key range; Query GetResult ("Blog2", "rowkey1"); Query the value of a column getresultbycolumn ("Blog2", "Rowkey1", "author", "Name"); Update column updateTable ("Blog2", "Rowkey1", "author", "Name", "Bin"); Query the value of a column getresultbycolumn ("Blog2", "Rowkey1", "author", "Name"); Querying multiple versions of a column getresultbyversion ("Blog2", "Rowkey1", "author", "Name"); Delete a column of DeleteColumn ("Blog2", "Rowkey1", "Author", "nickname"); Delete all Columns Deleteallcolumn ("Blog2", "rowkey1"); Delete Table deletetable ("blog2"); }}
HBase Learning (12) Java API and HBase Interactive instance