三、HBase Shell 操作

一、 基本操作

1.进入 HBase 客户端命令行
      zlj@dell:/usr/local/Hbase$ bin/hbase shell

2.查看帮助命令
      hbase(main):001:0> help

3.查看当前数据库中有哪些表
      hbase(main):002:0> list

二、 表的操作

1.创建表
hbase(main):002:0> create 'student','info'

2.插入数据到表
hbase(main):003:0> put 'student','1001','info:sex','male'
hbase(main):004:0> put 'student','1001','info:age','18'
hbase(main):005:0> put 'student','1002','info:name','Janna'
hbase(main):006:0> put 'student','1002','info:gender','female'
hbase(main):007:0> put 'student','1002','info:age','20'

3.扫描查看表数据
hbase(main):008:0> scan 'student'
hbase(main):009:0> scan 'student',{STARTROW => '1001', STOPROW =>'1002'}
hbase(main):010:0> scan 'student',{STARTROW => '1001'}

4.查看表结构
hbase(main):011:0> describe ‘student’

5.更新指定字段的数据
hbase(main):012:0> put 'student','1001','info:name','Nick'
hbase(main):013:0> put 'student','1001','info:age','100'

6.查看“指定行”或“指定列族:列”的数据
hbase(main):014:0> get 'student','1001'
hbase(main):015:0> get 'student','1001','info:name'

7.统计表数据行数
hbase(main):021:0> count 'student'

8.删除数据
删除某 rowkey 的全部数据:
hbase(main):016:0> deleteall 'student','1001'

删除某 rowkey 的某一列数据:
hbase(main):017:0> delete 'student','1002','info:sex'

9.清空表数据
hbase(main):047:0> truncate 'test'
Truncating 'test' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.4070 seconds

hbase(main):048:0> list
TABLE                                                                                                                      
test                                                                                                                       
1 row(s) in 0.0150 seconds

=> ["test"]
hbase(main):049:0> 

提示:清空表的操作顺序为先 disable,然后再 truncate。truncate之后,表还存在,只是数据没有了。

10.删除表
首先需要先让该表为 disable 状态:
hbase(main):019:0> disable 'student'
然后才能 drop 这个表:
hbase(main):020:0> drop 'student'
提示:如果直接 drop 表,会报错:ERROR: Table student is enabled. Disable it first.

11.变更表信息
将 info 列族中的数据存放 3 个版本:
hbase(main):022:0> alter 'student',{NAME=>'info',VERSIONS=>3}
hbase(main):022:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3}

三、 HBase多版本操作:

hbase(main):032:0> describe 'test'
Table test is ENABLED                                                                                                      
test                                                                                                                       
COLUMN FAMILIES DESCRIPTION                                                                                                
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENC
ODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', 
REPLICATION_SCOPE => '0'}                                                                                                  
1 row(s) in 0.0310 seconds

目前版本为1,修改版本为3:
hbase(main):036:0> alter 'test', {NAME=>'info', VERSIONS=>3}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9960 seconds

hbase(main):037:0> scan 'test'
ROW                             COLUMN+CELL                                                                                
 10001                          column=info:age, timestamp=1544770827600, value=12                                         
 10001                          column=info:name, timestamp=1544770739729, value=tom                                       
1 row(s) in 0.0100 seconds

hbase(main):038:0> put 'test', '10001', 'info:name', 'jack'
0 row(s) in 0.0140 seconds

hbase(main):039:0> scan 'test'
ROW                             COLUMN+CELL                                                                                
 10001                          column=info:age, timestamp=1544770827600, value=12                                         
 10001                          column=info:name, timestamp=1544772955796, value=jack                                      
1 row(s) in 0.0100 seconds

hbase(main):040:0> get 'test', '10001', {COLUMN=>'info:name', VERSIONS=>3}
COLUMN                          CELL                                                                                       
 info:name                      timestamp=1544772955796, value=jack                                                        
 info:name                      timestamp=1544770739729, value=tom                                                         
1 row(s) in 0.0150 seconds

hbase(main):041:0> put 'test', '10001', 'info:name', 'marry'
0 row(s) in 0.0140 seconds

hbase(main):042:0> get 'test', '10001', {COLUMN=>'info:name', VERSIONS=>3}
COLUMN                          CELL                                                                                       
 info:name                      timestamp=1544773025754, value=marry                                                       
 info:name                      timestamp=1544772955796, value=jack                                                        
 info:name                      timestamp=1544770739729, value=tom                                                         
1 row(s) in 0.0140 seconds

hbase(main):043:0> put 'test', '10001', 'info:name', 'tina'
0 row(s) in 0.0110 seconds

** Hbase版本最多为3,大于3时,就会更新

hbase(main):044:0> get 'test', '10001', {COLUMN=>'info:name', VERSIONS=>3}
COLUMN                          CELL                                                                                       
 info:name                      timestamp=1544773037918, value=tina                                                        
 info:name                      timestamp=1544773025754, value=marry                                                       
 info:name                      timestamp=1544772955796, value=jack                                                        
1 row(s) in 0.0090 seconds

hbase(main):045:0> get 'test', '10001'
COLUMN                          CELL                                                                                       
 info:age                       timestamp=1544770827600, value=12                                                          
 info:name                      timestamp=1544773037918, value=tina                                                        
1 row(s) in 0.0070 seconds

hbase(main):046:0> get 'test', '10001', 'info:name'
COLUMN                          CELL                                                                                       
 info:name                      timestamp=1544773037918, value=tina                                                        
1 row(s) in 0.0060 seconds

hbase(main):047:0> 

你可能感兴趣的:(三、HBase Shell 操作)