查看HBase状态
hbase(main):001:0> status
1 active master, 0 backup masters, 2 servers, 0 dead, 33.0000 average load
查看HBase版本
hbase(main):002:0> version
1.2.0-cdh5.12.1, rUnknown, Thu Aug 24 09:45:34 PDT 2017
查看当前用户
hbase(main):003:0> whoami
root (auth:SIMPLE)
groups: root, supergroup
查看对某张表进行操作的基本命令
hbase(main):004:0> table_help
...
查看所有表
hbase(main):005:0> list
TABLE
HBASE_TEST1
HBASE_TEST2
29 row(s) in 0.0570 seconds
=> ["HBASE_TEST1", "HBASE_TEST2"]
创建表
语法:
create , { NAME => , VERSIONS => , ...}, { NAME => , VERSIONS => , ...}
hbase(main):008:0> create 'test', 'cf'
0 row(s) in 1.2240 seconds
=> Hbase::Table - test
-
判断表是否存在
语法:
list <table>
hbase(main):009:0> list 'test'
TABLE
test
1 row(s) in 0.0030 seconds
=> ["test"]
语法:
exists <table>
hbase(main):015:0> exists 'test'
Table test does exist
0 row(s) in 0.0200 seconds
hbase(main):016:0> exists 'test1'
Table test1 does not exist
0 row(s) in 0.0120 seconds
-
查看表结构
语法:
describe <table>
hbase(main):010:0> describe 'test'
#或
hbase(main):010:0> desc 'test'
Table test is ENABLED
test
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf', 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'}
1 row(s) in 0.1100 seconds
其中的属性的意义:
属性
说明
NAME
列族名
VERSIONS
最大版本号
MIN_VERSIONS
最小版本号
TTL(Time To Live)
存活时间
IN_MEMORY
是否开启缓存,默认false,应该开启,否则与BLOCKCACHE冲突
BLOCKCACHE
读缓存是否开启,默认开启,64M
-
添加数据
语法:
put , , , ,
timestamp 系统默认
hbase(main):017:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.4670 seconds
hbase(main):018:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0110 seconds
hbase(main):019:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0070 seconds
-
全表扫描
语法:
scan , {COLUMNS => [,...], LIMIT => num}
扫描全表
hbase(main):020:0> scan 'test'
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1558942138361, value=value1
row2 column=cf:b, timestamp=1558942146386, value=value2
row3 column=cf:c, timestamp=1558942153915, value=value3
3 row(s) in 0.0690 seconds
扫描指定列族
hbase(main):021:0> scan 'test' ,{COLUMNS => 'cf'}
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1558942138361, value=value1
row2 column=cf:b, timestamp=1558942146386, value=value2
row3 column=cf:c, timestamp=1558942153915, value=value3
3 row(s) in 0.0130 seconds
设置开启Raw模式,开启Raw模式会把那些已添加删除标记但是未实际删除的数据也显示出来
scan 'test', {COLUMNS => 'cf', RAW => true}
查询test表中列族为info和data的信息
scan ‘test’, {COLUMNS => [‘cf’, ‘a’]}
查询test表中列族为cf,列名为a的信息
hbase(main):039:0> scan 'test', {COLUMNS => ['cf:a']}
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1558942138361, value=value1
1 row(s) in 0.0070 seconds
```shell
查询user表中列族为info,列名为name的信息,并且版本最新的5个
```shell
scan 'user', {COLUMNS => 'info:name', VERSIONS => 5}
查询user表中列族为info和data且列名含有a字符的信息
scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}
查询user表中列族为info,rk范围是[rk0001, rk0003)的数据
scan 'people', {COLUMNS => 'info', STARTROW => 'rk0001', ENDROW => 'rk0003'}
查询user表中row key以rk字符开头的
scan 'user',{FILTER=>"PrefixFilter('rk')"}
查询user表中指定时间范围的数据
scan 'user', {TIMERANGE => [1392368783980, 1392380169184]}
查询满足条件的前5条数据
hbase(main):024:0> scan 'test' ,{LIMIT => 5}
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1558942138361, value=value1
1 row(s) in 0.0180 seconds
scan的用法很多,参数,过滤条件可以各种组合。参考help 'scan'
-
查询某行记录
语法:
get , , { ...}
hbase(main):041:0> get 'test', 'row1'
COLUMN CELL
cf:a timestamp=1558942138361, value=value1
1 row(s) in 0.0130 seconds
-
查询表数据的行数
语法:
count , {INTERVAL => intervalNum ,CACHE => cacheNum}
INTERVAL 设置多少行显示一次及对应的rowkey,默认1000。
CACHE 每次去取的缓存区大小,默认10,调整该参数可提高查询速度。
hbase(main):001:0> count 'MY_SCHEMA:PERSONAS', {INTERVAL => 100 ,CACHE => 500}
Current count: 100, row: 0490ec15106068238
Current count: 200, row: 0920b813602752880
Current count: 300, row: 0db35c15900425591
Current count: 400, row: 124e8915900828482
Current count: 500, row: 16e0d113902624856
559 row(s) in 11.2610 seconds
=> 559
-
删除数据
语法:
delete , , , 必须指定列名。
hbase(main):004:0> delete 'test','row1','cf:a'
0 row(s) in 0.0880 seconds
注: 删除表test中row1行中cf:a列所有版本的数据
-
删除行
语法:
deleteall , , ,
可以不指定列名,删除整行数据。
hbase(main):003:0> deleteall 'test','row2'
0 row(s) in 0.0310 seconds
-
删除表中的所有数据
语法:
truncate
具体过程是: disable table
-> drop table
-> create table
hbase(main):005:0> truncate 'test'
Truncating 'test' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 3.6110 seconds
-
修改数据
语法:
put table>, , , ,
可以不指定列名,删除整行数据。
hbase(main):001:0> put 'test','rowkey001','cf:a','value1'
0 row(s) in 0.2410 seconds
hbase(main):002:0> scan 'test'
ROW COLUMN+CELL
rowkey001 column=cf:a, timestamp=1558945565238, value=value1
1 row(s) in 0.0550 seconds
hbase(main):003:0> put 'test','rowkey001','cf:a','value2'
0 row(s) in 0.0030 seconds
hbase(main):004:0> scan 'test'
ROW COLUMN+CELL
rowkey001 column=cf:a, timestamp=1558945576947, value=value2
1 row(s) in 0.0040 seconds
-
删除表
语法:
drop
注意: 在删除表前,需要先禁用表
hbase(main):008:0> disable 'test'
0 row(s) in 2.2680 seconds
hbase(main):009:0> drop 'test'
0 row(s) in 1.2520 seconds
hbase(main):011:0> exists 'test'
Table test does not exist
0 row(s) in 0.0100 seconds
-
修改表结构
增加列族
语法:
alter , { NAME => , VERSIONS => , ...} ...
hbase(main):015:0> alter 'test', 'add_family'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9340 seconds
删除列族
语法:
alter , { NAME => , METHOD => 'delete'}
用法:
alter 'table_name', {NAME => 'delete_family', METHOD => 'delete'}
或者
alter 'table_name', 'delete' => 'delete_family'
示例:
hbase(main):021:0> alter 'test', {NAME => 'add_family', METHOD => 'delete'},{NAME => 'add', METHOD => 'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 3.8020 seconds
添加列族f1同时删除列族f2
alter 'user', {NAME => 'f1'}, {NAME => 'f2', METHOD => 'delete'}
修改列族
将user表的f1列族版本号改为5
alter 'user', NAME => 'f1', VERSIONS => 5
-
禁用表与启用表
禁用表
disable 'table_name'
查看表是否禁用
is_disabled 'table_name'
启用表
enable 'table_name'
查看表是否启用
is_enabled 'table_name'
你可能感兴趣的:(HBase)