1. Run eclipse, create a new Java project "hbaseclient", right-click the project root directory, select "properties"-> "Java build path"-> "library"-> "add external jars ", after hbase is decompressed, all the jar packages under the hbase-0.94.1-security.jar, hbase-0.94.1-security-tests.jar, and Lib subdirectory under the root directory are added to the classpath of this project.
2. Follow the steps in step 1 to add your own connected hbase configuration file hbase-site.xml to the classpath of this project, as shown in an example of the configuration file:
<configuration><property> <name>hbase.rootdir</name> <value>hdfs://localhost:9000/hbase</value> </property> </configuration>
This step is still available on the Internet, but I did not perform this step and the results are displayed correctly.
Hbase provides Java APIs to manage hbase, including table management and data operations. Common API operations include:
1. You can use hbaseadmin to create, delete, display, and modify tables. Once a table is created, you can access the table through an htable instance and add data to the table each time.
2. Insert data
Create a Put object. In this put object, you can specify the column to be added with data and the current timestamp equivalent, and then call htable. put (Put) to submit the operation. The child monkey reminds me that when creating a Put object, you must specify a row value, input as a parameter when constructing a Put object.
3. Get Data
To obtain data, use the get object. The get object has several constructor functions like the Put object. when constructing the get object, input the row value, indicating the data of the first row. get (get.
4. Browse each row
Scan can be used to browse the rows in the table to obtain information about each row, such as the column name and timestamp. Scan is equivalent to a cursor. Next () is used to browse the next row and htable is called. getask( scan) to return a resultresponse object. Both htable. Get (get) and htable. getsums (SCAN) return a result. Result is
KeyValue linked list.
5. Delete
Delete is used to delete records, and htable. Delete (delete) is called to delete records. (Note: deletion is special, that is, deletion does not immediately delete data from the table .)
6. Lock
Adding, obtaining, and deleting a lock will apply to the operated row during the operation, but browsing will not.
7. Cluster access
The client code accesses and finds the cluster through zookeeper. That is to say, Zookeeper quorum will be used. Then, the related classes (packages) should be in the class directory of the client, that is, the client must find the file hbase-site.xml.
Create a class:
Import Java. io. ioexception; import Java. util. arraylist; import Java. util. list; 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. masternotrunningexception; import Org. apache. hadoop. hbA Se. zookeeperconnectionexception; 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. result; import Org. apache. hadoop. hbase. client. resulttables; import Org. apache. hadoop. hbase. client. scan; import Org. apache. hadoop. hbase. client. P Ut; import Org. apache. hadoop. hbase. util. bytes; public class hbasetest {Private Static configuration conf = NULL;/*** initialize configuration */static {conf = hbaseconfiguration. create ();}/*** create a table */public static void creattable (string tablename, string [] familys) throws exception {hbaseadmin admin = new hbaseadmin (CONF ); if (Admin. tableexists (tablename) {system. out. println ("table already exists! ");} Else {htabledescriptor tabledesc = new htabledescriptor (tablename); For (INT I = 0; I <familys. length; I ++) {tabledesc. addfamily (New hcolumndescriptor (familys [I]);} admin. createtable (tabledesc); system. out. println ("create table" + tablename + "OK. ") ;}}/*** Delete table */public static void deletetable (string tablename) throws exception {try {hbaseadmin admin = new hbaseadmin (CONF); Admin. disabletable (tablename); Admin. deletetable (tablename); system. out. println ("Delete table" + tablename + "OK. ");} catch (masternotrunningexception e) {e. printstacktrace ();} catch (zookeeperconnectionexception e) {e. printstacktrace () ;}/ ** insert a row record */public static void addrecord (string tablename, string rowkey, string family, string qualifier, string value) throws exception {try {htable table = new htable (Conf, tablename); Put put = new put (bytes. tobytes (rowkey); Put. add (bytes. tobytes (family), bytes. tobytes (qualifier), bytes. tobytes (value); table. put (Put); system. out. println ("insert recored" + rowkey + "to table" + tablename + "OK. ");} catch (ioexception e) {e. printstacktrace () ;}/ *** delete a row of records */public static void delrecord (string tablename, string rowkey) throws ioexception {htable table = new htable (Conf, tablename ); list list = new arraylist (); Delete del = new Delete (rowkey. getbytes (); list. add (DEL); table. delete (list); system. out. println ("del recored" + rowkey + "OK. ");}/*** query a row record */public static void getonerecord (string tablename, string rowkey) throws ioexception {htable table = new htable (Conf, tablename ); get = new get (rowkey. getbytes (); Result rs = table. get (get); For (keyValue KV: Rs. raw () {system. out. print (new string (KV. getrow () + ""); system. out. print (new string (KV. getfamily () + ":"); system. out. print (new string (KV. getqualifier () + ""); system. out. print (KV. gettimestamp () + ""); system. out. println (new string (KV. getvalue ();}/** show all data */public static void getallrecord (string tablename) {try {htable table = new htable (Conf, tablename ); scan S = new scan (); result1_ss = table. getiterator (s); For (result R: SS) {for (keyValue KV: R. raw () {system. out. print (new string (KV. getrow () + ""); system. out. print (new string (KV. getfamily () + ":"); system. out. print (new string (KV. getqualifier () + ""); system. out. print (KV. gettimestamp () + ""); system. out. println (new string (KV. getvalue () ;}} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] AGRs) {try {string tablename = "scores"; string [] familys = {"Grade ", "Course"}; hbasetest. creattable (tablename, familys); // Add record zkb hbasetest. addrecord (tablename, "zkb", "Grade", "", "5"); hbasetest. addrecord (tablename, "zkb", "course", "", "90"); hbasetest. addrecord (tablename, "zkb", "course", "math", "97"); hbasetest. addrecord (tablename, "zkb", "course", "art", "87"); // Add record baoniu hbasetest. addrecord (tablename, "baoniu", "Grade", "", "4"); hbasetest. addrecord (tablename, "baoniu", "course", "math", "89"); system. out. println ("=========== get one record ======="); hbasetest. getonerecord (tablename, "zkb"); system. out. println ("=========== show all record ======="); hbasetest. getallrecord (tablename); system. out. println ("=========== del one record ======="); hbasetest. delrecord (tablename, "baoniu"); hbasetest. getallrecord (tablename); system. out. println ("=========== show all record ======="); hbasetest. getallrecord (tablename);} catch (exception e) {e. printstacktrace ();}}}
3. The result is displayed as follows:
Create Table scores OK.
Insert recored zkb to table scores OK.
Insert recored zkb to table scores OK.
Insert recored zkb to table scores OK.
Insert recored zkb to table scores OK.
Insert recored baoniu to table scores OK.
Insert recored baoniu to table scores OK.
=========== Get one record ==========
Zkb Course: 1345450733304 90
Zkb Course: Art 1345450733323 87
Zkb Course: Math 1345450733316 97
Zkb grade: 1345450733294 5
============ Show all record ========
Baoniu Course: Math 1345450733333 89
Baoniu grades: 1345450733328 4
Zkb Course: 1345450733304 90
Zkb Course: Art 1345450733323 87
Zkb Course: Math 1345450733316 97
Zkb grade: 1345450733294 5
=========== Del one record ==========
Del recored baoniu OK.
Zkb Course: 1345450733304 90
Zkb Course: Art 1345450733323 87
Zkb Course: Math 1345450733316 97
Zkb grade: 1345450733294 5
============ Show all record ========
Zkb Course: 1345450733304 90
Zkb Course: Art 1345450733323 87
Zkb Course: Math 1345450733316 97
Zkb grade: 1345450733294 5