Hadoop (ix)-hbase shell commands and Java interfaces

Source: Internet
Author: User

A. Shell command

1. Enter the HBase command line./hbase Shell


2. Display the list of tables in HBase

3. Create a user table, including info, data two column families
Create ' user ', ' info ', ' data '
Create ' user ', {NAME = ' info ', VERSIONS = ' 3 '}


4. Insert the information into the table:

Inserts information into the user table. Row key is rk0001. The column family info is added with the Name column identifier, and the value is Zhangsan
Put ' user ', ' rk0001 ', ' info:name ', ' Zhangsan '

Inserts information into the user table. Row key is rk0001. The column family info is added with the Gender column identifier, and the value is female
Put ' user ', ' rk0001 ', ' info:gender ', ' female '

Inserts information into the user table. Row key is rk0001, and the Age column identifier is added to the column family info. Value of 20
Put ' user ', ' rk0001 ', ' info:age ', 20

Insert a message into the user table, row key is rk0001, and a pic column identifier is added to the column family data, the value is picture
Put ' user ', ' rk0001 ', ' data:pic ', ' picture '


5. Get the information in the table:

Get all the information for row key rk0001 in the user table
Get ' user ', ' rk0001 '

Gets all the information for row key in the user table for the Rk0001,info column family
Get ' user ', ' rk0001 ', ' info '

Gets information about the name, age column identifier of row key in the user table for the Rk0001,info column family
Get ' user ', ' rk0001 ', ' info:name ', ' info:age '

Gets the information for row key rk0001,info, data column family in the user table
Get ' user ', ' rk0001 ', ' info ', ' data '
Get ' user ', ' rk0001 ', {COLUMN = = [' Info ', ' data '}
Get ' user ', ' rk0001 ', {COLUMN = = ' Info:name ', ' data:pic '}

Get information about the latest 5 in the user table for row key rk0001, column family info, version
Get ' people ', ' rk0002 ', {COLUMN = ' info ', VERSIONS = 2}
Get ' user ', ' rk0001 ', {COLUMN = ' info:name ', VERSIONS = 5}
Get ' user ', ' rk0001 ', {COLUMN = ' info:name ', VERSIONS = 5, Timerange = [1392368783980, 1392380169184]}

Gets the user table in row key for rk0001. The value of the cell is Zhangsan information
Get ' people ', ' rk0001 ', {FILTER = ' valuefilter (=, ' binary: Picture ') '}

Gets the information in the user table for row key rk0001, which contains a in the column designator
Get ' people ', ' rk0001 ', {FILTER = ' (Qualifierfilter (=, ' substring:a ')) "}

Put ' user ', ' rk0002 ', ' info:name ', ' fanbingbing '
Put ' user ', ' rk0002 ', ' info:gender ', ' female '
Put ' user ', ' rk0002 ', ' info:nationality ', ' China '
Get ' user ', ' rk0002 ', {FILTER = ' valuefilter (=, ' binary: China ') '}


6. Query the information in the table:

Querying all the information in the user table
Scan ' user '

Querying information in the User table for column family info
Scan ' People ', {COLUMNS = ' info '}
Scan ' user ', {COLUMNS = ' info ', RAW = true, VERSIONS = 5}
Scan ' Persion ', {COLUMNS = ' info ', RAW = true, VERSIONS = 3}
Querying information for column families in the user table for info and data
Scan ' user ', {COLUMNS + = [' info ', ' data '}
Scan ' user ', {COLUMNS = [' Info:name ', ' Data:pic ']}

Querying the user table for information about the column family is info, and the column identifier is name
Scan ' user ', {COLUMNS = ' info:name '}

Query the user table for information about the column family as info, the column identifier name, and the latest 5 version number
Scan ' user ', {COLUMNS = ' info:name ', VERSIONS = 5}

Query for information in the user table that has the column family as info and data and that contains the a character in the column identifiers
Scan ' People ', {COLUMNS = [' info ', ' data '], FILTER = ' (Qualifierfilter (=, ' substring:a ') "}

Query the column family in the user table for info. RK Range is data of [rk0001, rk0003)
Scan ' People ', {COLUMNS = ' info ', StartRow = ' rk0001 ', Endrow = ' rk0003 '}

Query the user table for row key starting with the RK character
Scan ' user ',{filter=> ' prefixfilter (' RK ')}

Querying data from a specified range in the user table
Scan ' user ', {timerange = [1392368783980, 1392380169184]}

7. Delete the data in the table:
Delete the user table row key is rk0001, and the column designator is info:name data

Delete ' People ', ' rk0001 ', ' info:name '


Delete the user table row key is rk0001, and the column identifier is info:name. Data for timestamp of 1392383705316
Delete ' user ', ' rk0001 ', ' info:name ', 1392383705316

Emptying data in the user table
Truncate ' People '


8. Other operations

ALTER TABLE structure:
First deactivate the user table (the new version number is not used)
Disable ' user '

Join two column families F1 and F2
Alter ' people ', NAME = ' F1 '

Alter ' user ', NAME = ' F2 '


Enabling table enable ' user '


# # #disable ' user ' (new version number not used)
Delete a column family:
Alter ' user ', NAME = ' F1 ', METHOD = ' delete ' or alter ' user ', ' delete ' = ' F1 '

Join the column Family F1 delete the column family at the same time F2
Alter ' user ', {name = ' F1 '}, {name = ' F2 ', METHOD = ' Delete '}

Change the F1 column family version of the user table to 5
Alter ' people ', NAME + ' info ', VERSIONS = 5
Enable table
Enable ' user '

Delete a table
Disable ' user '

Drop ' user '



Two. Java interface

public class Hbasedemo {private Configuration conf = null; @Beforepublic void init () {conf = Hbaseconfiguration.create (); Co Nf.set ("Hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");} @Testpublic void Testdrop () throws exception{hbaseadmin admin = new hbaseadmin (conf); Admin.disabletable ("account"); Admin.deletetable ("account"); Admin.close ();} @Testpublic void Testput () throws exception{htable table = new htable (conf, "user"); Put put = new put (Bytes.tobytes ("rk0003"));p Ut.add (bytes.tobytes ("info"), Bytes.tobytes ("name"), Bytes.tobytes (" Liuyan ")); Table.put (put); Table.close ();} @Testpublic void Testget () throws exception{htable table = new htable (conf, "user"); Get get = new Get (Bytes.tobytes ("rk0001")); Get.setmaxversions (5); Result result = Table.get (get), for (KeyValue kv:result.list ()) {String family = new String (kv.getfamily ()); String qualifier = new String (Kv.getqualifier ()); String value = new String (Kv.getvalue ()); System.out.println ("Family:" + ", qualifier:" + qualifier + ", Value:"+ value);} Table.close ();} @Testpublic void Testscan () throws Exception{htablepool pool = new Htablepool (conf, 10); Htableinterface table = pool.gettable ("user"); Scan scan = new Scan (bytes.tobytes ("rk0001"), Bytes.tobytes ("rk0002")), scan.addfamily (bytes.tobytes ("info")); Resultscanner scanner = Table.getscanner (scan); for (Result R:scanner) {byte[] value = R.getvalue (bytes.tobytes ("info"), Bytes.tobytes ("name")); System.out.println (new String (value));} Pool.close ();} @Testpublic void Testdel () throws exception{htable table = new htable (conf, "user");D elete del = new Delete (Bytes.tobytes ( "rk0001"));d El.deletecolumn (bytes.tobytes ("Data"), Bytes.tobytes ("pic")), Table.delete (DEL); Table.close ();}}




Hadoop (ix)-hbase shell commands and Java interfaces

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.