hbase shell
命名空间
注意点:我是用的1.2.11版本的hbase,所以退格键是往后删除,和平时使用习惯不太一样,ctrl+退格是往前删
1. list_namespace:查询所有命名空间
hbase(main):008:0> list_namespace
NAMESPACE
default
hbase
2. list_namespace_tables : 查询指定命名空间的表
hbase(main):014:0> list_namespace_tables 'hbase'
TABLE
meta
namespace
3. create_namespace : 创建指定的命名空间
hbase(main):018:0> create_namespace 'ns1'
hbase(main):019:0> list_namespace
NAMESPACE
default
hbase
ns1
4. describe_namespace : 查询指定命名空间的结构
hbase(main):021:0> describe_namespace 'ns1'
DESCRIPTION
{NAME => 'ns1'}
5. alter_namespace :修改命名空间的结构
hbase(main):022:0> alter_namespace 'ns1', {METHOD => 'set', 'name' => 'lixi'}
hbase(main):023:0> describe_namespace 'ns1'
DESCRIPTION
{NAME => 'ns1', name => 'lixi'}
hbase(main):022:0> alter_namespace 'ns1', {METHOD => 'unset', NAME => 'name'}
hbase(main):023:0> describe_namespace 'ns1'
6. 删除命名空间
hbase(main):026:0> drop_namespace 'ns1'
hbase(main):027:0> list_namespace
NAMESPACE
default
hbase
7. 利用新添加的命名空间建表
hbase(main):032:0> create 'ns1:t1', 'f1', 'f2'
=> Hbase::Table - ns1:t1
hbase(main):033:0> list
TABLE
ns1:t1
=> ["ns1:t1"]
DDL和DML的操作
DDL
create :建表
hbase(main):010:0> create 'user_info','base_info','extra_info'
=> Hbase::Table - user_info
hbase(main):043:0> create 'ns1:user_info', {NAME=>'base_info', BLOOMFILTER=>'ROWCOL',VERSIONS=>'3'}
describe : 查询表结构
hbase(main):003:0> describe 'ns1:user_info'
Table ns1:user_info is ENABLED
ns1:user_info
COLUMN FAMILIES DESCRIPTION
{NAME => 'base_info', BLOOMFILTER => 'ROWCOL', VERSIONS => '3', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS
=> '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
create , splits : 创建表分片
hbase(main):007:0> create 'ns1:t2', 'f1', SPLITS => ['10', '20', '30', '40']
修改表
- alter : 修改表,添加修改列簇信息
hbase(main):009:0> alter 'ns1:t1', {NAME=>'lixi_info'}
hbase(main):010:0> describe 'ns1:t1'
Table ns1:t1 is ENABLED
ns1:t1
COLUMN FAMILIES DESCRIPTION
{NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', B
LOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', B
LOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'lixi_info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS =>
'0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
3 row(s) in 0.0250 seconds
- 删除列簇
hbase(main):014:0> alter 'ns1:t1', 'delete' => 'lixi_info'
hbase(main):015:0> describe 'ns1:t1'
Table ns1:t1 is ENABLED
ns1:t1
COLUMN FAMILIES DESCRIPTION
{NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', B
LOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', B
LOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.0170 seconds
- 删除表(先要disable表,再删除表)
hbase(main):016:0> disable 'ns1:t1'
0 row(s) in 2.2790 seconds
hbase(main):017:0> drop 'ns1:t1'
0 row(s) in 1.2900 seconds
hbase(main):018:0> list
TABLE
ns1:t2
ns1:user_info
2 row(s) in 0.0090 seconds
=> ["ns1:t2", "ns1:user_info"]
DML
插入数据(put命令,不能一次性插入多条)
hbase(main):012:0> put 'user_info','001','base_info:username','lixi'
0 row(s) in 0.9800 seconds
scan扫描
hbase(main):024:0> scan 'user_info'
ROW COLUMN+CELL
001 column=base_info:love, timestamp=1538897913186, value=basketball
001 column=base_info:username, timestamp=1538897633942, value=lixi
002 column=base_info:username, timestamp=1538898168513, value=lishiming
2 row(s) in 0.0520 seconds
-
通过指定版本查询
注意点这里加的RAW=true,会将已经存过的数据(和在内存还没清除)也展示出来,可以flush表将内存超过版本存储的数据,清除
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AA5tTzwi-1571822830166)(D:\新机\千峰笔记\1571664692603.png)]
hbase(main):024:0>0 scan 'user_info', {RAW => true, VERSIONS => 1}
ROW COLUMN+CELL
001 column=base_info:age, timestamp=1546922817429, value=32
001 column=base_info:name, timestamp=1546923712904, value=rock
001 column=extra_info:feature, timestamp=1546922881922, value=shuai
001 column=super_info:size, timestamp=1546922931075, value=111
1 row(s) in 0.0160 seconds
hbase(main):025:0> scan 'user_info', {RAW => true, VERSIONS => 2}
ROW COLUMN+CELL
001 column=base_info:age, timestamp=1546922817429, value=32
001 column=base_info:name, timestamp=1546923712904, value=rock
001 column=base_info:name, timestamp=1546922810789, value=lixi
001 column=extra_info:feature, timestamp=1546922881922, value=shuai
001 column=super_info:size, timestamp=1546922931075, value=111
1 row(s) in 0.0180 seconds
- 查询指定列的数据
hbase(main):014:0> scan 'user_info',{COLUMNS => 'base_info:name'}
ROW COLUMN+CELL
001 column=base_info:name, timestamp=1546923712904, value=rock
- 分页查询
hbase(main):021:0> scan 'user_info', {COLUMNS => ['base_info:name', 'base_info:age'], LIMIT => 10, STARTROW => '001'}
ROW COLUMN+CELL
001 column=base_info:age, timestamp=1546922817429, value=32
001 column=base_info:name, timestamp=1546923712904, value=rock
get查询
hbase(main):015:0> get 'user_info','001','base_info:username'
COLUMN CELL
base_info:username timestamp=1538897633942, value=lixi
1 row(s) in 0.2500 seconds
hbase(main):017:0> put 'user_info','001','base_info:love','basketball'
0 row(s) in 0.0140 seconds
hbase(main):018:0> get 'user_info','001'
COLUMN CELL
base_info:love timestamp=1538897913186, value=basketball
base_info:username timestamp=1538897633942, value=lixi
- 根据时间戳查询
hbase(main):029:0> get 'user_info', '001', {TIMERANGE => [1546922817429, 1546922931075]}
COLUMN CELL
base_info:age timestamp=1546922817429, value=32
extra_info:feature timestamp=1546922881922, value=shuai
hbase(main):029:0> get 'zhao:user_info','001',{TIMERANGE => [1570662972435,15716716822385]}
COLUMN CELL
base_info:age timestamp=1571662922435, value=21
base_info:love timestamp=1571671682385, value=basketball
base_info:username timestamp=1571662847435, value=zhao
hbase的一个重要特性:排序特性(rowkey)
插入到hbase中去的数据,hbase会自动排序存储:
排序规则: 首先看行键,然后看列族名,然后看列(key)名; 按字典顺序
Hbase的这个特性跟查询效率有极大的关系
比如:一张用来存储用户信息的表,有名字,户籍,年龄,职业....等信息
然后,在业务系统中经常需要:
查询某个省的所有用户
经常需要查询某个省的指定姓的所有用户
思路:如果能将相同省的用户在hbase的存储文件中连续存储,并且能将相同省中相同姓的用户连续存储,那么,上述两个查询需求的效率就会提高!!!
做法:将查询条件拼到rowkey内
更新数据
hbase(main):010:0> put 'user_info', '001', 'base_info:name', 'rock'
0 row(s) in 0.1420 seconds
incr
hbase(main):053:0> incr 'user_info', '002', 'base_info:age3'
COUNTER VALUE = 1
0 row(s) in 0.0290 seconds
hbase(main):055:0> scan 'user_info'
ROW COLUMN+CELL
001 column=base_info:age, timestamp=1546922817429, value=32
001 column=base_info:name, timestamp=1546923712904, value=rock
001 column=extra_info:feature, timestamp=1546922881922, value=shuai
001 column=super_info:size, timestamp=1546922931075, value=111
002 column=base_info:age2, timestamp=1546924991038, value=11
002 column=base_info:age3, timestamp=1546925153059, value=\x00\x00\x00\x00\x00\x00\x00\x02
2 row(s) in 0.1020 seconds
删除数据
- 删除一个kv数据
hbase(main):058:0> delete 'user_info', '002', 'base_info:age3'
0 row(s) in 0.0180 seconds
hbase(main):059:0> scan 'user_info'
ROW COLUMN+CELL
001 column=base_info:age, timestamp=1546922817429, value=32
001 column=base_info:name, timestamp=1546923712904, value=rock
001 column=extra_info:feature, timestamp=1546922881922, value=shuai
001 column=super_info:size, timestamp=1546922931075, value=111
002 column=base_info:age2, timestamp=1546924991038, value=11
- 删除一行数据
hbase(main):028:0> deleteall 'user_info','001'
0 row(s) in 0.0100 seconds
hbase(main):029:0> scan 'user_info'
ROW COLUMN+CELL
- 删除指定的版本
hbase(main):081:0> delete 'user_info','001','extra_info:feature', TIMESTAMP=>1546922931075
- 删除一个表数据
disable 'user_info'
drop 'user_info'
- 判断表是否存在
hbase(main):083:0> exists 'user_info'
Table user_info does exist
0 row(s) in 0.0130 seconds
- 表生效和失效
hbase(main):084:0> enable 'user_info'
0 row(s) in 0.0130 seconds
hbase(main):085:0> disable 'user_info'
0 row(s) in 2.5100 seconds
- 统计表行数
hbase(main):088:0> count 'user_info'
1 row(s) in 0.0300 seconds
=> 1
- 清空表数据
hbase(main):089:0> truncate 'user_info'
刷新到文件
flush 'namespace:tablename'
效和失效
hbase(main):084:0> enable 'user_info'
0 row(s) in 0.0130 seconds
hbase(main):085:0> disable 'user_info'
0 row(s) in 2.5100 seconds
- 统计表行数
hbase(main):088:0> count 'user_info'
1 row(s) in 0.0300 seconds
=> 1
- 清空表数据
hbase(main):089:0> truncate 'user_info'
刷新到文件
flush 'namespace:tablename'