Big Data Learning series of three-----HBase Java Api Graphic Detailed

Source: Internet
Author: User
Tags bulk insert zookeeper

Introduction

In the previous Big Data Learning Series two-----hbase Environment Building (standalone), successfully set up a hadoop+hbase environment, this article mainly on the use of Java to hbase some operations.

First, prepare beforehand 1. Confirm that Hadoop and HBase start successfully

2. Verify that the firewall is shutting down the required rack pack for 3.maven
<!--Hadoop-related racks--<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.8.2</version> </dependency > <dependency> <groupId>org.apache.hadoop</groupId> <artifactid>ha doop-client</artifactid> <version>2.8.2</version> </dependency> <depen Dency> <groupId>org.apache.hadoop</groupId> &LT;ARTIFACTID&GT;HADOOP-HDFS&LT;/ARTIFAC tid> <version>2.8.2</version> </dependency> <dependency> &L T;groupid>org.apache.hadoop</groupid> <artifactid>hadoop-mapreduce-client-core</artifactid&gt            ; <version>2.8.2</version> </dependency> <dependency> <groupid>org.ap Ache.hadoop</groupId> <artifactId>hadoop-yarn-common</artifactId> <version>2.8.2</ve rsion> </dependency><!--HBase related Jars--<dependency> <groupid>org.apach E.hbase</groupid> <artifactId>hbase-hadoop-compat</artifactId> <version>1.3. 1</version> </dependency> <dependency> <groupid>org.apache.hbase</grou        Pid> <artifactId>hbase-server</artifactId> <version>1.1.2</version> </dependency><dependency> <groupId>org.apache.hbase</groupId> <artifact Id>hbase-client</artifactid> <version>1.1.2</version> </dependency><depend Ency> <groupId>org.apache.hbase</groupId> <artifactid>hbase-common</artifact Id> <veRsion>1.1.2</version> </dependency> 
4. Modifying the Hosts file (optional)

Modify the Hosts file under the Windows C:\Windows\System32\drivers\etc directory to add HBase host IP and host name for relational mapping.

192.168.238.128 Master

Note: If you do not use mappings, change the hostname in your code to IP.

The principle of 5.HBase

This article is described in detail:
http://blog.csdn.net/woshiwanxin102213/article/details/17584043

Ii. Test Example 1. Create a table

Create two tables, T_student, T_student_info, and add two column families
It can be seen in the hbase shell and the 16010 interface after the creation is successful.

2. Add Data

After the table is successfully created, insert the data in both tables.
Because HBase is a dynamic database, columns can be added.
HBase New and modified is a method, the data is the same, and later the data will be overwritten out of the previous!

3. Querying data

Query based on table name, row Health, column family, columns, respectively

4. Delete data

Delete one of these data

