Manipulating the HBase database in the shell command line
Shell control
Enter the shell command line interface, execute the hbase command, and attach the SHELL keyword:
[Grid@hdnode3 ~]$ hbase shell
hbase shell; enter¨help¨for list of supported.
Type "Exit" to leave the HBase Shell
Version 0.90.5, r1212209, Fri Dec 9 05:40:36 UTC HBase
(main):001:0>
Although successfully logged in, but we do not know what to do now, do not know what the shell under the command. At this point, we can choose to look at the instructions in the official document, or to look at the help.
HBase (main):002:0> help
...
....... ..................
COMMAND GROUPS:
group name:general
commands:status, version
group NAME:DDL
Commands:alter, create, Describe, disable, drop, enable, exists, is_disabled, is_enabled, list
Group name:dml
commands:count, delete, de Leteall, GET, Get_counter, incr, put, scan, truncate
Group name:tools
commands:assign, Balance_switch, Balanc Er, close_region, compact, flush, major_compact, move, split, Unassign, Zk_dump
Group name:replication
Commands:add_peer, Disable_peer, Enable_peer, Remove_peer, start_replication, Stop_replication
..................
..................
The help information really helps, and by outputting the information we have a general idea of what we can do. You can see that hbase is also divided into ddl/dml such statements, in addition to replication-related, management-related commands, and so on.
First try the General Command, query status:
HBase (main):003:0> status
5 servers, 0 dead, 0.4000 average load
Query version:
HBase (main):004:0> version
0.90.5, r1212209, Fri Dec 9 05:40:36 UTC 2011
Next, the key items, DDL and DML (unexpected hbase also DML/DDL statements). There is no concept of a library in HBase, as the bigtable of the cottage products, although no Name cottage to the name, but the essence of the cottage, from the design, it does not need to be a library, and even do not need to be a table, all the data in the same table can be, this is the real bigtable well.
To create a Table object:
HBase (main):005:0> Create¨t¨,¨t_id¨,¨t_vl¨
0 Row (s) in 2.3490 seconds
The syntax for creating objects in HBase is more flexible, as the preceding example is simply written with the function equivalent to the complete writing, "Hbase> Create¨t¨, {name =>¨t_id¨}, {name =>¨t_vl¨}", and the first parameter is used to specify the table name. All subsequent arguments are the names of the column families. The column family of each table needs to be defined when the table is created (although it can be modified later, but preferably defined at the outset), from this perspective, the objects in the hbase are structured.
To view a Table object:
HBase (main):006:0> list TABLE T 1 row (s) in 0.0080 seconds HBA
SE (main):018:0> describe¨t¨description ENABLED {Name =>¨t¨, families => [{name =>¨t_id¨, Bloomfilter =>¨none¨, Replication_scope =>¨0¨, COMPRE Ssion => True¨none¨, versions =>¨3¨, TTL =>¨2147483647¨, BLOCKSIZE =>¨65536 ¨, In_memory =>¨false¨, Blockcache =>¨t Rue¨}, {NAME =>¨t_vl¨, Bloomfilter =&G T ¨none¨, Replication_scope =>¨0¨, COMPRESSION =>¨none¨, versions =>¨3¨, TTL =& Gt
¨2147483647¨, BLOCKSIZE =>¨65536¨, In_memory =>¨false¨, Blockcache =>¨true¨}]} 1 row (s) In 0.0100 seconds
The output format is also in the form of a JSON string from which you can see the number of versions retained, the TTL (time to Live, retention), the definition of the column, the block size, and so on.
To modify a Table object, you must first disable the object before you modify it (including the deletion), and then enable the object after the execution of the Modify command succeeds.
To disable an object:
HBase (main):004:0> Disable¨t¨
0 Row (s) in 2.0430 seconds
To determine whether the current Table object is enabled or disabled:
HBase (main):007:0> Is_enabled¨t¨
false
0 row (s) in 0.0040 seconds HBase
(main):008:0> is_disabled¨t ¨
true
0 row (s) in 0.0040 seconds
To modify a Table object, add a column family:
HBase (Main):021:0> Alter¨t¨, {NAME =>¨t_info¨, versions => 3}
0 row (s) in 0.0360 seconds HBase
(main): 02 3:0> Enable¨t¨
0 Row (s) in 2.0250 seconds
Insert Record:
HBase (main):025:0> Put¨t¨,¨10001¨,¨t_vl:name¨,¨jss¨
0 Row (s) in 0.0060 seconds HBase
(main):026:0> Put¨ T¨,¨10001¨,¨t_vl:age¨,¨99¨
0 Row (s) in 0.0070 seconds
hbase (main):027:0> Put¨t¨,¨10001¨,¨t_info:general¨ , ¨his fullname is Junsanis!¨
0 row (s) in 0.0040 seconds
Record acquisition:
HBase (main):028:0> Get¨t¨,¨10001¨
COLUMN CELL
t_info:general timestamp=1365670813664, value= His fullname is junsanis!
T_vl:age timestamp=1365670733223, value=99
t_vl:name timestamp=1365670723056, VALUE=JSS
3 row (s) In 0.0450 seconds
Gets the data for the specified column family in the specified record:
HBase (main):029:0> Get¨t¨,¨10001¨,¨t_vl¨
COLUMN CELL
t_vl:age timestamp=1365670733223, value=
t_vl:name timestamp=1365670723056, VALUE=JSS
2 row (s) in 0.0070 seconds
Gets the data for the specified column in the specified column family in the specified record:
HBase (main):030:0> Get¨t¨,¨10001¨,¨t_vl:age¨
COLUMN CELL
t_vl:age timestamp= 1365670733223, value=99
1 row (s) in 0.0070 seconds
Record update (no difference to insert):
HBase (main):031:0> Put¨t¨,¨10001¨,¨t_vl:age¨,¨10¨
0 Row (s) in 0.0050 seconds HBase
(main):032:0> Get¨t¨ , ¨10001¨,¨t_vl:age¨
COLUMN CELL
t_vl:age timestamp=1365670912700, value=10
1 row (s) in 0.0080 Seconds
Full table Scan:
HBase (main):033:0> Scan¨t¨
ROW column+cell
10001 column=t_info:general , timestamp= 1365670813664, Value=his fullname is junsanis!
10001 column=t_vl:age, timestamp=1365670912700, value=10
10001 column=t_vl:name , timestamp= 1365670723056, VALUE=JSS
1 row (s) in 0.0370 seconds
The entire table describes a column:
:036:0> Scan¨t¨, {COLUMNS =>¨t_vl¨} ROW column+cell 10001 column=t_vl:age , hbase (main) timestamp=1365670912700, value=10
10001 column=t_vl:name, timestamp=1365670723056, VALUE=JSS
1 row (s) In 0.0080 seconds
To delete a record line:
HBase (main):043:0> Delete¨t¨,¨10001¨,¨t_vl:age¨
0 Row (s) in 0.0050 seconds HBase
(main):045:0> Get¨t¨,¨ 10001¨
COLUMN CELL
t_info:general timestamp=1365670813664, Value=his fullname is junsanis!
T_vl:name timestamp=1365670723056, VALUE=JSS
2 row (s) in 0.0070 seconds
To delete a table:
HBase (main):047:0> Disable¨t¨
0 Row (s) in 2.0230 seconds HBase
(main):048:0> Drop¨t¨
0 Row (s) in 1. 1170 seconds
After reading the previous example, do you have any questions or thoughts? I have a question mark in my mind: there is no update operation in the HBase, only inserts, but each time we put the new record to replace the old version, how to save a lot of records? Is there only one record in the columns of each row key? It's not science! This is obviously not the people look forward to and loved performance.
The problem, in fact, is that the column value save version (versions) or retention time (TTL, timing to Liv) is at work.
For example, we want to count the most recent (n) browsing records for a user, so create a HBase Table object as follows:
Hbase> create¨rlog¨,¨userid¨,{name=>¨article¨,versions=>100}
Currently set, keep the last 100 versions. When the user browses the post, insert a record into the Rlog table as follows:
hbase> Put¨rlog¨, $userid, ¨article:id¨, $aid
Here only select the record browsing User ID and browse the page ID, also can according to the actual situation, saves the page the URL address, the article title and so on information. HBase table column Family is unstructured, we can arbitrarily increase the value of the column according to the requirements.
So, to get the user's recent browsing records, how should we check? , for example, to get the most recently browsed 10 entries:
Hbase> Get¨rlog¨, $userid, {Column=>¨article:id¨, versions=>10}
In addition to through the versions control, you can also consider the version of the Save time TTL to control, the TTL unit is seconds, the default is generally to save 30 days.
Thank you for reading, I hope to help you, thank you for your support for this site!