Java hbase operation example

Source: Internet
Author: User

Java hbase operation example

 

To operate hbase in java, create a java project in eclipse, import the jar package in the root directory of the hbase installation file and the jar package in the lib directory to the project, and then write the java code to operate hbase. The following code provides a simple example:

 

/*** @ Date 2015-07-23 21:28:10 * @ author sgl */package com. songguoliang. hbase; 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. apac He. 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. put; 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; public CIA Ss HBaseTest {// declare static Configuration, HBaseConfigurationprivate static Configuration conf = null; static {conf = HBaseConfiguration. create (); conf. set (hbase. zookeeper. quorum, sdw1, sdw2 );} /*** create a table ** @ date 2015-07-23 21:44:32 * @ author sgl * @ param tableName table name * @ param columnFamilys column family * @ throws IOException */public static void createTable (String tableName, string [] columnFamilys) throws IOException {HBaseAdmin admin = n Ew HBaseAdmin (conf); if (admin. tableExists (tableName) {System. out. println (table already exists !);} Else {HTableDescriptor tableDesc = new HTableDescriptor (tableName); for (int I = 0; I
 
  
List = new ArrayList
  
   
(); Delete delete Delete = new Delete (rowKey. getBytes (); list. add (delete); table. delete (list); System. out. println (delete record + rowKey + success !);} /*** Get a row record * @ date 2015-07-23 22:21:33 * @ author sgl * @ param tableName table name * @ param rowKey row Jian * @ throws IOException */public static void getOneRecord (String tableName, string rowKey) throws IOException {HTable table = new HTable (conf, tableName); Get 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 Stri Ng (kv. getFamily () +:); System. out. print (new String (kv. getQualifier () +); System. out. print (kv. getTimestamp () +); System. out. println (new String (kv. getValue ()));}} /*** get all data * @ date 2015-07-23 22:26:19 * @ author sgl * @ param tableName table name * @ throws IOException */public static void getAllRecord (String tableName) throws IOException {HTable table = new HTable (conf, tableName); Scan scan = new Scan (); R Esultpartition partition = table. getask( scan); for (Result result: Success) {for (KeyValue kv: result. 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 () ;}}/ *** delete a table * @ date 2015-07-23 22:29:35 * @ author sgl * @ para M tableName table name * @ throws IOException */public static void deleteTable (String tableName) throws IOException {HBaseAdmin admin = new HBaseAdmin (conf); admin. disableTable (tableName); admin. deleteTable (tableName); System. out. println (delete table + tableName + success !);} Public static void main (String [] args) {try {String tableName = scores; String [] columnFamilys = {grade, course}; HBaseTest. createTable (tableName, columnFamilys); HBaseTest. addRecord (tableName, sgl, grade, 5); HBaseTest. addRecord (tableName, sgl, course, 90); HBaseTest. addRecord (tableName, sgl, course, math, 97); HBaseTest. addRecord (tableName, sgl, course, art, 87); HBaseTest. addRecord (tableName, guoguo, grade, 4); HBaseTest. addRecord (tableName, guoguo, course, math, 89); System. out. println (********* get one record **********); HBaseTest. getOneRecord (tableName, sgl); System. out. println (********* get all record **********); HBaseTest. getAllRecord (tableName); System. out. println (********* delete one record **********); HBaseTest. deleteRecord (tableName, guoguo); HBaseTest. getAllRecord (tableName); System. out. println (********** delete table **********); HBaseTest. deleteTable (tableName);} catch (IOException e) {e. printStackTrace ();}}}
  
 


 

 

 

Related Article

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.