Third, code sample tool class
Import Java.io.ioexception;import java.util.arraylist;import Java.util.hashmap;import Java.util.List;import Java.util.map;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.hbase.cell;import Org.apache.hadoop.hbase.cellutil;import Org.apache.hadoop.hbase.hbaseconfiguration;import Org.apache.hadoop.hbase.hcolumndescriptor;import Org.apache.hadoop.hbase.htabledescriptor;import Org.apache.hadoop.hbase.tablename;import Org.apache.hadoop.hbase.client.admin;import Org.apache.hadoop.hbase.client.connection;import Org.apache.hadoop.hbase.client.connectionfactory;import Org.apache.hadoop.hbase.client.delete;import Org.apache.hadoop.hbase.client.get;import Org.apache.hadoop.hbase.client.put;import Org.apache.hadoop.hbase.client.result;import Org.apache.hadoop.hbase.client.resultscanner;import Org.apache.hadoop.hbase.client.scan;import Org.apache.hadoop.hbase.client.table;import Org.apache.hadoop.hbase.util.bytes;import com.alibaba.fastjson.jsonobject;/** * * Title:hbaseutil * Description:hbase Tool class * version:1.0.0 * @author PANCM * @date December 6, 2017 */public class Hbaseutil {/** Hadoop connection */    private static Configuration conf = null;    /** hbase Connection */private static Connection con = null;    /** Session */private static admin admin = null;    private static String IP = "Master";    private static String port = "2181";   private static String Port1 = "9001";        Initialize the connection static {//Get the configuration file object conf = Hbaseconfiguration.create ();        Set Configuration Parameters Conf.set ("Hbase.zookeeper.quorum", IP);          Conf.set ("Hbase.zookeeper.property.clientPort", port);    If HBase is a cluster, this must be added//This IP and port is configured in the Hadoop/mapred-site.xml configuration file Conf.set ("Hbase.master", ip+ ":" +port1);            }/** * Get connection * * @return */public synchronized static Connection getconnection () {try { if (null = = Con | | con.isclosed ()) {//Get Connection object con = Connectionfactory.createconnec            tion (conf); }        } catch (IOException e) {System.out.println ("Get Connection Failed!");        E.printstacktrace ();    } return con; }/** * Connection closed */public static void Close () {try {if (admin! = null) {ADM            In.close ();            } if (con! = null) {con.close (); }} catch (IOException e) {System.out.println ("Connection shutdown failed!            ");        E.printstacktrace ();     }}/** * CREATE TABLE * * @param tableName * Table name * @param columnfamily * Column Family */public static void Creattable (String tableName, string[] columnfamily) {if (null==tablename| |        Tablename.length () ==0) {return; } if (null==columnfamily| |        columnfamily.length==0) {return;        }//CREATE table name object TableName tn = tablename.valueof (TableName); A. Determine if the database exists try {//get session admin = getconnection (). GetadmIn ();                if (Admin.tableexists (TN)) {System.out.println (tableName + "table exists, delete table ....");                Set the table to non-editable admin.disabletable (TN) first;                Delete Table admin.deletetable (TN);            SYSTEM.OUT.PRINTLN ("Table Delete succeeded ...");            }//CREATE table structure object Htabledescriptor HTD = new Htabledescriptor (TN); for (String str:columnfamily) {//Create column Family Structure object Hcolumndescriptor HCD = new Hcolumndescriptor (                STR);            Htd.addfamily (HCD);            }//CREATE TABLE admin.createtable (HTD); System.out.println (tableName + "table created successfully!)        ");        } catch (IOException e) {e.printstacktrace ();        } finally {close (); }}/** * Data single INSERT or UPDATE * * @param tableName * Table name * @param rowKey * Line Jian (main Key) * @param family * Column family * @param qualifier * column * @PARAM value * @return * */public static void Insert (String tableName, String RowKey, Strin        G Family, String qualifier, String value) {Table t = null;            try {t = getconnection (). GetTable (Tablename.valueof (TableName));            Put put = new put (Bytes.tobytes (RowKey));            Put.addcolumn (Bytes.tobytes (family), bytes.tobytes (qualifier), bytes.tobytes (value));            T.put (Put);        System.out.println (tableName + "update succeeded!");            } catch (IOException e) {System.out.println (tableName + "update failed!");        E.printstacktrace ();        } finally {close (); }}/** * Data BULK INSERT or UPDATE * * @param tableName * Table name * @param list * hbase number According to * @return */public static void Insertbatch (String tableName, list<?> List) {if (Null = = Table Name | |        Tablename.length () ==0) {return;    }    if (null = = List | | list.size () = = 0) {return;        } Table t = null;        put put = null;        Jsonobject json = NULL;        List<put> puts = new arraylist<put> ();            try {t = getconnection (). GetTable (Tablename.valueof (TableName));                for (int i = 0, j = list.size (), I < J; i++) {json = (jsonobject) list.get (i);                put = new put (Bytes.tobytes (json.getstring ("RowKey"));                        Put.addcolumn (Bytes.tobytes (json.getstring ("Family")), Bytes.tobytes (json.getstring ("qualifier")),                Bytes.tobytes (json.getstring ("value"));            Puts.add (Put);            } t.put (puts);        System.out.println (tableName + "update succeeded!");            } catch (IOException e) {System.out.println (tableName + "update failed!");        E.printstacktrace ();        } finally {close (); }}/** * Data delete * @param tablenAME Table name * @param rowKey * @return * */public static void Delete (String tableName, String rowKey) {    Delete (Tablename,rowkey, "", "");    }/** * Data delete * @param tableName table name * @param rowKey * @param family Column Family * * @return */    public static void Delete (String tableName, String RowKey, string family) {Delete (tablename,rowkey,family, ""); }/** * Data delete * @param tableName table name * @param rowKey * @param family Row Family * @param qualifie R column * @return */public static void Delete (String tableName, String RowKey, String family, string qu Alifier) {if (null = = TableName | |        Tablename.length () ==0) {return;        } if (null = = RowKey | | rowkey.length () = = 0) {return;        } Table t = null;            try {t = getconnection (). GetTable (Tablename.valueof (TableName));            Delete del = new Delete (Bytes.tobytes (RowKey)); /If the column family is not empty if (null! = Family && family.length () > 0) {//If the column is not empty if (n                            Ull! = Qualifier && qualifier.length () > 0) {del.addcolumn (bytes.tobytes),                Bytes.tobytes (qualifier));                } else {del.addfamily (bytes.tobytes (family));            }} t.delete (Del);            } catch (IOException e) {System.out.println ("Delete failed!");        E.printstacktrace ();        } finally {close (); }}/** * Query all data in the table * * @param tableName * Table name */public static void Select (Strin G tableName) {if (null==tablename| |        Tablename.length () ==0) {return;        } Table t = null;        List<map<string,object>> list=new arraylist<map<string,object>> (); try {t = getconnection (). GetTable (Tablename.valueof (TablenaMe));            Read operation scan scan = new scan ();            Get a scanned result set Resultscanner rs = T.getscanner (scan);            if (null = = rs) {return;                 } for (Result result:rs) {//Get cell collection List<cell> cs = Result.listcells ();                if (NULL = = CS | | cs.size () = = 0) {continue; } for (Cell Cell:cs) {map<string,object> map=new hashmap<string, object> (                    ); Map.put ("RowKey", bytes.tostring (Cellutil.clonerow (cell)));//Take the line Jian Map.put ("timestamp", Cell.gettimestam                    P ());//Take timestamp map.put ("Family", bytes.tostring (cellutil.clonefamily (cell)));//Fetch to column family Map.put ("qualifier", bytes.tostring (Cellutil.clonequalifier (cell)));//Fetch to column map.put ("Value", bytes.tostr   ING (cellutil.clonevalue (cell)));//Fetch the value list.add (map);             }} System.out.println ("Queried data:" +list);            } catch (IOException e) {System.out.println ("Query failed!");        E.printstacktrace ();        } finally {close (); }}/** * Based on table name and row health query * @param tableName * @param rowKey */public static void Select (String tablen    Ame, String rowKey) {Select (Tablename,rowkey, "", ""); /** * Based on table name, row health, and column family query * @param tableName * @param rowKey * @param family */public static void SE    Lect (String tableName, String RowKey, String family) {Select (Tablename,rowkey,family, "");     /** * Query based on condition detail * * @param tableName * Table name * @param rowKey * Line Health (primary key) * @param family * Column family * @param qualifier * Column * * public static void Select (String        TableName, String RowKey, String family, string qualifier) {Table t = null; List<map<strinG,object>> list=new arraylist<map<string,object>> ();            try {t = getconnection (). GetTable (Tablename.valueof (TableName));            Query by get in hbase for Get get = new Get (Bytes.tobytes (RowKey)); If the column family is not empty if (null! = Family && family.length () > 0) {//If the column is not empty if (                            Null! = Qualifier && qualifier.length () > 0) {get.addcolumn (bytes.tobytes (family),                Bytes.tobytes (qualifier));                } else {get.addfamily (bytes.tobytes (family));            }} Result R = T.get (get);            List<cell> cs = R.listcells ();            if (NULL = = CS | | cs.size () = = 0) {return;                } for (Cell Cell:cs) {map<string,object> map=new hashmap<string, object> (); Map.put ("RowKey", Bytes.tostring (Cellutil.clonerow (cell));//Map.put ("timestamp", Cell.gettimestamp ());//Fetch timestamp map.put ("Family", Bytes.tos Tring (cellutil.clonefamily (cell)));//Fetch to the column family Map.put ("qualifier", bytes.tostring (Cellutil.clonequalifier (cell             ));//Fetch to the column map.put ("Value", bytes.tostring (Cellutil.clonevalue (cell)));//Take the value list.add (map);        } System.out.println ("Queried data:" +list);            } catch (IOException e) {System.out.println ("Query failed!");        E.printstacktrace ();        } finally {close (); }    }}
Test code
Import Java.util.arraylist;import java.util.list;import com.alibaba.fastjson.jsonobject;/** * * title:hbasetest* Description:hbase Related Test * version:1.0.0 * @author pancm* @date November 23, 2017 */public class Hbasetest {public static voi    D main (string[] args) {test (); }/** * Some tests * * * private static void Test () {String tablename1= "t_student", tablename2= "T_student_info        ";        String []columnfamily1={"St1", "St2"};        String []columnfamily2={"STF1", "STF2"};        Hbaseutil.creattable (tableName1, columnFamily1);        Hbaseutil.creattable (tableName2, columnFamily2);        Hbaseutil.insert (tableName1, "1001", Columnfamily1[0], "name", "Zhangsan");        Hbaseutil.insert (tableName1, "1002", Columnfamily1[0], "name", "Lisi");        Hbaseutil.insert (tableName1, "1001", Columnfamily1[1], "age", "18");        Hbaseutil.insert (tableName1, "1002", columnfamily1[1], "age", "20"); Hbaseutil.insert (tableName2, "1001", Columnfamily2[0], "phone", "123456");        Hbaseutil.insert (tableName2, "1002", columnfamily2[0], "phone", "234567");        Hbaseutil.insert (tableName2, "1001", Columnfamily2[1], "Mail", "[email protected]");        Hbaseutil.insert (tableName2, "1002", columnfamily2[1], "Mail", "[email protected]"); Hbaseutil.select (TABLENAME1); Query the table for all data Hbaseutil.select (tableName1, "1001"); Based on table name and row health query Hbaseutil.select (tableName2, "1002", columnfamily2[0]); Query Hbaseutil.select (tableName2, "1002", columnfamily2[1], "Mail") based on table name, row health, and column family; Based on table name, row Health, column family, and column query Hbaseutil.select (tableName1, "1002"); Based on table name and row health query Hbaseutil.delete (tableName1, "1002", columnfamily1[0]);//delete data Hbaseutil.select (tableName1, "1002 "); Based on table name and row health query}}
Other

Copyright Notice:
Empty Realm
Blog Park Source: http://www.cnblogs.com/xuwujing
CSDN Source: HTTP://BLOG.CSDN.NET/QAZWSXPCM
Personal blog Source: http://www.panchengming.com
Original is not easy, reproduced please indicate the source, thank you!

Big Data Learning series of three-----HBase Java Api Graphic Detailed

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.