Hbase(四)MR on Hbase & Hbase的 架构
三、Shell 操作
3.1 常见命令
status, table_help, version, whoami
hbase(main):002:0> status
1 active master, 0 backup masters, 1 servers, 0 dead, 2.0000 average load
hbase(main):003:0> version
1.2.4, rUnknown, Wed Feb 15 18:58:00 CST 2017
hbase(main):004:0> whoami
root (auth:SIMPLE)
groups: root
3.2 命名空间操作
alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables
创建命名空间
hbase(main):005:0> create_namespace 'zpark',{'user'=>'zhangsan'}
0 row(s) in 0.0560 seconds
# 需要注意的指定值为XXX 不能用等于好 而需要用‘=>’
hbase(main):006:0> list_namespace
list_namespace list_namespace_tables
hbase(main):006:0> list_namespace
NAMESPACE
default
hbase
zpark
3 row(s) in 0.0470 seconds
描述命名空间
hbase(main):008:0> describe_namespace 'zpark'
DESCRIPTION
{NAME => 'zpark', user => 'zhangsan'}
1 row(s) in 0.0450 seconds
修改命名空间
hbase(main):009:0> alter_namespace 'zpark',{METHOD => 'set' ,'user'=>'rechen'}
0 row(s) in 0.0460 seconds
hbase(main):010:0> describe_namespace 'zpark'
DESCRIPTION
{NAME => 'zpark', user => 'rechen'}
1 row(s) in 0.0020 seconds
删除命名空间 属性
hbase(main):012:0> alter_namespace 'zpark',{METHOD => 'unset' ,NAME=>'user'}
0 row(s) in 0.0450 seconds
hbase(main):013:0> describe_namespace 'zpark'
DESCRIPTION
{NAME => 'zpark'}
1 row(s) in 0.0090 seconds
显示所有命名空间
hbase(main):015:0> list_namespace
NAMESPACE
default
hbase
zpark
3 row(s) in 0.0270 seconds
删除命名空间
hbase(main):016:0> drop_namespace
ERROR: wrong number of arguments (0 for 1)
Here is some help for this command:
Drop the named namespace. The namespace must be empty. # 命名空间必须为空
hbase(main):017:0> drop_namespace 'zpark'
0 row(s) in 0.0480 seconds
显示某个命名空间下的表
hbase(main):003:0> list_namespace_tables 'csdn'
TABLE
t_user
1 row(s) in 0.0330 seconds
3.3 DDL 数据定义语言
对命名空间(数据库)的中表进行操作
创建表
hbase(main):004:0> create 'csdn:t_user','cf1','cf2'
hbase(main):003:0> list_namespace_tables 'csdn'
TABLE
t_user
1 row(s) in 0.0330 seconds
查看表详情
hbase(main):004:0> describe 'csdn:t_user'
Table baizhi:t_user is ENABLED
csdn:t_user
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODIN
G => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICA
TION_SCOPE => '0'}
{NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODIN
G => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICA
TION_SCOPE => '0'}
2 row(s) in 0.1400 seconds
删除表
注意在删除前 需要先将 表 disable 掉
hbase(main):001:0> drop 'csdn:t_user'
ERROR: Table csdn:t_user is enabled. Disable it first.
Here is some help for this command:
Drop the named table. Table must first be disabled:
hbase> drop 't1'
hbase> drop 'ns1:t1'
hbase(main):002:0> disable 'csdn:t_user'
0 row(s) in 2.5940 seconds
hbase(main):003:0> drop 'csdni:t_user'
0 row(s) in 1.5920 seconds
显示所有表
hbase(main):004:0> list
TABLE
0 row(s) in 0.0190 seconds
3.4 数据的CURD DML(数据管理语言)
插入(put)
# 插入一条数据 在baizhi:t_user 行健为1 列簇cf1 字段名为name 值为zs
hbase(main):012:0> put 'csdn:t_user',1,'cf1:name','zs'
0 row(s) in 0.1420 seconds
hbase(main):001:0> t = get_table 'csdn:t_user' # 做表的引用
0 row(s) in 0.0330 seconds
=> Hbase::Table - baizhi:t_user
# 插入一条数据 在baizhi:t_user 行健为1 列簇cf1 字段名为sex 值为true
hbase(main):002:0> t.put 1,'cf1:sex','true'
0 row(s) in 0.3510 seconds
# 插入一条数据 在baizhi:t_user 行健为1 列簇cf1 字段名为age 值为18
hbase(main):003:0> t.put 1,'cf1:age',18
0 row(s) in 0.0210 seconds
hbase(main):004:0> t.get 1
COLUMN CELL
cf1:age timestamp=1572888118460, value=18
cf1:name timestamp=1572887983788, value=zs
cf1:sex timestamp=1572888095574, value=true
3 row(s) in 0.0500 seconds
更新
# 创建一个可以多个版本的表
hbase(main):005:0> create 'csdn:t_user',{NAME=>'cf1',VERSIONS=>3},{NAME=>'cf2',VERSIONS=>3}
0 row(s) in 1.2410 seconds
=> Hbase::Table - csdn:t_user
# 插入基础的数据
hbase(main):012:0> t.put 1,'cf1:name','zs'
0 row(s) in 0.0870 seconds
hbase(main):013:0> t.put 1,'cf1:age',18
0 row(s) in 0.0220 seconds
hbase(main):014:0> t.put 1,'cf1:sex',true
0 row(s) in 0.0270 seconds
# 进行数据的更新
hbase(main):015:0> t.put 1,'cf1:name','zhangsan'
0 row(s) in 0.0080 seconds
# 可以看到数据已经变为其他数据
hbase(main):016:0> t.get 1
COLUMN CELL
cf1:age timestamp=1572888599018, value=18
cf1:name timestamp=1572888630027, value=zhangsan
cf1:sex timestamp=1572888605427, value=true
3 row(s) in 0.0350 seconds
取值(get)
# 获取所有rowkey 为1 列簇为cf1 列名为name 最多获取三个版本的数据
hbase(main):017:0> t.get 1 ,{COLUMNS=>'cf1:name',VERSIONS=>3}
COLUMN CELL
cf1:name timestamp=1572888630027, value=zhangsan
cf1:name timestamp=1572888590148, value=zs
2 row(s) in 0.0300 seconds
# 根据某个时间戳进行获取
hbase(main):020:0> t.get 1 ,{COLUMNS=>'cf1:name',TIMESTAMP => 1572888590148}
COLUMN CELL
cf1:name timestamp=1572888590148, value=zs
1 row(s) in 0.0170 seconds
# 根据时间戳区间进行获取
hbase(main):024:0> t.get 1 ,{COLUMNS=>'cf1:name',TIMERANGE => [157288850147,1572888630030],VERSIONS => 4}
COLUMN CELL
cf1:name timestamp=1572888630027, value=zhangsan
cf1:name timestamp=1572888590148, value=zs
2 row(s) in 0.0160 seconds
删除(delete/deleteall)
# 直接进行删除
hbase(main):025:0> delete 'csdn:t_user',1,'cf1:name'
0 row(s) in 0.0560 seconds
# 引用删除
hbase(main):027:0> t.delete 1,'cf1:sex'
0 row(s) in 0.0290 seconds
# 删除某个id下的所有数据
hbase(main):029:0> t.deleteall 1
0 row(s) in 0.0150 seconds
# 删除某个id某个列簇某个字段所有版本的值
hbase(main):043:0> t.deleteall 1 ,'cf1:name'
0 row(s) in 0.0150 seconds
全表扫描
hbase(main):049:0> t.scan
ROW COLUMN+CELL
1 column=cf1:age, timestamp=1572889604496, value=18
1 column=cf1:name, timestamp=1572889601629, value=zs
1 column=cf1:sex, timestamp=1572889608032, value=true
1 row(s) in 0.0240 seconds
计数(count)
hbase(main):050:0> t.count
1 row(s) in 0.0590 seconds
=> 1
追加(append)
hbase(main):051:0> t.append 1,'cf1:name','123'
0 row(s) in 0.0220 seconds
hbase(main):052:0> t.scan
ROW COLUMN+CELL
1 column=cf1:age, timestamp=1572889604496, value=18
1 column=cf1:name, timestamp=1572889732899, value=zs123
1 column=cf1:sex, timestamp=1572889608032, value=true
1 row(s) in 0.0240 seconds
清空数据
hbase(main):053:0> truncate 'csdn:t_user'
Truncating 'csdn:t_user' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 3.9640 seconds
hbase(main):054:0> t.scan
ROW COLUMN+CELL
0 row(s) in 0.1440 seconds
无文案,献给每一个正在努力的我们,就算在忙,也要注意休息和饮食哦!我就是我,一个在互联网跌跌撞撞,摸爬滚打的热忱,给个三连吧~