文章目录
简单整理了下HBase Shell的常见命令。
version
查看HBase版本。
1 2 |
hbase(main):001:0> version 1.2.2, r3f671c1ead70d249ea4598f1bbcc5151322b3a13, Fri Jul 1 08:28:55 CDT 2016 |
status
查看HBase集群的状态。
1 2 |
hbase(main):002:0> status 1 active master, 0 backup masters, 1 servers, 0 dead, 3.0000 average load |
create
创建表。建表时需要指定表名及列族的名称。
1 2 3 4 |
hbase(main):003:0> create 'test', 'cf' 0 row(s) in 2.8040 seconds => Hbase::Table - test |
list
列出HBase中创建的所有表的信息。在当前的HBase中创建了两个表,分别是test和test2。
1 2 3 4 5 6 7 8 |
hbase(main):005:0> list TABLE m_domain test test2 3 row(s) in 0.0660 seconds => ["m_domain", "test", "test2"] |
describe
查看某个表的表结构。使用时表名需要用括号括起来。
1 2 3 4 5 6 7 |
hbase(main):008:0> describe 'test' Table test is ENABLED test COLUMN FAMILIES DESCRIPTION {NAME => 'cf', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 1 row(s) in 0.3940 seconds |
这个命令也可以使用简写的“desc”来代替。
disable和enable
用来启用禁用表。通常和表结构调整命令或者删除命令一起使用。
is_disabled和is_enabled
用来查看是否已经禁用或启用表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
hbase(main):002:0> is_enabled 'test2' true 0 row(s) in 0.3090 seconds hbase(main):003:0> is_disabled 'test2' false 0 row(s) in 0.0170 seconds hbase(main):004:0> disable 'test2' 0 row(s) in 2.5320 seconds hbase(main):005:0> is_disabled 'test2' true 0 row(s) in 0.0170 seconds hbase(main):006:0> enable 'test2' 0 row(s) in 2.4010 seconds hbase(main):007:0> is_enabled 'test2' true 0 row(s) in 0.0160 seconds |
put
向表里写数据,格式是:put ‘表名’, ‘行键’, ‘列族:限定符’, ‘值’。
1 2 3 4 5 6 7 8 |
hbase(main):009:0> put 'test', 'row1', 'cf:a', 'value1' 0 row(s) in 0.3030 seconds hbase(main):010:0> put 'test', 'row1', 'cf:b', 'value2' 0 row(s) in 0.0310 seconds hbase(main):011:0> put 'test', 'row1', 'cf:c', 'value3' 0 row(s) in 0.0370 seconds |
get
从表中取一行数据,格式是:get ‘表名’, ‘行键’。
1 2 3 4 5 6 7 8 9 10 11 |
hbase(main):015:0> get 'test', 'row1' COLUMN CELL cf:a timestamp=1477954590555, value=value1 cf:b timestamp=1477954620043, value=value2 cf:c timestamp=1477954635358, value=value3 3 row(s) in 0.0140 seconds hbase(main):016:0> get 'test', 'row2' COLUMN CELL cf:d timestamp=1477955145240, value=value1 1 row(s) in 0.0150 seconds |
结合上面的put命令,我们可以看出hbase数据更新的方式就是向表里put新的记录。
scan
扫描全表。查看表里的全部数据。
1 2 3 4 5 6 7 8 |
hbase(main):017:0> scan 'test' ROW COLUMN+CELL row1 column=cf:a, timestamp=1477954590555, value=value1 row1 column=cf:b, timestamp=1477954620043, value=value2 row1 column=cf:c, timestamp=1477954635358, value=value3 row2 column=cf:d, timestamp=1477955145240, value=value1 row3 column=cf:e, timestamp=1477955172939, value=value4 3 row(s) in 0.0710 seconds |
alter
用来调整表结构。
执行前先看一下目标表test2的结构:
1 2 3 4 5 6 7 |
hbase(main):007:0> desc 'test2' Table test2 is ENABLED test2 COLUMN FAMILIES DESCRIPTION {NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSION S => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 1 row(s) in 0.0370 seconds |
表中目前只有一个列族“cf2”,使用如下命令来新增列族“cf1”:
1 |
alter 'test2', NAME=>'cf1' |
执行结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
hbase(main):001:0> alter 'test2', NAME=>'cf1' Updating all regions with the new schema... 0/1 regions updated. 1/1 regions updated. Done. 0 row(s) in 4.0280 seconds hbase(main):002:0> desc 'test2' Table test2 is ENABLED test2 COLUMN FAMILIES DESCRIPTION {NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSION S => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} {NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSION S => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 2 row(s) in 0.0530 seconds |
使用如下命令来删除列族“cf2”:
1 |
alter 'test2', {METHOD=>'delete', NAME=>'cf2'} |
执行结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
hbase(main):003:0> alter 'test2', {METHOD=>'delete', NAME=>'cf2'} Updating all regions with the new schema... 0/1 regions updated. 1/1 regions updated. Done. 0 row(s) in 3.6280 seconds hbase(main):004:0> desc 'test2' Table test2 is ENABLED test2 COLUMN FAMILIES DESCRIPTION {NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSION S => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 1 row(s) in 0.0310 seconds |
使用如下命令改变列族“cf1”单元数目:
1 |
alter 'test2',{NAME=>'cf1', VERSIONS=>5} |
执行结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
hbase(main):008:0> alter 'test2',{NAME=>'cf1', VERSIONS=>5} Updating all regions with the new schema... 1/1 regions updated. Done. 0 row(s) in 2.5860 seconds hbase(main):009:0> desc 'test2' Table test2 is ENABLED test2 COLUMN FAMILIES DESCRIPTION {NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '5', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSION S => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 1 row(s) in 0.0330 seconds |
count
查看表中有多少条记录,格式是:count ‘表名’:
1 2 3 4 |
hbase(main):001:0> count 'test' 3 row(s) in 0.7260 seconds => 3 |
delete
删除指定行键和列族的命令。hbase的删除并不是立刻删除,而是将要删除的记录标记为可以删除,而后在下一次region合并分裂时将标记为删除的记录删除。
命令格式为:delete ‘表名’, ‘行键名’, ‘列族名’, [时间戳]。
执行结果:
1 2 3 4 5 6 7 8 9 10 11 12 |
hbase(main):008:0> delete 'test', 'row4', 'cf1' 0 row(s) in 0.0750 seconds hbase(main):009:0> scan 'test' ROW COLUMN+CELL row1 column=cf:a, timestamp=1477954590555, value=value1 row1 column=cf:b, timestamp=1477954620043, value=value2 row1 column=cf:c, timestamp=1477954635358, value=value3 row2 column=cf:d, timestamp=1477955145240, value=value1 row3 column=cf:e, timestamp=1477955172939, value=value4 row4 column=cf1:a, timestamp=1478819078975, value=value5 4 row(s) in 0.0470 seconds |
可以看到虽然执行了删除命令,但是记录没有被立即删除掉。
deleteall
删除整行数据的命令。
删除命令格式:deleteall ‘表名’, ‘行键名’。
执行示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
hbase(main):002:0> scan 'test' ROW COLUMN+CELL row1 column=cf:a, timestamp=1477954590555, value=value1 row1 column=cf:b, timestamp=1477954620043, value=value2 row1 column=cf:c, timestamp=1477954635358, value=value3 row2 column=cf:d, timestamp=1477955145240, value=value1 row3 column=cf:e, timestamp=1477955172939, value=value4 row4 column=cf1:a, timestamp=1478819078975, value=value5 4 row(s) in 1.3760 seconds hbase(main):003:0> deleteall 'test', 'row1' 0 row(s) in 0.2310 seconds hbase(main):004:0> scan 'test' ROW COLUMN+CELL row2 column=cf:d, timestamp=1477955145240, value=value1 row3 column=cf:e, timestamp=1477955172939, value=value4 row4 column=cf1:a, timestamp=1478819078975, value=value5 3 row(s) in 0.0310 seconds |
truncate
删除表中的全部记录。命令格式:truncate ‘表名’。
执行示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
hbase(main):007:0> count 'test' 3 row(s) in 0.1050 seconds => 3 hbase(main):008:0> truncate 'test' Truncating 'test' table (it may take a while): - Disabling table... - Truncating table... 0 row(s) in 7.3760 seconds hbase(main):009:0> count 'test' 0 row(s) in 0.1650 seconds => 0 |
在执行过程中看到truncate表前不需要执行disable命令,truncate命令在执行前就自动执行disable了。
exists
检查表是否存在。命令格式如下:exists ‘表名’。
执行示例如下:
1 2 3 |
hbase(main):011:0> exists 'test' Table test does exist 0 row(s) in 0.0250 seconds |
drop
删除表的命令。命令格式:drop ‘表名’。
drop前需要先disable。
执行实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
hbase(main):013:0> exists 'test' Table test does exist 0 row(s) in 0.0160 seconds hbase(main):014:0> disable 'test' 0 row(s) in 2.3370 seconds hbase(main):015:0> drop 'test' 0 row(s) in 2.3530 seconds hbase(main):016:0> exists 'test' Table test does not exist 0 row(s) in 0.0270 seconds |
更新(update)
hbase没有提供更新命令。hbase的更新是通过put新的数据覆盖旧的数据来完成的。
hbase是通过行键、列族、时间戳这三个维度来作为数据的唯一性的标准的。所以要更新数据只需要put一条这个维度都一致的数据即可。
参考文档
HBase的数据的update: http://punishzhou.iteye.com/blog/1266341
######################
发表评论