Hbase Jave Programming

Source: Internet
Author: User

1. Build the environment

Copy the configured hbase directory in Linux to go to Windows. Of course, you do not need to use the hbase directory developed in Linux.

Create a Java project and add the jar packages under the hbase directory and lib to the class path.

In addition, the external class folder is added to the conf directory of hbase. Most connection operations are performed because this part is not configured.

2. OK

Edit the code and run it.

  

Package hbase; 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. client. delete; import Org. apache. hadoop. hbase. client. hbaseadmin; import Org. apache. hadoop. hbase. client. htable; import Org. apache. hadoop. hbase. client. htableinterface; 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. resulttables; import Org. apache. hadoop. hbase. client. scan; public class hbasedemo {public static configuration conf; static {conf = hbaseconfiguration. create ();} public static void main (string [] ARGs) throws exception {createtable ("test1 ");} /*** query all data * @ Param tablename * @ throws exception */public static void queryall (string tablename) throws exception {system. out. println ("query all start .... "); htablepool pool = new htablepool (Conf, 1000); htableinterface table = pool. gettable (tablename); resultpartition partition = table. getask( new scan (); For (result: Success) {system. out. println ("rowkey: \ t" + new string (result. getrow (); For (Org. apache. hadoop. hbase. keyValue KV: result. raw () {system. out. println ("column: \ t" + new string (KV. getfamily () + ":" + new string (KV. getvalue () ;}}/ *** Delete row * @ Param tablename * @ Param rowkey * @ throws exception */public static void deleterow (string tablename, string rowkey) throws exception {htable table = new htable (Conf, tablename); List list = new arraylist (); Delete d1 = new Delete (rowkey. getbytes (); list. add (D1); table. delete (list); system. out. println ("Delete row end .... ");}/*** Delete table * @ Param tablename * @ throws exception */public static void droptable (string tablename) throws exception {hbaseadmin admin = new hbaseadmin (CONF ); admin. disabletable (tablename); Admin. deletetable (tablename); Admin. close ();}/**** insert data * @ Param tablename * @ throws exception */public static void insertdata (string tablename) throws exception {system. out. println ("insert data start ..... "); htablepool pool = new htablepool (Conf, 1000); htableinterface table = pool. gettable (tablename); Put put = new put ("row001 ". getbytes (); Put. add ("TF1 ". getbytes (), null, "AAA ". getbytes (); Put. add ("TF2 ". getbytes (), null, "AAA ". getbytes (); Put. add ("tf3 ". getbytes (), null, "AAA ". getbytes (); table. put (Put); system. out. println ("insert data end ..... ");}/*** create table * @ Param tablename * @ throws exception */public static void createtable (string tablename) throws exception {system. out. println ("create table .... "); hbaseadmin admin = new hbaseadmin (CONF); // If the table to be created exists, delete it first, and then create if (Admin. tableexists (tablename) {admin. disabletable (tablename); Admin. deletetable (tablename); system. out. println (tablename + "table exists, delete .... ");} htabledescriptor descriptor = new htabledescriptor (tablename); descriptor. addfamily (New hcolumndescriptor ("TF1"); descriptor. addfamily (New hcolumndescriptor ("TF2"); descriptor. addfamily (New hcolumndescriptor ("tf3"); Admin. createtable (descriptor); Admin. close (); system. out. println ("create table finished .... ");}}

3. Linux hbase Shell

> List

List all tables

> Status

> Version

> Scan 'tablename'

Browse data

"Describe 'tablename'

> Descrive extended 'tablename'

......................

Reference: http://javacrazyer.iteye.com/blog/1186881

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.