HBase 相关API操练(一):Shell操作
HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”。
HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建、删除及修改表, 还可以向表中添加数据、列出表中的相关信息等。
在启动 HBase 之后,用户可以通过下面的命令进入 HBase Shell 之中,命令如下所示:
[hadoop@master hbase]$ bin/hbase shell
2018-06-04 16:35:46,762 INFO [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.98.19-hadoop2, r1e527e73bc539a04ba0fa4ed3c0a82c7e9dd7d15, Fri Apr 22 19:07:24 PDT 2016
hbase(main):001:0>
输入 help 可以看到命令分组。
Group Name | Commands |
---|---|
general | status,version |
ddl | alter,create,describe,disable,drop,enable,exists,is_disable,is_enable,list |
dml | count,delete,deleteall,get,get_counter,incr,put,scan,truncate |
tools | assign,balance_switch,balancer,close_region,compact,flush,major_compact,move,split,unassign,zk_dump |
replication | add_peer,disable_peer,enable_peer,remove_peer,start_replication,stop_replication |
部分命令清单。
名称 | 命令表达式 |
---|---|
创建表 | create '表名称','列名称1','列名称2','列名称N' |
添加记录 | put '表名','行名','列名','值' |
查看记录 | get '表名','行名' |
查看表中的记录总数 | count '表名' |
删除记录 | delete '表名','行名称','列名称' |
删除一张表 | 第一步 disable'表名称'(先要屏蔽该表,才能对该表进行删除) 第二步 drop '表名称' |
查看所有记录 | scan '表名称' |
查看某个表某个列中的所有数据 | scan '表名称',['列名称:'] |
更新记录 | 就是重写一遍进行覆盖 |
下边分组举例 Shell 的各种操作。
general操作
查询 HBase 服务器状态 status。
hbase(main):002:0> status
2018-06-04 15:49:42,011 WARN [main] util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/hadoop/app/hbase-0.98.19/lib/phoenix-4.8.2-HBase-0.98-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/hadoop/app/hbase-0.98.19/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/hadoop/app/hadoop-2.6.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
1 active master, 0 backup masters, 1 servers, 0 dead, 7.0000 average load
查询hbase版本 version
hbase(main):003:0> version
0.98.19-hadoop2, r1e527e73bc539a04ba0fa4ed3c0a82c7e9dd7d15, Fri Apr 22 19:07:24 PDT 2016
ddl操作
1、 创建一个表
hbase(main):004:0> create 'member','memeber_id','address','info'
0 row(s) in 0.5980 seconds
=> Hbase::Table – member
2、 列出所有的表
hbase(main):005:0> list
TABLE
SYSTEM.CATALOG
SYSTEM.FUNCTION
SYSTEM.SEQUENCE
SYSTEM.STATS
member
tvcount
6 row(s) in 0.0290 seconds
=> ["SYSTEM.CATALOG", "SYSTEM.FUNCTION", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "member", "tvcount"]
3、 获得表的描述
hbase(main):008:0> describe 'member'
Table member is ENABLED
member
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => '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', COMPRESSION => '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', COMPRESSION => 'NONE', MIN_VERSIONS =>
'0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
3 row(s) in 0.0490 seconds
4、 删除一个列族: disable -> alter -> enable
hbase(main):013:0> disable 'member'
0 row(s) in 0.6500 seconds
hbase(main):011:0> alter 'member',{NAME=>'member_id',METHOD=>'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.2610 seconds
hbase(main):012:0> describe 'member'
Table member is DISABLED
member
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => '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', COMPRESSION => 'NONE', MIN_VERSIONS => '0',
TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0370 seconds
hbase(main):013:0> enable 'member'
0 row(s) in 0.6500 seconds
hbase(main):014:0> describe 'member'
Table member is ENABLED
member
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => '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', COMPRESSION => 'NONE', MIN_VERSIONS => '0',
TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0300 seconds
5、 查看表是否存在
hbase(main):015:0> exists 'member'
true
0 row(s) in 0.0305 seconds
6、 判断表是否为‘enable’和‘disable’
hbase(main):018:0> is_enabled 'member'
true
0 row(s) in 0.0200 seconds
hbase(main):021:0> is_disabled 'member'
false 0 row(s) in 0.0210 seconds
7、 删除一个表
hbase(main):013:0> disable 'member'
0 row(s) in 0.6500 seconds
hbase(main):026:0> drop 'member'
0 row(s) in 0.2570 seconds
hbase(main):027:0> list
TABLE
SYSTEM.CATALOG
SYSTEM.FUNCTION
SYSTEM.SEQUENCE
SYSTEM.STATS
tvcount
5 row(s) in 0.0060 seconds
=> ["SYSTEM.CATALOG", "SYSTEM.FUNCTION", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "tvcount"]
dml操作
首先新建一个表
hbase(main):028:0> create 'member','address','info'
0 row(s) in 0.1550 seconds
=> Hbase::Table - member
hbase(main):029:0> describe 'member'
Table member is ENABLED
member
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => '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', COMPRESSION => 'NONE', MIN_VERSIONS => '0',
TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0370 seconds
1、 插入几条记录
hbase(main):030:0> put 'member','zimo','info:age','28'
0 row(s) in 0.0750 seconds
hbase(main):031:0> put 'member','zimo','info:birthday','1994-07-17'
0 row(s) in 0.0080 seconds
hbase(main):032:0> put 'member','zimo','info:company','luoji'
0 row(s) in 0.0060 seconds
hbase(main):033:0> put 'member','zimo','address:country','china'
0 row(s) in 0.0100 seconds
hbase(main):034:0> put 'member','zimo','address:province','beijing'
0 row(s) in 0.0060 seconds
hbase(main):035:0> put 'member','zimo','address:city','beijing'
0 row(s) in 0.0050 seconds
put命令比较简单,只有这一种用法:
hbase> put ‘t1′, ‘r1′, ‘c1′, ‘value', ts1
t1指表名,r1指行键名,c1指列名,value指单元格值。ts1指时间戳,一般都省略掉了。
2、 全表扫描 scan
hbase(main):036:0> scan 'member'
ROW COLUMN+CELL
zimo column=address:city, timestamp=1528100333405, value=beijing
zimo column=address:country, timestamp=1528100280333, value=china
zimo column=address:province, timestamp=1528100323616, value=beijing
zimo column=info:age, timestamp=1528100120269, value=28
zimo column=info:birthday, timestamp=1528100169722, value=1994-07-17
zimo column=info:company, timestamp=1528100217095, value=luoji
1 row(s) in 0.0510 seconds
3、 获得数据 get
1) 获得一行的所有数据
hbase(main):037:0> get 'member','zimo'
COLUMN CELL
address:city timestamp=1528100333405, value=beijing
address:country timestamp=1528100280333, value=china
address:province timestamp=1528100323616, value=beijing
info:age timestamp=1528100120269, value=28
info:birthday timestamp=1528100169722, value=1994-07-17
info:company timestamp=1528100217095, value=luoji
6 row(s) in 0.0160 seconds
2) 获得某行,某列族的所有数据
hbase(main):038:0> get 'member','zimo','info'
COLUMN CELL
info:age timestamp=1528100120269, value=28
info:birthday timestamp=1528100169722, value=1994-07-17
info:company timestamp=1528100217095, value=luoji
3 row(s) in 0.0130 seconds
3) 获得某行,某列族,某列的所有数据
hbase(main):039:0> get 'member','zimo','info:company'
COLUMN CELL
info:company timestamp=1528100217095, value=luoji
1 row(s) in 0.0100 seconds
4、 更新一条记录 put(把scutshuxue年龄改为99)
hbase(main):040:0> put 'member','zimo','info:age','24'
0 row(s) in 0.0070 seconds
5、 删除 delete、 deleteall
1) 删除行'zimo', 列族为‘info' 中age的值
hbase(main):042:0> delete 'member','zimo','info:age'
0 row(s) in 0.0280 seconds
hbase(main):043:0> get 'member','zimo'
COLUMN CELL
address:city timestamp=1528100333405, value=beijing
address:country timestamp=1528100280333, value=china
address:province timestamp=1528100323616, value=beijing
info:birthday timestamp=1528100169722, value=1994-07-17
info:company timestamp=1528100217095, value=luoji
5 row(s) in 0.0180 seconds
2) 删除整行
deleteall 'member', 'zimo'
6、 查询表中有多少行
hbase(main):044:0> count 'member'
1 row(s) in 0.0190 seconds
=> 1
7、 将整个表清空
hbase(main):046:0> truncate 'member'
Truncating 'member' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 1.5320 seconds
可以看出,HBase 是通过先对表执行 disable,然后再执行 drop 操作后重建表来实现 truncate 的功能的。
以上就是博主为大家介绍的这一板块的主要内容,这都是博主自己的学习过程,希望能给大家带来一定的指导作用,有用的还望大家点个支持,如果对你没用也望包涵,有错误烦请指出。如有期待可关注博主以第一时间获取更新哦,谢谢!
版权声明:本文为博主原创文章,未经博主允许不得转载。