HBase Shell操作
一、基本操作
1、進入HBase客戶端命令行
[root@hadoop102 hbase]$ bin/hbase shell
2、查看幫助命令
hbase(main):001:0> help
3、查看當前數據庫中有哪些表
hbase(main):002:0> list
二、表的相關操作
1.創建表getget
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:sex','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 => '1001'}
hbase(main):010:0> scan 'student',{STARTROW => '1001'}
4.查看錶結構
hbase(main):011:0> describe ‘student’d
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、變更表信息-版本使用
8.1此時只有一條數據
hbase(main):019:0> scan 'student'
ROW a COLUMN+CELL
1001 column=info:sex, timestamp=1574695433041, value=male
1 row(s) in 0.0260 seconds
8.2將info列族中的數據存放3個版本:
hbase(main):022:0> alter 'student',{NAME=>'info',VERSIONS=>3}
8.3此時只有一條數據,也就是一個版本
hbase(main):021:0> get 'student','1001',{COLUMN=>'info:sex',VERSIONS=>3}
COLUMN CELL
info:sex timestamp=1574695433041, value=male
1 row(s) in 0.0360 seconds
8.4修改sex數據,再查詢
hbase(main):022:0> put 'student','1001','info:sex','famale'
0 row(s) in 0.0380 seconds
此時有兩個版本
hbase(main):023:0> get 'student','1001',{COLUMN=>'info:sex',VERSIONS=>3}
COLUMN CELL
info:sex timestamp=1574695681028, value=famale
info:sex timestamp=1574695433041, value=male
1 row(s) in 0.0120 seconds
8.5多修改幾次看看底層會保存幾次版本,並且scan時數據是什麼值
hbase(main):024:0> put 'student','1001','info:sex','famale111'
0 row(s) in 0.0290 seconds
hbase(main):025:0> put 'student','1001','info:sex','famale222'
0 row(s) in 0.0200 seconds
此時保存的版本中已經沒有了male值,也就是指定了3個版本,底層只保留最新的三個值
hbase(main):026:0> get 'student','1001',{COLUMN=>'info:sex',VERSIONS=>3}
COLUMN CELL
info:sex timestamp=1574695762316, value=famale222
info:sex timestamp=1574695754743, value=famale111
info:sex timestamp=1574695681028, value=famale
1 row(s) in 0.0230 seconds
查詢返回最新的
hbase(main):027:0> scan 'student'
ROW COLUMN+CELL
1001 column=info:sex, timestamp=1574695762316, value=famale222
1 row(s) in 0.0370 seconds
指定了幾個版本底層會保存幾次最新值,但是獲取時返回的一定是最新的版本;
9、刪除數據
刪除某rowkey的全部數據:
hbase(main):016:0> deleteall 'student','1001'
刪除某rowkey的某一列數據:
hbase(main):017:0> delete 'student','1002','info:sex'
10、清空表數據
hbase(main):018:0> truncate 'student'
提示:清空表的操作順序爲先disable,然後再truncate。
11、刪除表
首先需要先讓該表爲disable狀態:
hbase(main):019:0> disable 'student'
然後才能drop這個表:
hbase(main):020:0> drop 'student'
提示:如果直接drop表,會報錯:ERROR: Table student is enabled. Disable it first.