HBase学习2-shell客户端基本使用

1
help命令很有用,不会命令可以用help,例如help create

一、Admin API

1.建表(最简):必须指定好列族

create ‘table_name’,’family_name’

例如

1
2
3
4
5
6
7
8
9
10
一个列簇
hbase(main):003:0> create 'user_test','score'
0 row(s) in 0.1120 seconds
=> Hbase::Table - user_test
多个列簇
hbase(main):004:0> create 'user_test2','score','info'
0 row(s) in 0.1380 seconds
=> Hbase::Table - user_test2

非默认配置,例如多个版本

1
create 'message_box_test',{NAME => 'f', VERSIONS => 20}

2.查看表描述

describe ‘table_name’

例如下,可以看到table一些详细信息,比如列,blocksize, 布隆过滤器等。

1
2
3
4
5
6
7
hbase(main):005:0> describe 'user_test'
Table user_test is ENABLED
user_test
COLUMN FAMILIES DESCRIPTION
{NAME => 'score', 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'}
1 row(s) in 0.2570 seconds

3. 删除表(表结构 + 数据)

删除表,首先需要先disable掉表,然后再删除

1
2
3
4
5
hbase(main):002:0> disable 'user_test2'
0 row(s) in 1.2870 seconds
hbase(main):003:0> drop 'user_test2'
0 row(s) in 0.1680 seconds

4. 删除表(数据): truncate

1
2
3
主要:truncate有两个命令:
(1) truncate: 只剩下一个Region
(2) truncate_preserve:会保留现有的Region

put ‘user_test’,’row-1’,’score:math’,’90’
put ‘user_test’,’row-1’,’score:english’,’66’
put ‘user_test’,’row-1’,’score:geo’,’82’

(1) 三条数据

1
2
3
4
5
6
hbase(main):007:0> scan 'user_test',{LIMIT => 10}
ROW COLUMN+CELL
row-1 column=score:english, timestamp=1495594592903, value=66
row-1 column=score:geo, timestamp=1495594602187, value=82
row-1 column=score:math, timestamp=1495594584921, value=90
1 row(s) in 0.0730 seconds

(2) 从执行日志看truncate实际也会Disabled table

1
2
3
4
5
hbase(main):008:0> truncate 'user_test'
Truncating 'user_test' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 3.4180 seconds

(3) 表数据已经没了,但是表结构依然在

1
2
3
4
5
6
7
8
9
10
11
hbase(main):009:0> scan 'user_test',{LIMIT => 10}
ROW COLUMN+CELL
0 row(s) in 0.1720 seconds
hbase(main):010:0> describe 'user_test'
Table user_test is ENABLED
user_test
COLUMN FAMILIES DESCRIPTION
{NAME => 'score', 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'}
1 row(s) in 0.0480 seconds

二、数据操作

1. 增

put ‘table_name’,’rowkey’,’family_name:column_name’,’value’
put ‘table_name’,’rowkey’,’family_name:column_name’,’value’,timestamp

例如:

1
2
3
4
5
6
7
8
hbase(main):009:0> put 'user_test','rowkey-1','score:math','99'
0 row(s) in 0.0130 seconds
hbase(main):010:0> put 'user_test','rowkey-1','score:sport','100'
0 row(s) in 0.0070 seconds
hbase(main):011:0> put 'user_test','rowkey-1','score:english','66'
0 row(s) in 0.0050 seconds

2. 查
1
如果不指定VERSIONS,那么每列获取最新的一个版本

get ‘table_name’,’rowkey’
get ‘table_name’,’rowkey’,’family’
get ‘table_name’,’rowkey’,’family:column’
get ‘table_name’,’rowkey’,{column => ‘f:c’, timestamp => [ts1,ts2], VERSIONS => 4}

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
hbase(main):012:0> get 'user_test','rowkey-1'
COLUMN CELL
score:english timestamp=1495604900005, value=66
score:math timestamp=1495604879564, value=99
score:sport timestamp=1495604887677, value=100
3 row(s) in 0.0820 seconds
hbase(main):013:0> get 'user_test','rowkey-1','score'
COLUMN CELL
score:english timestamp=1495604900005, value=66
score:math timestamp=1495604879564, value=99
score:sport timestamp=1495604887677, value=100
3 row(s) in 0.0360 seconds
hbase(main):014:0> get 'user_test','rowkey-1','score:math'
COLUMN CELL
score:math timestamp=1461916251017, value=99
1 row(s) in 0.0080 seconds
hbase(main):016:0> get 'user_test','rowkey-1',{COLUMN => 'score:math', TIMESTAMP => 1495604879564}
COLUMN CELL
score:math timestamp=1495604879564, value=99
1 row(s) in 0.0130 seconds

3. 改

在HBase中改就是在cell里面添加一个新的版本,如果最大版本数是1,就会覆盖,其使用方法还是put。
例如,将english的分数改为20,发现timestamp= 1495604900005已经发生了变化,因为create table时没有指定版本数量,那么版本数量就是1。

1
2
3
4
5
6
7
8
9
hbase(main):015:0> put 'user_test','rowkey-1','score:english','20'
0 row(s) in 0.0040 seconds
hbase(main):016:0> get 'user_test','rowkey-1','score'
COLUMN CELL
score:english timestamp=1495605251276, value=20
score:math timestamp=1495604879564, value=99
score:sport timestamp=1495604887677, value=100
3 row(s) in 0.0310 seconds

下面修改一下最大版本数为3(这个操作如果是线上可能不太适合(考虑到数据量),具体有什么影响后面继续学习)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
hbase(main):016:0> alter 'user_test', NAME => 'score', VERSIONS => 3
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.0850 seconds
hbase(main):017:0> describe 'user_test'
Table user_test is ENABLED
user_test
COLUMN FAMILIES DESCRIPTION
{NAME => 'score', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VE
RSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
1 row(s) in 0.0200 seconds
4.删
在HBase中并不会立刻删除row,而是做了一个标记,当做major compact时候才会真正删除,但是对于客户端来说是访问不到的,但是major compact是必须要的,否则硬盘不会变小
  • delete ‘table_name’,’rowkey’, ‘family_name:column’
  • delete ‘table_name’,’rowkey’

例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
hbase(main):019:0> scan 'user_test',{LIMIT => 10}
ROW COLUMN+CELL
rowkey-1 column=score:english, timestamp=1495605251276, value=20
rowkey-1 column=score:math, timestamp=1495604879564, value=99
rowkey-1 column=score:sport, timestamp=1495604887677, value=100
1 row(s) in 0.0550 seconds
hbase(main):020:0> delete 'user_test','rowkey-1','score:math'
0 row(s) in 0.0210 seconds
hbase(main):021:0> scan 'user_test',{LIMIT => 10}
ROW COLUMN+CELL
rowkey-1 column=score:english, timestamp=1495605251276, value=20
rowkey-1 column=score:sport, timestamp=1495604887677, value=100
1 row(s) in 0.0360 seconds