1006-hbase operation Combat (JAVA API status)

Source: Internet
Author: User
Tags zookeeper

first, the preparatory stageDevelopment Environment: Hadoop : hadoop -2.4.0 hbase : hbase -0.94.11-security Eclipse:juno Service Release 2
ii. Creation of Hbasedemo Project
1. Create a new Java project from Eclipse2. Right-click the project root folder and select "Propertiesà> Java Build pathà> libraryà> Add External JARs" 3. Add jar file to classpath 3.1 Unpacking the HBase installation file 3.2 Copy hbase-0.94.7-security.jar, hbase- 0.94.11-security-tests.jar to Project 3.3 lib Sub-folder all the jar packages are added to the Build path of this project
Iii. using the hbase JAVA API
Initialize and dispose of resource methods:
public void init () data initialization
public void destory ()   Free resources

Authentication method:
public void testcreate () Validation create TABLE
public void Testdrop () Verify delete table
public void Testput () Validate join table data
public void Testgetbyrowkey () Verify how to get data through Rowkey
public void Testscanstoend () Validate range row data get
public void Testscanall ()   Verify full table Scan

Method:
Public list<cell> Get (string tableName, string rowKey)   Insert table data through RowKey
public list <Result> Scan (string tableName)   query all information by table name
Public list<result> Scan (string tablename,string Startrow,string stoprow) Query range row method
public void Create (String tableName, string[] cfs)   CREATE table by table name and column family information
Public void Drop (string tableName) Delete table
public void put (string tableName, list<cellbean> values) insert hbase based on provided table and object information

First step: Encapsulate a storage cell in HBase Cell object

