教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

HBase表常見Shell命令及具體語法操作講解【大數(shù)據(jù)文章】

更新時間:2020年11月17日14時10分 來源:傳智播客 瀏覽次數(shù):

  HBase Shell提供了大量的操作HBase的命令,通過Shell命令可以很方便地操作HBase數(shù)據(jù)庫,例如創(chuàng)建、刪除及修改表、向表中添加數(shù)據(jù)、列出表中的相關(guān)信息等操作。不過當使用Shell命令行操作HBase時,首先需要進入HBase Shell交互界面。執(zhí)行“bin/hbase shell”命令進入到目錄/hbase-1.2.1的界面,具體效果如圖1所示。

1605585479430_1.jpg

   圖1 進入HBase Shell的交互界面

  進入HBase Shell交互界面后,可以通過一系列Shell命令操作HBase,接下來,通過一張表列舉一些操作HBase表常見的Shell命令,具體如表1所示。

  表1 常見的Shell命令

1605585490032_2.png

關(guān)于HBase中常見的Shell操作的講解具體如下:

1. 創(chuàng)建表

通過create創(chuàng)建表,具體語法如下:

create 'table name','column family'

  在上述語法中,“table name”為表名,創(chuàng)建表必須指定;“column family”為列族名,創(chuàng)建表也必須指定。

  例如,創(chuàng)建一個名稱為student、列族名為info的HBase表,命令如下:

hbase(main):001:0> **create 'student','info'**
0 row(s) in 2.3870 seconds
=> Hbase::Table - student

  執(zhí)行“list”命令,查看數(shù)據(jù)庫中的數(shù)據(jù)表,命令如下:

hbase(main):002:0> **list**
TABLE
Student1 
row(s) in 0.0200 seconds
=> ["student"]

  在上述代碼中,出現(xiàn)了student數(shù)據(jù)表,說明創(chuàng)建表成功。

  2. 插入操作

  通過使用put插入或者更新表中的數(shù)據(jù),具體語法如下:

put 'table name','row1','column family: column name', 'value'

  在上述語法中,“row1”為行鍵(即Row Key);“column family:column name”為列族名和列名;“value”為插入列的值。

  例如,向student表中插入五條數(shù)據(jù),命令如下:

hbase(main):003:0> **put 'student','1001','info:sex','male'**
0 row(s) in 0.1350 seconds
hbase(main):004:0> **put 'student','1001','info:age','18'**
0 row(s) in 0.0390 seconds
hbase(main):005:0> **put 'student','1002','info:name','Janna'**
0 row(s) in 0.0360 seconds
hbase(main):006:0> **put 'student','1002','info:sex','female'**
0 row(s) in 0.0190 seconds
hbase(main):007:0> **put 'student','1002','info:age','20'**0 row(s) in 0.0120 seconds

  4. 查看操作

  通過describe查看表結(jié)構(gòu),具體語法如下:

describe 'table name'

  查看student表的表結(jié)構(gòu),命令如下:

hbase(main):009:0> **describe 'student'**
Table student is ENABLED
student
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', 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'}
1 row(s) in 0.0430 seconds

  上述代碼中,通過describe輸出了student表的結(jié)構(gòu),表結(jié)構(gòu)包含很多字段,具體介紹如下:

  NAME:表示列族名。

  BLOOMFILTER:表示為列族級別的類型(讀者只作了解即可)。

  VERIONS:表示版本數(shù)。

  N_MEMORY:設(shè)置是否存入內(nèi)存。

  KEEP_DELETED_CELLS:設(shè)置被刪除的數(shù)據(jù),在基于時間的歷史數(shù)據(jù)查詢中是否依然可見。

  DATA_BLOCK_ENCODING:表示數(shù)據(jù)塊的算法(讀者只作了解即可)。

  TTL:表示版本存活的時間。

  COMPRESSION:表示設(shè)置壓縮算法。

  MIN_VERSIONS:表示最小版本數(shù)。

  BLOCKCACHE:表示是否設(shè)置讀緩存。

  REPLICATION:表示設(shè)置備份。

  5. 更新操作

  通過使用put更新student表指定字段的數(shù)據(jù),具體語法如下:

put 'table name', 'row ','column family:column name','new value'

  在student表中,將行鍵為1001、列名name且值為18這一條數(shù)據(jù)中的值更新成100,命令如下:

hbase(main):010:0> **put 'student','1001','info:age','100'**
0 row(s) in 0.0420 seconds

  上述命令執(zhí)行成功后,使用scan掃描數(shù)據(jù)表中的數(shù)據(jù),掃描結(jié)果如下:

