HBase Shell是使用(J)Ruby的IRB实现的命令行脚本。
因此可以在HBase Shell中混合使用Ruby代码:
hbase(main):011:0> create'testtable','colfam'
hbase(main):011:0> for i in 'a'..'z' do
hbase(main):012:1* for j in 'a'..'z' do
hbase(main):013:2* put'testtable',"row-#{i}#{j}","colfam:#{j}","#{j}"
1) Shell命令帮助
hbase(main):025:0> help
COMMAND GROUPS:
Group name: general
Commands: status, table_help, version, whoami
………………….
会出现很多命令组,然后也可以查看每个命令组下面命令的帮助
hbase(main):025:0> help "status"
Show cluster status. Can be 'summary','simple', 'detailed', or 'replication'. The
default is 'summary'. Examples:
hbase> status
hbase> status 'simple'
hbase> status 'summary'
hbase> status 'detailed'
hbase> status 'replication'
hbase> status 'replication', 'source'
hbase> status 'replication', 'sink'
执行命令:
hbase(main):026:0> whoami
hadoop (auth:SIMPLE)
groups: hadoop
2) HBase Shell客户端,退格键无法删除命令的问题
ctrl+返回键即可删除
3) 查看数据库状态
hbase(main):061:0> status 'simple'
2 live servers
gpseg:16020 1434249190233
requestsPerSecond=0.0, numberOfOnlineRegions=3, usedHeapMB=15,maxHeapMB=193, numberOfStores=3, numberOfStorefiles=2,storefileUncompressedSizeMB=0, storefileSizeMB=0, memstoreSizeMB=0,storefileIndexSizeMB=0, readRequestsCount=87, writeRequestsCount=8,rootIndexSizeKB=0, totalStaticIndexSizeKB=0, totalStaticBloomSizeKB=0,totalCompactingKVs=0, currentCompactedKVs=0, compactionProgressPct=NaN,coprocessors=[]
gpmaster:16202 1434249598161
requestsPerSecond=0.0, numberOfOnlineRegions=2, usedHeapMB=13,maxHeapMB=193, numberOfStores=2, numberOfStorefiles=0,storefileUncompressedSizeMB=0, storefileSizeMB=0, memstoreSizeMB=0,storefileIndexSizeMB=0, readRequestsCount=0, writeRequestsCount=0,rootIndexSizeKB=0, totalStaticIndexSizeKB=0, totalStaticBloomSizeKB=0,totalCompactingKVs=0, currentCompactedKVs=0, compactionProgressPct=NaN,coprocessors=[]
0 dead servers
Aggregate load: 0, regions: 5
hbase(main):062:0> status 'summary'
2 servers, 0 dead, 2.5000 average load
hbase(main):063:0> status 'detailed' -----更加详细的信息,此处略
4) 表操作
hbase(main):001:0> version
hbase(main):001:0> create 'member','member_id','address','info'
注释:member指表名,后面三个为列族,系统会自动分配region
list
describe tablename
hbase(main):010:0> list
TABLE
member
student
t1
3 row(s) in 0.0450 seconds
=> ["member","student", "t1"]
hbase(main):011:0> describe 'member'
Table member is ENABLED
member
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', DATA_BLOCK_ENCODING=> 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS=> '1', COMPR
ESSION => 'NONE', MIN_VERSIONS =>'0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE =>'65536', IN_MEMORY =
> 'false', BLOCKCACHE =>'true'}
{NAME => 'info', DATA_BLOCK_ENCODING=> 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS=> '1', COMPRESS
ION => 'NONE', MIN_VERSIONS => '0',TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536',IN_MEMORY => '
false', BLOCKCACHE => 'true'}
{NAME => 'member_id',DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE=> '0', VERSIONS => '1', COM
PRESSION => 'NONE', MIN_VERSIONS =>'0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE =>'65536', IN_MEMORY
=> 'false', BLOCKCACHE => 'true'}
3 row(s) in 0.0710 seconds
注释:上面创建表时没有指定时间戳,那么将由系统自动生成
5) 删除列族:alter、disable、enable命令
错误的方式:
hbase(main):016:0> alter 'member',NAME=> 'member_id',METHOD => 'delete'
Updating all regions with the newschema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.2130 seconds
正确的方式:修改表结构之前先disable
hbase(main):023:0> disable 'member'
0 row(s) in 1.2830 seconds
hbase(main):024:0> alter 'member',NAME => 'member_id',METHOD=> 'delete'
Updating all regions with the newschema...
1/1 regions updated.
Done.
hbase(main):025:0> enable 'member'
0 row(s) in 0.4320 seconds
hbase(main):026:0> describe 'member'
Table member is ENABLED
member
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', DATA_BLOCK_ENCODING=> 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS=> '1', COMPR
ESSION => 'NONE', MIN_VERSIONS =>'0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE =>'65536', IN_MEMORY =
> 'false', BLOCKCACHE =>'true'}
{NAME => 'info', DATA_BLOCK_ENCODING=> 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS=> '1', COMPRESS
ION => 'NONE', MIN_VERSIONS => '0',TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536',IN_MEMORY => '
false', BLOCKCACHE => 'true'}
2 row(s) in 0.0300 seconds
可以看到只有两个列族了。
6) 表操作
a) 删除表
hbase(main):027:0>create 'temp_table','table'
0 row(s) in 0.4420 seconds
=> Hbase::Table - temp_table
hbase(main):029:0> disable 'temp_table'
0 row(s) in 1.2190 seconds
hbase(main):030:0> drop 'temp_table'
0 row(s) in 0.1890seconds
b) 查看表是否存在
hbase(main):035:0> exists 'member'
Table member does exist
0 row(s) in 0.0120seconds
c) 判断表是否是enable或disable
hbase(main):044:0> is_enabled('member')
true
0 row(s) in 0.0190 seconds
hbase(main):046:0> is_disabled('member')
false
0 row(s) in 0.0220seconds
d) 插入记录
hbase(main):049:0>put'member','xueba','info:age','25'
注释:member指表名,xueba指行键,作为查找数据的键
info:age指列族info中的一个列,值value为25
put'member','xueba','info:birthday','1989-06-19'
put'member','xueba','info:company','tecent'
put'member','xueba','address:country','china'
put'member','xueba','address:province','guangdong'
put'member','xueba','address:city','shenzhen'
上面其实就一行数据,行键为xueba
下面再插入一行,行键为xiaoming:
put 'member','xiaoming','info:age','24'
put 'member','xiaoming','info:birthday','1990-03-22'
put'member','xiaoming','info:company','tecent'
put'member','xiaoming','info:favorite','movie'
put'member','xiaoming','address:country','china'
put'member','xiaoming','address:province','guangdong'
put 'member','xiaoming','address:city','guangzhou'
e) 获取数据
hbase(main):065:0>get'member','xueba'
COLUMN CELL
address:city timestamp=1434253878145, value=shenzhen
address:country timestamp=1434253848370,value=china
address:province timestamp=1434253860038,value=guangdong
info:age timestamp=1434253655367, value=25
info:birthday timestamp=1434253816609, value=1989-06-19
info:company timestamp=1434253830724, value=tecent
6 row(s) in0.1420 seconds
hbase(main):066:0>get'member','xiaoming','info'
COLUMN CELL
info:age timestamp=1434253975698,value=24
info:birthday timestamp=1434253997664,value=1990-03-22
info:company timestamp=1434254011707,value=tecent
info:favorite timestamp=1434254024140,value=movie
4 row(s) in0.0700 seconds
hbase(main):067:0>get'member','xiaoming','info:age'
COLUMN CELL
info:age timestamp=1434253975698,value=24
1 row(s) in0.0440 seconds
f) 更新记录
命令put
hbase(main):067:0>get'member','xiaoming','info:age'
COLUMN CELL
info:age timestamp=1434253975698,value=24
1 row(s) in0.0440 seconds
hbase(main):068:0>put'member','xueba','info:age','26'
0 row(s) in0.1000 seconds
hbase(main):069:0>get 'member','xueba','info:age'
COLUMN CELL
info:age timestamp=1434254477087,value=26
1 row(s) in0.0410 seconds
上面get查看的是新的数据,如果想查看老的数据,执行:
通过timestamp来获取数据
hbase(main):070:0>get'member','xueba',{COLUMN => 'info:age', TIMESTAMP => 1434253655367}
COLUMN CELL
info:age timestamp=1434253655367,value=25
1 row(s) in 0.0450seconds
g) 全表扫描
hbase(main):071:0>scan 'member'
ROW COLUMN+CELL
xiaoming column=address:city, timestamp=1434254055056,value=guangzhou
xiaoming column=address:country,timestamp=1434254034436, value=china
xiaoming column=address:province,timestamp=1434254044916, value=guangdong
xiaoming column=info:age,timestamp=1434253975698, value=24
xiaoming column=info:birthday,timestamp=1434253997664, value=1990-03-22
xiaoming column=info:company,timestamp=1434254011707, value=tecent
xiaoming column=info:favorite,timestamp=1434254024140, value=movie
xueba column=address:city,timestamp=1434253878145, value=shenzhen
xueba column=address:country, timestamp=1434253848370, value=china
xueba column=address:province,timestamp=1434253860038, value=guangdong
xueba column=info:age,timestamp=1434254477087, value=26
xueba column=info:birthday,timestamp=1434253816609, value=1989-06-19
xueba column=info:company,timestamp=1434253830724, value=tecent
2 row(s) in 0.2120seconds
上面其实只有两行。
h) 删除数据
命令:delete
hbase(main):072:0>delete'member','xueba','info:age'
0 row(s) in0.0700 seconds
hbase(main):073:0> get'member','xueba'
COLUMN CELL
address:city timestamp=1434253878145,value=shenzhen
address:country timestamp=1434253848370,value=china
address:province timestamp=1434253860038,value=guangdong
info:birthday timestamp=1434253816609,value=1989-06-19
info:company timestamp=1434253830724, value=tecent
5 row(s) in0.0520 seconds
删除整行:deleteall
hbase(main):074:0>deleteall'member','xiaoming'
0 row(s) in0.0120 seconds
hbase(main):075:0>get'member','xiaoming'
COLUMN CELL
0 row(s) in 0.0130 seconds
查询表中还有多少行:count(每个行键对应一行)
hbase(main):076:0> count 'member'
1 row(s) in 0.0520 seconds
=> 1
截断表:truncate
hbase(main):077:0> truncate 'member'
Truncating 'member' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 1.8140 seconds
可以看到truncate先disable然后truncate(先删除表(hdfs都删除了)再创建表)操作