HBase entry, hbase

Source: Internet
Author: User

HBase entry, hbase
I. Basic concepts of HBase

1. Row key

The Row primary key can only rely on the Row key when querying HBase. HBase does not support conditional queries and other query methods similar to some mainstream databases. Reading records only depends on the Row primary key and performs global scanning, you can think of a row primary key as the primary key (for example, id) used in the Query Process of a mainstream database ).

2. Column Family

The column family can be imagined as a big manager of all columns in the table structure in the daily mainstream database. The column family stores the names of all columns and the total number of columns in the table, the number of columns included (excluding the Row key and Timestamp columns ).

3. Column

Column. Each Column in HBase belongs to a Column family and uses the Column family name as the prefix. All columns in the same Column family are clustered in one storage unit and sorted by Column key.

4. Timestamp

In HBase, a copy of data is determined by row key And Colum Family. The same row key And Colum Family may have multiple different copies of data. HBase uses time stamps to distinguish the data, at the same time, the data is sorted by the timestamp. The latest data is placed at the top. The default timestamp is the current system time (accurate to milliseconds). You can also set this value manually.

5. Value

When we precisely query data in an HBase table, we use TableName to find the table, then use Row key to find the corresponding Row, and then use ColumnKey to find the corresponding column, finally, find the latest value to be queried Based on the timestamp. This value is value.

6. Storage Type

In HBase, the table name is a string, the row key and column name are binary values (that is, Byte [] in Java), and The timestamp is a 64-bit integer (long type in Java ), the final query result Value is a byte array (byte [] type in Java ).

7. Storage Structure

In HBase, the entire data table is sorted by the row key. Each row contains any number of columns. Columns and columns are sorted by the column key. Each column contains several pieces of data, the entire HBase storage structure can be understood as follows:

Table (

Row key, List (

SortedMap (

Column, list (

Value, Timestamp

)

)

)

)

Ii. HBase basic Shell commands

1. Create a table

> Create 'table name', 'column family ',... ellipsis (...) indicates that several columns can be appended.

2. Add record

> Put 'table name', 'row key', 'column family: column name', 'data'

> Put 'table name', 'row key', 'column family: column name', 'data ',

>...

The preceding statement adds several records to the table. data indicates the data to be added. column family: column name indicates adding data to a column in the column family, the columns in each row can be different, and there can be multiple columns in each row.

3. Query

As we can see above, HBase only supports query by Row key or full table scan. Data Tables are sorted by row key lexicographically. Each Row contains any number of columns, all columns are automatically sorted by column key.

> Get 'table name', 'row key'

4. Update

> Put 'table name', 'row key', 'column family name: Column name', 'data'

The preceding statement updates the value of the data to be updated in the table to data.

5. Conditional Query

> Get 'table name', 'row key', {COLUNM => 'column family name: Column name', VERSIONS => n}

The preceding statement queries n records in the column name in the table. Each column has many records which are sorted in reverse chronological order.

> Get 'table name', 'row key', {COLUNM => 'column family name: Column name', TIMESTAMP => time}

The preceding statement queries all records whose timestamp is greater than time in the column name in the table.

6. delete records

> Delete 'table name', 'row key', 'column family name: Column name'

In HBase, the delete statement can only delete one Column. The preceding statement deletes all data named Column name.

> Delete 'table name', 'row key'

Delete all data in the key row

7. delete a table

> Disable 'table name'

> Delete 'table name'

When deleting a table, you must make the table disable in HBase and then use the delete statement.

2. Use Java APIs to operate HBase

1. load configuration

Copy the hbase-site.xml file under HBase to the project directory,

1 Configuration conf = new Configuration; 2 conf = HBaseConfiguration. create (conf); 3 // conf. addResource ("hbase-site-cluster.xml"); // you can load the specified file

2. Create an HBase table

1/* --------------------- create a table ---------------- */2 HTableDescriptor table = new HTableDescriptor ("table-name"); 3 table. addFamily (new HColumnDescriptor ("column family name1"); 4 table. addFamily (new HColumnDescriptor ("column family name2"); 5 admin. createTable (table );View Code

3. Add records

1/* ---------------------- add record ----------------- */2 Put = new put (Bytes. toBytes ("row key"); 3 put. add (Byte. toBytes ("column family name"), Bytes. toBytes ("column name"), Bytes. toBytes ("content"); 4 put. add (Byte. toBytes ("column family name"), Bytes. toBytes ("column name"), Bytes. toBytes ("content"); 5 put. add (Byte. toBytes ("column family name"), Bytes. toBytes ("column name"), Bytes. toBytes ("content"); 6 put. add (Byte. toBytes ("column family name"), Bytes. toBytes ("column name"), Bytes. toBytes ("content"); 7 table. put (put );

4. query records

1/* ------------------- query by row key ------------------- */2 Get = new get (Bytes. toBytes ("row key"); 3 Result result = table. get (get); 4 for (KeyValue var: reslut) {5 System. out. println ("columnfamily:" + Bytes. toString (var. getFamily (); 6 System. out. println ("Quantity:" + Bytes. toString (var. getQualifier (); 7 System. out. println ("value:" + Bytes. toString (var. getValue (); 8 System. out. println ("time:" + Bytes. toString (var. getTimestamp (); 9}

5. Update Data

1/* --------------- update data --------------- */2 Get = new get (Bytes. toBytes ("row key"); 3 get. addColumn (Bytes. toBytes ("column family"), Bytes. toBytes ("column name"); 4 assertThat (Bytes. toString (table. get (get ). list. get (0 ). getValue (), is ("Original Value"); 5 Put put = new Put (Bytes. toBytes ("Row key"); 6 put. add (Bytes. toBytes ("column family"), Bytes. toBytes ("column name"), Bytes. toBytes ("New Value"); 7 table. put (put );View Code

6. delete records

1/* --------------- Delete record -------------- */2 // delete specified column 3 Delete = new Delete (Bytes. toBytes ("Row key"): 4 delete. deleteColumns (Bytes. toBytes ("column family"), Bytes. toBytes ("column name"); 5 table. delete (delete); 6 assertThat (table. get (get ). list, nullValue); 7 // delete all columns 8 table. delete (delete); 9 assertThat (table. getask( scan ). next), nullValue (); 10 // Delete the entire table 11 admin. disableTable ("table name"); 12 admin. delete ("table name"); 13 assertThat (admin. tableExists ("table name"), is (false ));View Code

 

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.