/** * Encapsulates the storage cell object in HBase * @author SHENFL * @version: V1.0 * @Date: 2015-6-8 */public class Cellbean {//Line Jian private String row key;//column Family private string columnfamilly;//column name private string Columnname;//cell value private String Columnvalue;public string Getrowkey () {return rowKey;} public void Setrowkey (String rowKey) {this.rowkey = RowKey;} Public String getcolumnfamilly () {return columnfamilly;} public void setcolumnfamilly (String columnfamilly) {this.columnfamilly = columnfamilly;} Public String getColumnName () {return columnName;} public void Setcolumnname (String columnName) {this.columnname = ColumnName;} Public String GetColumnValue () {return columnvalue;} public void Setcolumnvalue (String columnvalue) {this.columnvalue = Columnvalue;}}

Step Two: Use the HBase JAVA API to manipulate the HBase database
/** * Using the HBase JAVA API operation * * @author SHENFL * */public class Hbasetest {Configuration config = null;//Create action table for image Hbaseadmin admin = null;//hbase connection Hconnection conn = null, @Beforepublic void init () {config = Hbaseconfiguration.create ();//HBase just Need to know zookeeper. will be able to manipulate the regionserver on the, set hbase connection Zookeeperconfig.set ("Hbase.zookeeper.quorum", " 192.168.2.35:2181,192.168.2.36:2181,192.168.2.37:2181 "); try {conn = hconnectionmanager.createconnection (config); admin = new hbaseadmin (config);} catch (Exception e) {e.printstacktrace ();}} @Afterpublic void Destory () {try {admin.close (); Conn.close ();} catch (IOException e) {e.printstacktrace ();}} @Testpublic void Testcreate () {String tableName = "Account"; string[] columnfamilies = new string[] {"Info", "Other"};create (TableName, columnfamilies);} @Testpublic void Testdrop () {Drop ("account"); @Testpublic void Testput () {//Set Table object list list<cellbean> cblist = new arraylist<cellbean> ();// Set table name accountstring TableName = "Account"; Cellbean e = null;for (int i=0;i<3;i++) {e = new Cellbean (); E.setrowkey ("g20142500" +i); e.setcolumnfamilly ("info"); E.setcolumnname (" Username "); E.setcolumnvalue (" SHENFL "+i); Cblist.add (e); e = new Cellbean (); E.setrowkey (" g20142500 "+i); E.setcolumnfamilly ("other"); E.setcolumnname ("career"); E.setcolumnvalue ("singer" +i); Cblist.add (e);} Put (TableName, cblist);} @Testpublic void Testgetbyrowkey () {String tableName = "Account"; String RowKey = "g201425001"; List<cell> cells = Get (TableName, RowKey); StringBuffer info = new StringBuffer (); for (Cell c:cells) {//Use the Cellutil method to output the corresponding column. The hbase0.96 version number uses the Cellutil function Info.append (new string (Cellutil.clonefamily (c))). Append (":"). Append (New String ( Cellutil.clonequalifier (c)). Append ("\ t"). Append (New String (Cellutil.clonevalue (c))). Append ("\ n"); SYSTEM.OUT.PRINTLN (info);} @Testpublic void Testscanstoend () {stringbuffer sb = new StringBuffer (); String tableName = "Account"; String StartRow = "g201425000"; String Stoprow = "g201425002"; list<result> rslist = Scan (TableName, StartRow,Stoprow) byte[] RowKey = null;byte[] Username = null;byte[] career = null;for (Result rs:rslist) {RowKey = Rs.getrow (); Usern Ame = Rs.getvalue (bytes.tobytes ("info"), Bytes.tobytes ("username")), career = Rs.getvalue (Bytes.tobytes ("other"), Bytes.tobytes ("career")), Sb.append (new String (RowKey)). Append ("\ t"). Append (new string (username)). append ("\ t"). Append (new String (Career)). Append ("\ n");} System.out.println (Sb.tostring ());} @Testpublic void Testscanall () {stringbuffer sb = new StringBuffer (); list<result> rslist = Scan ("account"), for (Result rs:rslist) {list<cell> listcells = Rs.listcells (), for (Cel L c:listcells) {//Use the Cellutil method to output the corresponding column. The hbase0.96 version number uses the Cellutil function Sb.append (new string (Cellutil.clonerow (c))). Append ("\ t"). Append (New String ( Cellutil.clonefamily (c)). Append (":"). Append (New String (Cellutil.clonequalifier (c))). Append ("\ t"). Append (New String (Cellutil.clonevalue (c))). Append ("\ n");}} System.out.println (SB);} /** * Insert Table data by Rowkey * @param tableName table name * @param rowkey LineKin * @return */public list<cell> Get (String tableName, String rowKey) {Htableinterface table = null; Get get = null; Result rs = null; list<cell> listcells = new arraylist<cell> (); try {table = conn.gettable (tableName); get = new Get ( Bytes.tobytes (RowKey)); rs = Table.get (get); listcells = Rs.listcells (); } catch (IOException e) {e.printstacktrace ();} Finally{try {table.close ();} catch (IOException e) {e.printstacktrace ()}} return listcells;} /** * Query All Lines * * @param tableName * @return */public list<result> Scan (String tableName) {htableinterface table = Nu ll list<result> rslist = new arraylist<result> (); try {table = conn.gettable (tableName); Scan scan = new scan (); Resultscanner scanner = Table.getscanner (scan);iterator<result> Iterator = Scanner.iterator (); Result rs = null;while (Iterator.hasnext ()) {rs = (Result) iterator.next (); Rslist.add (RS);}} catch (Exception e) {e.printstacktrace ();} Finally{try {table.close ();} catch (IOException e) {e.printstacktRace ();}} return rslist;} /** * Query Range row * @param tableName table name * @param startrow start of the line Jian * @param stoprow end Line Kin * @return */public list<result> s Can (String tablename,string startrow,string stoprow) {htableinterface table = null; list<result> rslist = new arraylist<result> (); try {table = conn.gettable (tableName); Scan scan = new scan (); Scan.setstartrow (Bytes.tobytes (StartRow)); Scan.setstoprow (Bytes.tobytes (Stoprow)); Resultscanner rs = table.getscanner (scan); for (Result V:rs) {rslist.add (v);}} catch (Exception e) {e.printstacktrace ();} Finally{try {table.close ();} catch (IOException e) {e.printstacktrace ()}} return rslist;} /** * Create Poster * * @param tableName * Table name * @param cfs * Column family */public void Create (String tableName, Strin G[] cfs) {if (cfs = = NULL | | cfs.length = = 0) {return;} try {//Verify if the table stores if (Admin.tableexists (TableName)) {return;} CREATE TABLE Htabledescriptor desc = new Htabledescriptor (tablename.valueof (TableName));//Create Column family for (String Cf:cfs) {DESC.ADDFA Mily (nEW hcolumndescriptor (CF));} CREATE TABLE admin.createtable (DESC);} catch (IOException e) {e.printstacktrace ();}} /** * Delete Table * * @param tableName * table name */public void Drop (String tableName) {try {if (admin.tableexists (tableName) {admin.disabletable (tableName); admin.deletetable (TableName);}} catch (IOException e) {e.printstacktrace ();}} /** * Insert data into the specified table * * @param tableName * Table name * @param values * data */public void put (String tableName, L Ist<cellbean> values) {if (Stringutils.isblank (tableName) | | values = = NULL | | values.size () = = 0) {return;} put put = null; Htableinterface table = null;try {table = conn.gettable (tableName); for (Cellbean v:values) {put = new put (Bytes.tobytes ( V.getrowkey ()));p Ut.add (Bytes.tobytes (v.getcolumnfamilly ()), Bytes.tobytes (V.getcolumnname ()), Bytes.toBytes ( V.getcolumnvalue ())); Table.put (put);}} catch (Exception e) {//Actual production environment to be logged by logging. For example: Logger.warn ("xxxxx", E); E.printstacktrace ();} Finally{try {table.close ();} catch (IOException e) {E.printStackTrace ();}}} 





Articles:1. HBase Connection Pool--htablepool after being deprecatedhttp://blog.csdn.net/u010967382/article/details/380468212. HBase Java API Introductionhttp://www.cnblogs.com/NicholasLee/archive/2012/09/13/2683432.html3. HBase Java API Operation CaseHttp://www.programcreek.com/java-api-examples/index.php?api=org.apache.hadoop.hbase.HTableDescriptorhttp://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Admin.htmlhttp://blog.csdn.net/hadoop_/article/details/11481215

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

1006-hbase operation Combat (JAVA API status)

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.