hbase shell命令

Hadoop_Liang發表於2020-10-09

環境:

hadoop2.7.3

hbase1.x,如果還沒安裝hbase,請點選這裡安裝hbase

 

hbase shell常用命令

啟動hadoop及hbase

start-all.sh
start-hbase.sh

進入hbase shell命令

hbase shell

檢視所有表

hbase(main):003:0> list
TABLE
0 row(s) in 0.0500 seconds

=> []

最開始hbase還沒有任何表

 

建表

語法: create '表名','列族1','列族2'

hbase(main):004:0> create 'Student','baseInfo','schoolInfo'
0 row(s) in 1.3250 seconds

=> Hbase::Table - Student

檢視錶資訊

語法:desc '表名'

hbase(main):005:0> describe 'Student'
Table Student is ENABLED
Student
COLUMN FAMILIES DESCRIPTION
{NAME => 'baseInfo', 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'}
{NAME => 'schoolInfo', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'fals
e', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVE
R', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE
=> '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.0890 seconds

禁用/啟用表

檢視錶是否啟用
hbase(main):006:0> is_enabled 'Student'
true
0 row(s) in 0.0200 seconds

禁用表
hbase(main):007:0> disable 'Student'
0 row(s) in 2.2660 seconds
hbase(main):008:0> is_enabled 'Student'
false
0 row(s) in 0.0270 seconds

啟用表
hbase(main):009:0> enable 'Student'
0 row(s) in 1.2520 seconds
hbase(main):010:0> is_enabled 'Student'
true
0 row(s) in 0.0150 seconds

刪除表

刪除表之前,需先禁用表
hbase(main):012:0> disable 'Student'
0 row(s) in 2.2390 seconds
刪除表
hbase(main):013:0> drop 'Student'
0 row(s) in 1.2630 seconds
確認表是否已經被刪除
hbase(main):015:0> exists 'Student'
Table Student does not exist
0 row(s) in 0.0090 seconds

增刪改表

上一步已經刪除了Student表,現在先將表再次建立出來
hbase(main):016:0> create 'Student','baseInfo','schoolInfo'
0 row(s) in 2.2290 seconds

=> Hbase::Table - Student

 新增列族

hbase(main):017:0> alter 'Student','teacherInfo'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9380 seconds


hbase(main):018:0> describe 'Student'
Table Student is ENABLED
Student
COLUMN FAMILIES DESCRIPTION
{NAME => 'baseInfo', 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'}
{NAME => 'schoolInfo', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'fals
e', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVE
R', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE
=> '65536', REPLICATION_SCOPE => '0'}
{NAME => 'teacherInfo', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'fal
se', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREV
ER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE
 => '65536', REPLICATION_SCOPE => '0'}
3 row(s) in 0.0130 seconds

刪除列族

刪除列族
hbase(main):019:0> alter 'Student',{NAME=>'teacherInfo', METHOD =>'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.3900 seconds

檢視錶結構
hbase(main):020:0> describe 'Student'
Table Student is ENABLED
Student
COLUMN FAMILIES DESCRIPTION
{NAME => 'baseInfo', 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'}
{NAME => 'schoolInfo', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'fals
e', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVE
R', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE
=> '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.0180 seconds

 插入/更新資料

語法:put ‘表名’, ‘行鍵’,‘列族:列’,‘值’

注意:如果新增資料的行鍵值、列族名、列名與原有資料完全相同(除了值之外),則相當於更新操作,否則為插入操作。

put插入一些資料 

put 'Student', 'rowkey1','baseInfo:name','jim'
put 'Student', 'rowkey1','baseInfo:birthday','1990-01-09'
put 'Student', 'rowkey1','baseInfo:age','18'
put 'Student', 'rowkey1','schoolInfo:name','Havard'
put 'Student', 'rowkey1','schoolInfo:localtion','Boston'

put 'Student', 'rowkey2','baseInfo:name','jack'
put 'Student', 'rowkey2','baseInfo:birthday','1998-08-22'
put 'Student', 'rowkey2','baseInfo:age','21'
put 'Student', 'rowkey2','schoolInfo:name','yale'
put 'Student', 'rowkey2','schoolInfo:localtion','New Haven'

put 'Student', 'rowkey3','baseInfo:name','maike'
put 'Student', 'rowkey3','baseInfo:birthday','1995-01-22'
put 'Student', 'rowkey3','baseInfo:age','24'
put 'Student', 'rowkey3','schoolInfo:name','yale'
put 'Student', 'rowkey3','schoolInfo:localtion','New Haven'

put 'Student', 'wrowkey4','baseInfo:name','maike-jack'

put更新操作

hbase(main):021:0> put 'Student','rowkey1','baseInfo:name','tom'
0 row(s) in 0.0760 seconds

hbase(main):022:0> put 'Student','rowkey1','baseInfo:birthday','1990-01-01'
0 row(s) in 0.0710 seconds

hbase(main):024:0> put 'Student','rowkey1','baseInfo:age','29'
0 row(s) in 0.0080 seconds

 刪除行資料

檢視行資料
hbase(main):042:0> get 'Student','rowkey3'
COLUMN                CELL
 baseInfo:age         timestamp=1600789513388, value=24
 baseInfo:birthday    timestamp=1600789513369, value=1995-01-22
 baseInfo:name        timestamp=1600789513349, value=maike
 schoolInfo:localtion timestamp=1600789513415, value=New Haven
 schoolInfo:name      timestamp=1600789513405, value=yale
1 row(s) in 0.0110 seconds
刪除指定行中指定列的資料
hbase(main):043:0> delete 'Student','rowkey3','baseInfo:name'
0 row(s) in 0.0240 seconds
再次檢視行資料
hbase(main):044:0> get 'Student','rowkey3'
COLUMN                CELL
 baseInfo:age         timestamp=1600789513388, value=24
 baseInfo:birthday    timestamp=1600789513369, value=1995-01-22
 schoolInfo:localtion timestamp=1600789513415, value=New Haven
 schoolInfo:name      timestamp=1600789513405, value=yale
1 row(s) in 0.0070 seconds

 

查詢

hbase中訪問資料有兩種基本的方式:

  • 按指定rowkey獲取資料:get方法;

  • 按指定條件獲取資料:scan方法。

scan可以設定begin和end引數來訪問一個範圍內所有的資料。get本質上就是begin和end相等的一種特殊的scan。

GET查詢

按rowkey查詢
hbase(main):059:0> get 'Student','rowkey3'
COLUMN                CELL
 baseInfo:age         timestamp=1600789513388, value=24
 baseInfo:birthday    timestamp=1600789513369, value=1995-01-22
 schoolInfo:localtion timestamp=1600789513415, value=New Haven
 schoolInfo:name      timestamp=1600789513405, value=yale
1 row(s) in 0.0110 seconds

按rowkey和列族來查詢
hbase(main):060:0> get 'Student','rowkey3','baseInfo'
COLUMN                CELL
 baseInfo:age         timestamp=1600789513388, value=24
 baseInfo:birthday    timestamp=1600789513369, value=1995-01-22
1 row(s) in 0.0070 seconds

按rowkey和列族中的某列來查詢
hbase(main):061:0> get 'Student','rowkey3','baseInfo:age'
COLUMN                CELL
 baseInfo:age         timestamp=1600789513388, value=24
1 row(s) in 0.0090 seconds

hbase(main):062:0>

SCAN查詢

hbase(main):065:0> scan 'Student'
ROW                            COLUMN+CELL
 rowkey1                       column=baseInfo:age, timestamp=1600789446674, value=29
 rowkey1                       column=baseInfo:birthday, timestamp=1600789402212, value=1990-01-01
 rowkey1                       column=baseInfo:name, timestamp=1600789375235, value=tom
 rowkey1                       column=schoolInfo:localtion, timestamp=1600789503013, value=Boston
 rowkey1                       column=schoolInfo:name, timestamp=1600789500988, value=Havard
 rowkey2                       column=baseInfo:age, timestamp=1600789513297, value=21
 rowkey2                       column=baseInfo:birthday, timestamp=1600789513285, value=1998-08-22
 rowkey2                       column=baseInfo:name, timestamp=1600789513258, value=jack
 rowkey2                       column=schoolInfo:localtion, timestamp=1600789513328, value=New Haven
 rowkey2                       column=schoolInfo:name, timestamp=1600789513314, value=yale
 rowkey3                       column=baseInfo:age, timestamp=1600789513388, value=24
 rowkey3                       column=baseInfo:birthday, timestamp=1600789513369, value=1995-01-22
 rowkey3                       column=schoolInfo:localtion, timestamp=1600789513415, value=New Haven
 rowkey3                       column=schoolInfo:name, timestamp=1600789513405, value=yale
 wrowkey4                      column=baseInfo:name, timestamp=1600789515763, value=maike-jack
4 row(s) in 0.0180 seconds
hbase(main):068:0> scan 'Student',{COLUMN=>'baseInfo'}
ROW                            COLUMN+CELL
 rowkey1                       column=baseInfo:age, timestamp=1600789446674, value=29
 rowkey1                       column=baseInfo:birthday, timestamp=1600789402212, value=1990-01-01
 rowkey1                       column=baseInfo:name, timestamp=1600789375235, value=tom
 rowkey2                       column=baseInfo:age, timestamp=1600789513297, value=21
 rowkey2                       column=baseInfo:birthday, timestamp=1600789513285, value=1998-08-22
 rowkey2                       column=baseInfo:name, timestamp=1600789513258, value=jack
 rowkey3                       column=baseInfo:age, timestamp=1600789513388, value=24
 rowkey3                       column=baseInfo:birthday, timestamp=1600789513369, value=1995-01-22
 wrowkey4                      column=baseInfo:name, timestamp=1600789515763, value=maike-jack
4 row(s) in 0.0180 seconds
hbase(main):071:0> scan 'Student',{COLUMNS=>'baseInfo:birthday'}
ROW                            COLUMN+CELL
 rowkey1                       column=baseInfo:birthday, timestamp=1600789402212, value=1990-01-01
 rowkey2                       column=baseInfo:birthday, timestamp=1600789513285, value=1998-08-22
 rowkey3                       column=baseInfo:birthday, timestamp=1600789513369, value=1995-01-22
3 row(s) in 0.0190 seconds
STARTROW為開始行
STOPROW為結束行
LIMIT為限制查詢結果行數
VERSIONS版本數
hbase(main):003:0> scan 'Student', {COLUMNS=> 'baseInfo',STARTROW => 'rowkey2',STOPROW => 'wrowkey4'}
ROW                            COLUMN+CELL
 rowkey2                       column=baseInfo:age, timestamp=1600789513297, value=21
 rowkey2                       column=baseInfo:birthday, timestamp=1600789513285, value=1998-08-22
 rowkey2                       column=baseInfo:name, timestamp=1600789513258, value=jack
 rowkey3                       column=baseInfo:age, timestamp=1600789513388, value=24
 rowkey3                       column=baseInfo:birthday, timestamp=1600789513369, value=1995-01-22
2 row(s) in 0.0200 seconds
只顯示1條資料
hbase(main):007:0> scan 'Student', {COLUMNS=> 'baseInfo',STARTROW => 'rowkey2',STOPROW => 'wrowkey4',LIMIT=>1}
ROW                            COLUMN+CELL
 rowkey2                       column=baseInfo:age, timestamp=1600789513297, value=21
 rowkey2                       column=baseInfo:birthday, timestamp=1600789513285, value=1998-08-22
 rowkey2                       column=baseInfo:name, timestamp=1600789513258, value=jack
1 row(s) in 0.0090 seconds

查詢年齡等於24的資料

hbase(main):010:0> scan 'Student', FILTER=>"ValueFilter(=,'binary:24')"
ROW                            COLUMN+CELL
 rowkey3                       column=baseInfo:age, timestamp=1600789513388, value=24
1 row(s) in 0.0790 seconds

查詢包含字串yal的資料

hbase(main):011:0> scan 'Student', FILTER=>"ValueFilter(=,'substring:yal')"
ROW                            COLUMN+CELL
 rowkey2                       column=schoolInfo:name, timestamp=1600789513314, value=yale
 rowkey3                       column=schoolInfo:name, timestamp=1600789513405, value=yale
2 row(s) in 0.0270 seconds

查詢列包括birth和值包括1988的資料

hbase(main):012:0> scan 'Student', FILTER=>"ColumnPrefixFilter('birth') AND ValueFilter ValueFilter(=,'substring:1998')"
ROW                            COLUMN+CELL
 rowkey2                       column=baseInfo:birthday, timestamp=1600789513285, value=1998-08-22
1 row(s) in 0.0630 seconds

PrefixFilter用於對Rowkey的字首進行判斷:

hbase(main):013:0> scan 'Student', FILTER=>"PrefixFilter('wr')"
ROW                            COLUMN+CELL
 wrowkey4                      column=baseInfo:name, timestamp=1600789515763, value=maike-jack
1 row(s) in 0.0190 seconds

 

參考:https://github.com/heibaiying/BigData-Notes/blob/master/notes/Hbase_Shell.md

 

 

完成!enjoy it!