hbase(main):011:0> scan 'student'
ROW       COLUMN+CELL
**1001      column=info:age, timestamp=1545732938717, value=100**
1001      column=info:sex, timestamp=1545728722162, value=male
1002      column=info:age, timestamp=1545728751824, value=20
1002      column=info:name, timestamp=1545728738069, value=Janna
1002      column=info:sex, timestamp=1545728745582, value=female
2 row(s) in 0.0510 seconds

  上述代碼中,行鍵為1001、列名為info:name且值為18這一條數(shù)據(jù)中的值已經(jīng)更新成100。

  6. 獲取指定字段的操作

  通過使用get獲取指定行或指定列族:列的數(shù)據(jù),具體語法如下:

//查看指定行的數(shù)據(jù)
get 'table name','row1'

  獲取student表中行鍵為1001的數(shù)據(jù),命令如下:

hbase(main):013:0> **count 'student'**
2 row(s) in 0.0310 seconds
=> 2

  8. 刪除操作

  通過使用delete刪除表中“指定字段”的數(shù)據(jù),具體語法如下:

delete 'table name', 'row', 'column name', 'timestamp'

  刪除student表中行鍵為1002、列名為info:sex的一條數(shù)據(jù),命令如下:

hbase(main):014:0> **delete 'student','1002','info:sex'**
0 row(s) in 0.0370 seconds

  上述命令執(zhí)行成功后,使用scan獲取數(shù)據(jù)表中的數(shù)據(jù),命令如下:

hbase(main):015:0> scan 'student'
ROW       COLUMN+CELL
1001      column=info:age, timestamp=1545732938717, value=100
1001      column=info:sex, timestamp=1545728722162, value=male
1002      column=info:age, timestamp=1545728751824, value=20
1002      column=info:name, timestamp=1545728738069, value=Janna

  從上述代碼可以看出,行鍵為1002、列名為info:sex的數(shù)據(jù)已經(jīng)被刪除。

  如果要刪除表中一行所有的數(shù)據(jù),可以使用deleteall命令,具體語法如下:

deleteall 'table name', 'row'

  例如,刪除student表中行鍵為1001的所有數(shù)據(jù),命令如下:

hbase(main):016:0> **deleteall 'student','1001'**

0 row(s) in 0.0690 seconds

  上述通過使用scan掃描數(shù)據(jù)表中的數(shù)據(jù),掃描結(jié)果如下:

hbase(main):017:0> scan 'student'
ROW       COLUMN+CELL
1002      column=info:age, timestamp=1545728751824, value=20
1002      column=info:name, timestamp=1545728738069, value=Janna
1 row(s) in 0.0220 seconds

  從上述代碼可以看出,行鍵為1001的所有數(shù)據(jù)已經(jīng)被刪除了。

  通過使用truncate清空表中的所有數(shù)據(jù),具體語法如下:

truncate 'table name'

  清空student表中的所有數(shù)據(jù),命令如下:

hbase(main):0018:0> **truncate 'student'**
Truncating 'student' table (it may take a while):
\- Disabling table...
\- Truncating table...
0 row(s) in 3.9730 seconds

  通過使用scan掃描數(shù)據(jù)表中的數(shù)據(jù),掃描結(jié)果如下:

hbase(main):019:0> scan 'student'
ROW         COLUMN+CELL
0 row(s) in 0.3950 seconds

  從上述代碼可以看出,表student中的所有數(shù)據(jù)都已經(jīng)被清空。

  通過使用drop刪除表,具體語法如下:

drop 'table name'

  例如,刪除表student,命令如下:

hbase(main):020:0> **disable 'student'**
0 row(s) in 2.4410 seconds
hbase(main):021:0> **drop 'student'**
0 row(s) in 1.3540 seconds

  上述的代碼中,首先使用“disable”讓student表變?yōu)榻脿顟B(tài),然后進行刪除操作。若表不是禁用狀態(tài),則無法刪除。

  通過使用list獲取HBase數(shù)據(jù)庫中的所有數(shù)據(jù)表,命令如下:

hbase(main):022:0> list
TABLE
0 row(s) in 0.0180 seconds
=> []

  上述代碼中,“[ ]”表示數(shù)據(jù)庫已經(jīng)為空,說明student表已經(jīng)被刪除。


猜你喜歡:

HDFS的高可用架構(gòu)是怎樣工作的?

InputFormat接口的定義代碼怎么設(shè)置?

大數(shù)據(jù)培訓(xùn)課程 

0 分享到:
和我們在線交談!