Hbase programming example

Source: Internet
Author: User

Summary: Hbase is installed in the previous article. You can perform some operations through hbase shell, but it is inconvenient to associate it with the actual programming instance. Therefore, this article introduces the hbase programming instance.

 

1. Use eclipse to develop hbase applications

1. Create a New Java project in eclipse, name it hbasetest, right-click Properties, select Java build path, and select Add external jars to import the jar package in the hbase/lib directory.

2. Create the conf folder under the project root directory and copy the hbase-site.xml file under hbase/conf to the folder, right-click Properties> JAVA buildpath> libraries> Add class folder and select the conf folder.

 

Ii. Brief Introduction to hbasejava APIs

1. hbaseconfiguration

Link: org. Apache. hadoop. hbase. hbaseconfiguration

Purpose: You can configure hbase through this class.

2, hbaseadmin

Link: org. Apache. hadoop. hbase. Client. hbaseadmin

Function: provides an interface for managing table information in hbase databases. It provides methods to create and Delete tables.

 

3. htabledescriptor

Link: org. Apache. hadoop. hbase. Client. htabledescriptor

Role: contains the table name and its corresponding column family. Provided methods include:

Void addfamily (hcolumndescriptor) to add a column family

Hcolumndescriptor removefamily (byte [] column) Remove a column family

Byte [] getname () to get the table name

Byte [] getvalue (byte [] Key) Get the attribute value

Void setvalue (string key, stringvalue) sets the attribute value

 

4, hcolumndescriptor

Link: org. Apache. hadoop. hbase. Client. hcolumndescriptor

Purpose: maintain column information. Provided methods include:

Byte [] getname () Get the name of the column family

Byte [] getvalue () gets the value of the corresponding attribute

Void setvalue (string key, string value) sets the value of the corresponding attribute

 

5. htable

Link: org. Apache. hadoop. hbase. Client. htable

Purpose: The user communicates with the hbase table. This method is non-thread-safe for update operations. If multiple threads are started to attempt to communicate with a single htable instance, the write buffer may crash.

 

6. Put

Link: org. Apache. hadoop. hbase. Client. Put

Role: Used to add a single row

7. Get

Link: org. Apache. hadoop. hbase. Client. Get

Function: used to obtain information about a single row.

8, result

Link: org. Apache. hadoop. hbase. Client. Result

Purpose: store the single row value obtained after the get or scan operation.

9. resulttasks

Link: Interface

Function: interface for Obtaining values from the client.

 

Iii. hbase Java API simple example

 

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. client. hbaseadmin; import Org. apache. hadoop. hbase. client. htable; import Org. apache. hadoop. hbase. client. put; import Org. apache. hadoop. hbase. client. get; 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. util. bytes;/** @ author minglaihan */public class hbasetest {static configuration CFG = hbaseconfiguration. create (); // use hbaseadmin htabledescriptor to create a new public static void create (string tablename, string columnfamily) throws exception {hbaseadmin admin = new hbaseadmin (CFG); If (Admin. tableexists (tablename) {system. out. println ("Table exist"); system. exit (0);} else {htabledescriptor tabledescriptor = new htabledescriptor (tablename); tabledescriptor. addfamily (New hcolumndescriptor (columnfamily); Admin. createtable (tabledescriptor); system. out. println ("Table create success") ;}// Add a data record. Use htable put to add public static void put (string tablename, string row, string columnfamily, string column, string data) throws ioexception {htable table = new htable (CFG, tablename); Put put = new put (bytes. tobytes (ROW); Put. add (bytes. tobytes (columnfamily), bytes. tobytes (column), bytes. tobytes (data); table. put (Put); system. out. println ("put success");} // obtain the result set public static void get (string tablename, string row) of the row column in The tablename table) throws ioexception {htable table = new htable (CFG, tablename); get = new get (bytes. tobytes (ROW); Result result = table. get (get); system. out. println ("get" + result);} // use htable scan to obtain all data information of the tablename table. Public static void scan (string tablename) throws ioexception {htable table = new htable (CFG, tablename); scan = new scan (); resulttables = table. getscanner (SCAN); For (Result s: result)) {system. out. println ("scan" + resultfailed);} public static Boolean Delete (string tablename) throws exception {hbaseadmin admin = new hbaseadmin (CFG); If (Admin. tableexists (tablename) {try {admin. disabletable (tablename); Admin. deletetable (tablename);} catch (exception e) {// todo: handle has tione. printstacktrace (); Return false ;}} return true;} public static void main (string [] ARGs) {string tablename = "hbase_test"; string columnfamily = "C1 "; try {hbasetest. create (tablename, columnfamily); hbasetest. put (tablename, "row1", columnfamily, "column1", "data1"); hbasetest. get (tablename, "row1"); hbasetest. scan (tablename); If (hbasetest. delete (tablename) = true) {system. out. println ("Delete table" + tablename + "success") ;}} catch (exception e) {// todo: handle has tione. printstacktrace ();}}}

 

Run the result of commenting out the delete step:

 



 

Iv. Summary

The interaction between hbase and Java APIs is currently outstanding. In future hadoop programming, you can make proper use of hbase to improve the overall level.


Reprinted please indicate the source: http://www.ming-yue.cn/hbase-program-examples/


Hbase programming example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.