Hbase shell操作

1. 常见命令

hbase(main):001: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

2. 命名空间操作

# 创建一个普通的命名空间
hbase(main):006:0> create_namespace "baizhi"
0 row(s) in 0.0870 seconds

# 创建带属性的命名空间
hbase(main):007:0> create_namespace "shahe",{'subway'=>'shahezhan'}
0 row(s) in 0.0130 seconds

#删除命名空间
hbase(main):001:0> drop_namespace 'baizhi'
0 row(s) in 0.1440 seconds

# 描述命名空间
hbase(main):001:0> describe_namespace 'shahe'
DESCRIPTION
{NAME => 'shahe', subway => 'shahezhan'}
1 row(s) in 0.1300 seconds

# 修改命名空间
hbase(main):003:0> alter_namespace 'shahe',{METHOD => 'set', 'subway'=>'gonghuacheng'}
0 row(s) in 0.0240 seconds

hbase(main):004:0> describe_namespace 'shahe'
DESCRIPTION
{NAME => 'shahe', subway => 'gonghuacheng'}
1 row(s) in 0.0060 seconds

# 列出所有命名空间
hbase(main):005:0> list_namespace
NAMESPACE
default
hbase
shahe
3 row(s) in 0.0160 seconds

3. 表操作(DDL 数据定义语言)

# 创建命名空间为baizhi
hbase(main):007:0> create_namespace 'baizhi'
0 row(s) in 0.0230 seconds

# 创建baizhi namespace的下的表叫做 t_user,有两个列簇叫做 cf1 cf2
hbase(main):008:0> create 'baizhi:t_user','cf1','cf2'
0 row(s) in 2.2930 seconds

=> Hbase::Table - baizhi:t_user

