Hbase的Shell操作
写在前面
因为格式问题,为了显示美观一点笔者在Shell命令中逗号后面添加了空格.实际使用请不要加空格
说明
名字 | 意义 |
tname | TableName表名 |
rk | Row_Key行键 |
cf | Column_Family列族 |
c | Column列 |
regex | 正则表达式 |
#使用如下命令进入Hbase 的shell 客户端,输入quit或exit退出
$ hbase shell
$ help #查看hbase 所有命令
如果忘记了命令如何使用,使用help ‘命令’查看帮助文档,如下
hbase(main):048:0> help 'list'
List all tables in hbase. Optional regular expression parameter could
be used to filter the output. Examples:
hbase> list
hbase> list 'abc.*'
hbase> list 'ns:abc.*'
hbase> list 'ns:.*'
1.常用命令操作
Shell | 作用 |
查看服务器状态 | status |
查看hbase 版本 | version |
查看当前用户 | whoami |
表引用命令提供帮助 | table_help |
查看服务器状态
hbase(main):002:0> status
1 active master, 1 backup masters, 3 servers, 0 dead, 1.0000 average load
查看hbase 版本
hbase(main):003:0> version
1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017
查看当前用户
hbase(main):004:0> whoami
hadoop (auth:SIMPLE)
groups: hadoop, wheel
表引用命令提供帮助
DDL操作(数据定义语言)
作用 | Shell |
创建表 | create ‘tname’, ‘cf1’,’cf2’,’cf…’ |
查看表结构 | desc ‘tname’ or describe ‘tname’ |
判断表是否存在 | exists ‘tname’ |
判断是否禁用启用表 | is_enabled ‘tname’; is_disabled ‘tname’ |
禁用表 | disable ‘tname’ |
启用表 | enable ‘tname’ |
查看所有表 | list |
删除列族 | alter ‘tname’,’delete’=>’cf’ |
新增列族 | alter ‘tname’,NAME=>’cf’ |
删除单个表 先禁用表, 再删除表 | 第一步disable ‘tname’,第二步 drop ‘tname’ |
批量删除表 | drop_all ‘regex’ |
创建表
hbase(main):008:0> create 'students','info','address'
0 row(s) in 10.5040 seconds
=> Hbase::Table - students
查看表结构
hbase(main):029:0> desc 'students'
Table students is ENABLED
students
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VER
SIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIO
NS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.2450 seconds
hbase(main):030:0> describe 'students'
Table students is ENABLED
students
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VER
SIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIO
NS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.2470 seconds
判断表是否存在(exit)
hbase(main):011:0> exists 'students'
Table students does exist
0 row(s) in 0.0830 seconds
判断是否禁用启用表(is_enabled,is_disabled)
#is_enabled 是否启用、is_disabled 是否禁用
hbase(main):012:0> is_enabled 'students'
true
0 row(s) in 0.0690 seconds
hbase(main):013:0> is_disabled 'students'
false
0 row(s) in 0.0860 seconds
禁用表(disable)
hbase(main):016:0> disable 'students'
0 row(s) in 2.6340 seconds
hbase(main):017:0> is_disabled 'students'
true
0 row(s) in 0.0520 seconds
启用表(enable)
hbase(main):018:0> enable 'students'
0 row(s) in 2.5390 seconds
hbase(main):019:0> is_enabled 'students'
true
0 row(s) in 0.0860 seconds
查看所有表(list)
hbase(main):020:0> list
TABLE
students
user
2 row(s) in 0.0400 seconds
=> ["students", "user"]
删除列族(alter)
#删除students表中的列族 address
hbase(main):024:0> alter 'students','delete'=>'address'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 4.0260 seconds
新增列族(alter)
#students 表中新增列族address
hbase(main):027:0> alter 'students',NAME=>'address'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 3.6260 seconds
删除表(drop,drop_all)
#注意:删除前必须先disable表,然后再使用drop删除
#删除单个表使用drop,删除students表
hbase(main):031:0> disable 'students'
0 row(s) in 2.3640 seconds
hbase(main):032:0> drop 'students'
0 row(s) in 2.5820 seconds
hbase(main):033:0> exists 'students'
Table students does not exist
0 row(s) in 0.0850 seconds
#批量删除表使用drop_all,使用正则匹配,删除前先disable表,例如有如下表,删除所有以stu开头的表
hbase(main):038:0> list
TABLE
stu
students1
students2
user
3 row(s) in 0.0570 seconds
=> ["stu", "students1", "students2"]
hbase(main):041:0> disable_all 'stu.*'
stu
students1
students2
Disable the above 3 tables (y/n)?
y
3 tables successfully disabled
DML (数据操作语言)
作用 | Shell |
插入数据 | put ‘tname’,’rk’,’cf:c’,’value’ |
获取某个列族 | get ‘tname’,’rk’, ‘cf’ |
获取某个列族的某个列 | get ‘tname’, ‘rk’, ‘cf : c’ |
全表扫描 | scan ‘tname’ |
查询表历史记录 | scan ‘tname’,{RAW => true,VERSION => 10} |
删除记录 | delete ‘tname’ ,’rk’, ‘cf : c’ |
删除整行 | deleteall ‘tname’, ‘rk’ |
清空表 | truncate ‘tname’ |
查看表中的记录总数 | count ‘tname’ |
插入数据(put)
hbase(main):003:0> put 'students','1001','info:name','zhangsan'
0 row(s) in 0.9800 seconds
hbase(main):006:0> put 'students','1001','info:sex','0'
0 row(s) in 0.0520 seconds
hbase(main):006:0> put 'students','1001','address:province','Henan'
0 row(s) in 0.1540 seconds
hbase(main):005:0> put 'students','1001','address:city','BeiJing'
0 row(s) in 0.3730 seconds
hbase(main):018:0> put 'students','1002','info:name','wangwu'
0 row(s) in 0.0690 seconds
hbase(main):019:0> put 'students','1003','info:sex','1'
0 row(s) in 0.0640 seconds
更新数据(put)
#更新行健为1001,列族为info,列为name的学生姓名为lisi
hbase(main):009:0> put 'students','1001','info:name','lisi'
0 row(s) in 0.1040 seconds
#更新行健为1001,列族为address,列为province的学生省份为BeiJing
hbase(main):011:0> put 'students','1001','address:province','BeiJing'
0 row(s) in 0.0650 seconds
查询数据(get、scan)
根据rowkey获取:get
全表扫描:scan
- 获取行健为1001的学生信息
hbase(main):025:0> get 'students','1001'
COLUMN CELL
address:city timestamp=1502172494982, value=BeiJing
address:province timestamp=1502172919511, value=Hebei
info:name timestamp=1502172821032, value=lisi
info:sex timestamp=1502171941941, value=0
4 row(s) in 0.3110 seconds
- 获取行健为1001且列族为address的学生信息
hbase(main):026:0> get 'students','1001','address'
COLUMN CELL
address:city timestamp=1502172494982, value=BeiJing
address:province timestamp=1502172919511, value=Hebei
2 row(s) in 0.0380 seconds
- 获取行健为1001、列族为address、列为ciry的学生信息
hbase(main):027:0> get 'students','1001','address:city'
COLUMN CELL
address:city timestamp=1502172494982, value=BeiJing
1 row(s) in 0.1150 seconds
- 获取所有的学生信息
hbase(main):028:0> scan 'students'
ROW COLUMN+CELL
1001 column=address:city, timestamp=1502172494982, value=BeiJing
1001 column=address:province, timestamp=1502172919511, value=Hebei
1001 column=info:name, timestamp=1502172821032, value=lisi
1001 column=info:sex, timestamp=1502171941941, value=0
1002 column=info:name, timestamp=1502173540238, value=wangwu
1003 column=info:sex, timestamp=1502173566515, value=1
3 row(s) in 0.1370 seconds
删除数据
#删除列族中的某个列
#删除students表行健1001,列族为address,列为city的数据
hbase(main):036:0> delete 'students','1001','address:city'
0 row(s) in 0.1750 seconds
删除前:
删除后:删除了列族中列city为BeiJing的数据
#删除整行数据
hbase(main):049:0> deleteall 'students','1002'
0 row(s) in 0.0510 seconds
使用scan 命令可以查看到students的历史记录,可以看到已被删除的列族,修改前的数据
scan 'students',{RAW => true,VERSION => 10}
清空表中所有数据
hbase(main):010:0> truncate 'students'
Truncating 'students' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 5.5080 seconds
hbase(main):012:0> scan 'students'
ROW COLUMN+CELL
0 row(s) in 0.2060 seconds
查看表中的总记录数(count)
hbase(main):001:0> count 'students'
2 row(s) in 1.7030 seconds
=> 2
hbase(main):002:0> scan 'students'
ROW COLUMN+CELL
1001 column=address:province, timestamp=1502172919511, value=Hebei
1001 column=info:name, timestamp=1502172821032, value=lisi
1001 column=info:sex, timestamp=1502171941941, value=0
1003 column=info:sex, timestamp=1502173566515, value=1
2 row(s) in 0.3620 seconds