Hadoop Learning Note -15.hbase Framework Learning (basic practice)

Source: Internet
Author: User

One, hbase installation Configuration 1.1 pseudo-distribution mode installation

Pseudo-Distribution mode installation is the role of deploying HBase on a single computer, Hmaster, Hregionserver, and zookeeper are all emulated on a single computer.

First of all, prepare the HBase installation package, I use the HBase-0.94.7 version, has been uploaded to the Baidu Network disk (URL:HTTP://PAN.BAIDU.COM/S/1PJ3HTY7)

(1) Copy the HBase installation package to the virtual machine Hadoop-master via FTP and perform a series of actions: Unzip, rename, set environment variables

① Decompression: tar-zvxf hbase-0.94.7-security.tar.gz

② renaming: MV Hbase-94.7-security hbase

③ SET environment variable: vim/etc/profile, add content as follows, modify and re-enter into effect: Source/etc/profile

Export Hbase_home=/usr/local/hbase

Export path=.: $HADOOP _home/bin: $HBASE _home/bin: $ZOOKEEPER _home/bin: $JAVA _home/bin: $PATH

(2) Enter the hbase/conf directory, modify the hbase-env.sh file:

Export JAVA_HOME=/USR/LOCAL/JDK
Export Hbase_manages_zk=true #告诉HBase使用它自己的zookeeper实例, you need to set to false in distributed mode

(3) Under the Hbase/conf directory, continue to modify the Hbase-site.xml file:

<property>
<name>hbase.rootdir</name>
<value>hdfs://hadoop-master:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop-master</value>
</property>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>

(4) " Optional Step Together" modify the regionservers file to change localhost to host name: Hadoop-master

(5) Start HBase:start-hbase.sh

PS: from the previous article, HBase is built on Hadoop HDFs, so make sure Hadoop is started before starting HBase, and the command to start Hadoop is:start-all.sh

(6) Verify that HBase is started:JPS

  

By discovery, there are three more Java processes:hmaster, Hregionserver, and Hquorumpeer.

You can also view the Web interface by accessing HBase:http://hadoop-master:60010

1.2 Distributed mode installation

This installation in the 1.1 section of the pseudo-distribution model based on the modification to build a distributed model, the cluster experiment environment structure as shown:

By the know, The Hmaster role is 192.168.80.100 (host name: Hadoop-master), while two hregionserver roles are two 192.168.80.101 (hostname: HADOOP-SLAVE1) and 192.168.80.102 (hostname: HA DOOP-SLAVE2) composed of.

(1) Modify several key configuration files on the Hadoop-master server:

① Modify hbase/conf/hbase-env.sh: Change the last line to the following

Export Hbase_manages_zk=false #不使用HBase自带的zookeeper实例

② Modify Hbase/conf/regionservers: Change the original hadoop-master to the following content

Hadoop-slave1

Hadoop-slave2

(2) Copy the HBase folder on Hadoop-master and the/etc/profile configuration file to Hadoop-slave1 and Hadoop-slave2 as a whole:

Scp-r/usr/local/hbase hadoop-slave1:/usr/local/

Scp-r/usr/local/hbase hadoop-slave2:/usr/local/

Scp/etc/profile hadoop-slave1:/etc/

Scp/etc/profile hadoop-slave2:/etc/

(3) Make the configuration file effective in hadoop-slave1 and hadoop-slave2:

Source/etc/profile

(4) Start Hadoop, zookeeper, and HBase in Hadoop-master: (Note the sequencing)

start-all.sh

zkserver.sh start

start-hbase.sh

(5) View the HBase cluster status in the Web interface of HBase:

Two, HBase Shell Basic Command 2.1 DDL: Create and delete tables

(1) Create a table:

>create ' users ', ' user_id ', ' address ', ' info '

#这里创建了一张表users, there are three column families User_id,address,info

Get a specific description of the table users:

>describe ' users '

(2) List all tables:

>list

  

(3) Delete table: Deleting a table in HBase takes two steps, first disable, and then drop

>disable ' users '

>drop ' users '

2.2 DML: Adding and deleting changes

(1) Add record: Put

>put ' users ', ' xiaoming ', ' info:age ', ' 24 ';

>put ' users ', ' xiaoming ', ' info:birthday ', ' 1987-06-17 ';

>put ' users ', ' xiaoming ', ' info:company ', ' Alibaba ';

>put ' users ', ' xiaoming ', ' address:contry ', ' China ';

>put ' users ', ' xiaoming ', ' address:province ', ' Zhejiang ';

>put ' users ', ' xiaoming ', ' address:city ', ' Hangzhou ';

(2) scan all records of the users table: Scan

>scan ' users '

(3) Get a record

① get all data for an ID (row_key)

>get ' users ', ' xiaoming '

② gets an ID of all the data for a column family

>get ' users ', ' xiaoming ', ' info '

③ gets an ID, all data for a column in a column family

>get ' users ', ' xiaoming ', ' info:age '

(4) Update a record: still put

For example: Update the Users table for Xiaoming's age of 29

>put ' users ', ' xiaoming ', ' info:age ', ' 29 '

>get ' users ', ' xiaoming ', ' info:age

(5) Delete record: Delete and DeleteAll

① the ' info:age ' field of the Xiaoming value is deleted

>delete ' users ', ' xiaoming ', ' info:age '

② Delete entire line information for Xiaoming

>deleteall ' users ', ' xiaoming '

2.3 Other: A few more useful commands

(1) Count: Count rows

>count ' users '

(2) Truncate: Empty the specified table

>truncate ' users '

Iii. HBase Java API Operations 3.1 preparatory work

(1) Importing project jar packages for HBase

(2) Import all dependent jar packages under Hbase/lib

3.2 HBase Java Development prerequisites: Get Configuration
    /*      * Get hbase configuration     *     /privatestatic  configuration GetConfiguration ( )    {        = hbaseconfiguration.create ();        Conf.set ("Hbase.rootdir", "Hdfs://hadoop-master:9000/hbase");         // You must add this when using Eclipse, or you cannot locate        Conf.set ("Hbase.zookeeper.quorum", "Hadoop-master");                 return conf;    }    
3.3 DDL operation with Hbaseadmin

(1) Create a table

/ ** Create a table*/    Private Static voidcreatetable ()throwsIOException {hbaseadmin admin=Newhbaseadmin (GetConfiguration ()); if(admin.tableexists (table_name)) {SYSTEM.OUT.PRINTLN ("The table is existed!"); }Else{htabledescriptor Tabledesc=NewHtabledescriptor (table_name); Tabledesc.addfamily (NewHcolumndescriptor (family_name));            Admin.createtable (TABLEDESC); System.out.println ("Create table success!"); }    }

(2) Delete a table

    /** Delete Table*/    Private Static voiddroptable (String tableName)throwsIOException {hbaseadmin admin=Newhbaseadmin (GetConfiguration ()); if(Admin.tableexists (tableName)) {Try{admin.disabletable (tableName);            Admin.deletetable (TableName); } Catch(IOException e) {e.printstacktrace (); System.out.println ("Delete" +tablename+ "failed!"); }} System.out.println ("Delete" +tablename+ "success!"); }
3.4 Using htable for DML operations

(1) New record

     Public Static void Putrecord (String tableName, String row,             string columnfamily, String column, string data)                     throws
     ioexception{        new  htable (GetConfiguration (), tableName);         New Put (bytes.tobytes (Row));        P1.add (Bytes.tobytes (columnfamily), bytes.tobytes (column),     bytes.tobytes (data));        Table.put (p1);        System.out.println ("put" "+row+" ', "+columnfamily+": "+column+" ', ' "+data+" ' ");    }

(2) Reading records

     Public Static void throws ioexception{        new  htable (GetConfiguration (), tableName);         New Get (bytes.tobytes (Row));         = Table.get (get);        System.out.println ("Get:" +result);    

(3) Full table scan

     Public Static void throws ioexception{    new  htable (GetConfiguration (), tableName);     New Scan ();     = Table.getscanner (scan);        for (Result result:scanner) {        System.out.println ("Scan:" +Result);    }    }

Zhou Xurong

Source: http://www.cnblogs.com/edisonchou/

The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.

Hadoop Learning Note -15.hbase Framework Learning (basic practice)

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.