hbase(main):001:0> describe 'baizhi:t_user'
Table baizhi:t_user is ENABLED
baizhi:t_user
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BL
CODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE =>
', REPLICATION_SCOPE => '0'}
{NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BL
CODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE =>
', REPLICATION_SCOPE => '0'}
2 row(s) in 0.1930 seconds



# 直接删除无法删除,需要将其先disable掉
hbase(main):002:0> drop 'baizhi:t_user'

ERROR: Table baizhi: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'

# disable 表 (禁用表)
hbase(main):003:0> disable
disable                     disable_all                 disable_peer                disable_table_replication
hbase(main):003:0> disable 'baizhi:t_user'
0 row(s) in 2.2570 seconds

# 删除表
hbase(main):004:0> drop 'baizhi:t_user'
0 row(s) in 1.2370 seconds


hbase(main):001:0> create 'baizhi:t_user','cf1','cf2'
0 row(s) in 1.3470 seconds

=> Hbase::Table - baizhi:t_user

# 列出所有的表
hbase(main):002:0> list
TABLE
baizhi:t_user
1 row(s) in 0.0120 seconds

=> ["baizhi:t_user"]

# 列出某个命名空间下的表
hbase(main):003:0> list_namespace_tables 'baizhi'
TABLE
t_user
1 row(s) in 0.0130 seconds

4. 数据操作(数据管理语言 DML)

4.1 put
# 最普通的插入语句的方式
hbase(main):005:0> put 'baizhi:t_user','1','cf1:name','zhangsan'
0 row(s) in 0.1010 seconds


# 将表对象复制给变量t
hbase(main):006:0> t = get_table 'baizhi:t_user'
0 row(s) in 0.0010 seconds

=> Hbase::Table - baizhi:t_user

# 使用复制对象t 对表进行操作
hbase(main):007:0> t.put '1','cf1:age',18
0 row(s) in 0.0110 seconds

# 覆盖操作和put插入相似 写入相同的条件  写入值就可覆盖
hbase(main):008:0> t.put '1','cf1:age',19
0 row(s) in 0.0070 seconds
# 扫描 当前的表
hbase(main):009:0> t.scan
ROW                           COLUMN+CELL
 1                            column=cf1:age, timestamp=1569278257582, value=19
 1                            column=cf1:name, timestamp=1569277976093, value=zhangsan
1 row(s) in 0.0180 seconds
4.2 get
# 获取baizhi:t_user 下的rowk为1 的所有的cell
hbase(main):013:0> get 'baizhi:t_user' ,'1'
COLUMN                        CELL
 cf1:age                      timestamp=1569278257582, value=19
 cf1:name                     timestamp=1569277976093, value=zhangsan
2 row(s) in 0.0120 seconds

hbase(main):014:0> t.get '1'
COLUMN                        CELL
 cf1:age                      timestamp=1569278257582, value=19
 cf1:name                     timestamp=1569277976093, value=zhangsan
2 row(s) in 0.0030 seconds
# 发现获取版本只有一个,是因为表目前不支持多版本
hbase(main):015:0> t.get '1',{COLUMN=>'cf1:name',VERSIONS=>3}
# 获取baizhi:t_user 下的rowk为1 的所有的cell
hbase(main):013:0> get 'baizhi:t_user' ,'1'
COLUMN                        CELL
 cf1:age                      timestamp=1569278257582, value=19
 cf1:name                     timestamp=1569277976093, value=zhangsan
2 row(s) in 0.0120 seconds

hbase(main):014:0> t.get '1'
COLUMN                        CELL
 cf1:age                      timestamp=1569278257582, value=19
 cf1:name                     timestamp=1569277976093, value=zhangsan
2 row(s) in 0.0030 seconds
# 发现获取版本只有一个,是因为表目前不支持多版本
hbase(main):015:0> t.get '1',{COLUMN=>'cf1:name',VERSIONS=>3}
# 获取baizhi:t_user 下的rowk为1 的所有的cell
hbase(main):013:0> get 'baizhi:t_user' ,'1'
COLUMN                        CELL
 cf1:age                      timestamp=1569278257582, value=19
 cf1:name                     timestamp=1569277976093, value=zhangsan
2 row(s) in 0.0120 seconds

hbase(main):014:0> t.get '1'
COLUMN                        CELL
 cf1:age                      timestamp=1569278257582, value=19
 cf1:name                     timestamp=1569277976093, value=zhangsan
2 row(s) in 0.0030 seconds
# 发现获取版本只有一个,是因为表目前不支持多版本
hbase(main):015:0> t.get '1',{COLUMN=>'cf1:name',VERSIONS=>3}


# 创建一个最多有三个版本的表
hbase(main):001:0> create 'baizhi:t_user',{NAME=>'cf1',VERSIONS=>3}
0 row(s) in 1.3510 seconds

=> Hbase::Table - baizhi:t_user

hbase(main):006:0> t.get '1',{COLUMN=>'cf1:name',VERSIONS=>3}
COLUMN                        CELL
 cf1:name                     timestamp=1569279103046, value=zs
 cf1:name                     timestamp=1569279081528, value=zhangsan
2 row(s) in 0.0090 seconds

4.3 delete /deleteall

hbase(main):009:0> t.delete '1','cf1:name'
0 row(s) in 0.0270 seconds
hbase(main):010:0> t.scan
ROW                           COLUMN+CELL
 1                            column=cf1:age, timestamp=1569279066158, value=18
1 row(s) in 0.0090 seconds



# 根据时间戳进行删除
hbase(main):012:0> t.get '1',{COLUMN=>'cf1:age',VERSIONS=>3}
COLUMN                        CELL
 cf1:age                      timestamp=1569279425168, value=19
 cf1:age                      timestamp=1569279066158, value=18
2 row(s) in 0.0080 seconds
hbase(main):013:0> t.delete '1','cf1:age',1569279066158
0 row(s) in 0.0060 seconds
hbase(main):014:0> t.get '1',{COLUMN=>'cf1:age',VERSIONS=>3}
COLUMN                        CELL
 cf1:age                      timestamp=1569279425168, value=19
1 row(s) in 0.0030 seconds

hbase(main):002:0> deleteall 'baizhi:t_user','1'
0 row(s) in 0.1600 seconds

hbase(main):003:0> scan 'baizhi:t_user'
ROW                           COLUMN+CELL
0 row(s) in 0.0120 seconds


4.4 scan

#  查询列簇为cf1 从第一行开始
hbase(main):019:0> scan 'baizhi:t_user',{COLUMNS=>['cf1'],STARTROW=>'1'}
ROW                           COLUMN+CELL
 1                            column=cf1:age, timestamp=1569279798830, value=20
 1                            column=cf1:name, timestamp=1569279748288, value=zhangsan
 2                            column=cf1:age, timestamp=1569279809118, value=22
 2                            column=cf1:name, timestamp=1569279779869, value=wangwubaizhi
2 row(s) in 0.0100 seconds

#  查询列簇为cf1 从第二行开始
hbase(main):020:0> scan 'baizhi:t_user',{COLUMNS=>['cf1'],STARTROW=>'2'}
ROW                           COLUMN+CELL
 2                            column=cf1:age, timestamp=1569279809118, value=22
 2                            column=cf1:name, timestamp=1569279779869, value=wangwubaizhi
1 row(s) in 0.0080 seconds

#  查询列簇为cf1 从第三行开始
hbase(main):021:0> scan 'baizhi:t_user',{COLUMNS=>['cf1'],STARTROW=>'3'}
ROW                           COLUMN+CELL
0 row(s) in 0.0040 seconds

#  查询列簇为cf1 从第一行开始 只显示一行
hbase(main):022:0> scan 'baizhi:t_user',{COLUMNS=>['cf1'],STARTROW=>'1',LIMIT=>1}
ROW                           COLUMN+CELL
 1                            column=cf1:age, timestamp=1569279798830, value=20
 1                            column=cf1:name, timestamp=1569279748288, value=zhangsan
1 row(s) in 0.0070 seconds
#  查询列簇为cf1 从第一行开始 只显示两行
hbase(main):023:0> scan 'baizhi:t_user',{COLUMNS=>['cf1'],STARTROW=>'1',LIMIT=>2}
ROW                           COLUMN+CELL
 1                            column=cf1:age, timestamp=1569279798830, value=20
 1                            column=cf1:name, timestamp=1569279748288, value=zhangsan
 2                            column=cf1:age, timestamp=1569279809118, value=22
 2                            column=cf1:name, timestamp=1569279779869, value=wangwubaizhi
2 row(s) in 0.0260 seconds

4.5 count

hbase(main):025:0> count 'baizhi:t_user'
2 row(s) in 0.0130 seconds

=> 2

4.6 append

hbase(main):026:0> append 'baizhi:t_user','1','cf1:name','110'
0 row(s) in 0.0070 seconds

hbase(main):027:0> scan 'baizhi:t_user'
ROW                           COLUMN+CELL
 1                            column=cf1:age, timestamp=1569279798830, value=20
 1                            column=cf1:name, timestamp=1569280127798, value=zhangsan110
 2                            column=cf1:age, timestamp=1569279809118, value=22
 2                            column=cf1:name, timestamp=1569279779869, value=wangwubaizhi
2 row(s) in 0.0090 seconds


4.7 truncate

清空数据

hbase(main):028:0> truncate 'baizhi:t_user'
Truncating 'baizhi:t_user' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.4400 seconds

hbase(main):001:0> scan 'baizhi:t_user'
ROW                           COLUMN+CELL
0 row(s) in 0.1550 seconds

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明月清风,良宵美酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值