HBase學習筆記(三)———《HBase簡單shell操作命令》

HBase簡單shell操作命令

1、Hbase Shell 練習

$ /bin/hbase shell 
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.27, rfb434617716493eac82b55180b0bbd653beb90bf, Thu Mar 19 06:17:55 UTC 2015
hbase(main):001:0>



輸入help 然後 按回車鍵,可以看到一列shell命令。這裏的幫助很詳細,要注意的是表名,行和列需要加引號。

hbase(main):001:0> help
HBase Shell, version 0.94.27, rfb434617716493eac82b55180b0bbd653beb90bf, Thu Mar 19 06:17:55 UTC 2015
Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.

COMMAND GROUPS:
  Group name: general
  Commands: status, version, whoami

  Group name: ddl
  Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, is_disabled, is_enabled, list, show_filters

  Group name: dml
  Commands: count, delete, deleteall, get, get_counter, incr, put, scan, truncate, truncate_preserve

  Group name: tools
  Commands: assign, balance_switch, balancer, close_region, compact, flush, hlog_roll, major_compact, move, split, unassign, zk_dump

  Group name: replication
  Commands: add_peer, disable_peer, enable_peer, list_peers, list_replicated_tables, remove_peer, start_replication, stop_replication

  Group name: snapshot
  Commands: clone_snapshot, delete_snapshot, list_snapshots, restore_snapshot, snapshot

  Group name: security
  Commands: grant, revoke, user_permission

SHELL USAGE:
Quote all names in HBase Shell such as table and column names.  Commas delimit
command parameters.  Type <RETURN> after entering a command to run it.
Dictionaries of configuration used in the creation and alteration of tables are
Ruby Hashes. They look like this:

  {'key1' => 'value1', 'key2' => 'value2', ...}

and are opened and closed with curley-braces.  Key/values are delimited by the
'=>' character combination.  Usually keys are predefined constants such as
NAME, VERSIONS, COMPRESSION, etc.  Constants do not need to be quoted.  Type
'Object.constants' to see a (messy) list of all constants in the environment.

If you are using binary keys or values and need to enter them in the shell, use
double-quote'd hexadecimal representation. For example:

  hbase> get 't1', "key\x03\x3f\xcd"
  hbase> get 't1', "key\003\023\011"
  hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"

The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
For more on the HBase Shell, see http://hbase.apache.org/docs/current/book.html
hbase(main):002:0>

以網上的一個學生成績表的例子來演示hbase的用法。


這裏grad對於表來說是一個只有它自己的列族,course對於表來說是一個有兩個列的列族,這個列族由兩個列組成math和art,當然我們可以根據我們的需要在course中建立更多的列族,如computer,physics等相應的列添加入course列族。

create命令用法:

create 'table_name', 'column_family_name1', 'column_family_name2', ....

其中,table_name指表名,column_family_name1爲第一個列族名,column_family_name2爲第二個列族名,可以加多個列族名

create創建一個名爲 scores的表,這個表有兩個列族(column family),分別爲grad和courese,操作如下:

hbase(main):002:0> create 'scores', 'grade', 'course'
0 row(s) in 4.1860 seconds

=> Hbase::Table - scores

list命令用法:

1、list

列出所有的表

2、list 'table_name'

列出表名爲table_name的表

list列出所有的表來檢查創建情況,操作如下:

hbase(main):003:0> list
TABLE
scores
1 row(s) in 0.0090 seconds

=> ["scores"]

put命令比較簡單,只有一種用法:

put 'table_name', 'row_value', 'column_name', 'value', timestamp

其中,table_name指表名,row_value指行鍵值,column_name指列名(具體到某個列族下的某個列),value指單元格的值,timestamp指時間戳,一般省略

按設計的表結構用put插入值,操作如下:

hbase(main):004:0> put 'scores', 'Tom', 'grade', '5'
0 row(s) in 0.0770 seconds

hbase(main):005:0> put 'scores', 'Tom', 'course:math', '97'
0 row(s) in 0.0040 seconds

hbase(main):006:0> put 'scores', 'Tom', 'course:art', '87'
0 row(s) in 0.0040 seconds

hbase(main):007:0> put 'scores', 'Jim', 'grade', '4'
0 row(s) in 0.0050 seconds

hbase(main):008:0> put 'scores', 'Jim', 'course:math', '89'
0 row(s) in 0.0100 seconds

hbase(main):009:0> put 'scores', 'Jim', 'course:art', '80'
0 row(s) in 0.0090 seconds

scan命令用法:

1、scan 'table_name'

掃描表名爲'table_name'的表

scan掃描表名爲scores的表,操作如下:

hbase(main):010:0> scan 'scores'
ROW                   COLUMN+CELL
 Jim                  column=course:art, timestamp=1429439736957, value=80
 Jim                  column=course:math, timestamp=1429439727558, value=89
 Jim                  column=grade:, timestamp=1429439693821, value=4
 Tom                  column=course:art, timestamp=1429439680468, value=87
 Tom                  column=course:math, timestamp=1429439671589, value=97
 Tom                  column=grade:, timestamp=1429439628765, value=5
2 row(s) in 0.0190 seconds

2、scan 'table_name',{COLUMNS => 'column_name',LIMIT => 10, STARTROW => 'row_value'}

掃描表名爲'table_name'的表,列名爲column_name的,行鍵值以row_value開始的10條數據

scan掃描表名爲scores的表,列名爲course:math的數據,操作如下:

hbase(main):011:0> scan 'scores', {COLUMNS => 'course:math'}
ROW                     COLUMN+CELL
 Jim                    column=course:math, timestamp=1429439727558, value=89
 Tom                    column=course:math, timestamp=1429439671589, value=97
2 row(s) in 0.0070 seconds

3、scan 'table_name',{COLUMNS => ['column_name1','column_name2'],LIMIT => 10, STARTROW => 'row_value'}

掃描表名爲'table_name'的表,列名爲column_name1和column_name2的,行鍵值以row_value開始的10條數據

scan掃描表名爲scores的表,操作如下:

hbase(main):008:0> scan 'scores', {COLUMNS => ['course:math','course:art']}
ROW                     COLUMN+CELL
 Jim                    column=course:art, timestamp=1429439736957, value=80
 Jim                    column=course:math, timestamp=1429439727558, value=89
 Tom                    column=course:art, timestamp=1429439680468, value=87
 Tom                    column=course:math, timestamp=1429439671589, value=97
2 row(s) in 0.0080 seconds

4、scan 'table_name',{COLUMNS => ['column_name1','column_name2'],LIMIT => 10, STARTROW => 'row_value', VERSIONS => 'n'}

scan操作默認只掃描最新版本的數據,如果想掃描多版本的數據,操作如下:

hbase(main):009:0> scan 'scores', {COLUMNS => 'course:math', VERSIONS => 2]}
ROW COLUMN+CELL
Jim column=course:math, timestamp=1429439736957, value=80
Jim column=course:math, timestamp=1429439727558, value=89
Tom column=course:math, timestamp=1429439680468, value=87
Tom column=course:math, timestamp=1429439671589, value=97
2 row(s) in 0.0080 seconds


get查詢表名爲scores,行鍵值爲Jim的數據,操作如下:

hbase(main):011:0> get 'scores', 'Jim'
COLUMN                CELL
 course:art           timestamp=1429439736957, value=80
 course:math          timestamp=1429439727558, value=89
 grade:               timestamp=1429439693821, value=4
3 row(s) in 0.0160 seconds

get命令用法:

1、get 'table_name', 'row_value'

查詢表名爲table_name,行鍵值爲row_value的數據

get查詢表名爲scores,行鍵值爲Jim的數據,操作如下:

hbase(main):011:0> get 'scores', 'Jim'
COLUMN                CELL
 course:art           timestamp=1429439736957, value=80
 course:math          timestamp=1429439727558, value=89
 grade:               timestamp=1429439693821, value=4
3 row(s) in 0.0160 seconds

2、get 'table_name', 'row_value',{COLUMN => 'column_name'}

      get 'table_name', 'row_value','column_name'

上述兩種方法相同,查詢表名爲table_name,行鍵值爲row_value,列名爲column_name的數據

get查詢表名爲scores,行鍵值爲Jim,列名爲course:art的數據,操作如下:

hbase(main):012:0> get 'scores', 'Jim', {COLUMN => 'course:art'}
COLUMN                CELL
 course:art           timestamp=1429439736957, value=80
1 row(s) in 0.0040 seconds

3、get 'table_name', 'row_value', {COLUMN => ['column_name1', 'column_name2']}

      get 'table_name', 'row_value',  ['column_name1', 'column_name2']

      get 'table_name', 'row_value',  'column_name1', 'column_name2'

上述三種方法相同,查詢表名爲table_name,行鍵值爲row_value,列名爲column_name1和column_name2的數據

get查詢表名爲scores,行鍵值爲Jim,列名爲course:art和course:math的數據,操作如下:

hbase(main):013:0> get 'scores', 'Jim', 'course:art','course:math'
COLUMN                CELL
 course:art           timestamp=1429439736957, value=80
1 row(s) in 0.0040 seconds


delete命令用法:

1、delete 'table_name', 'row_value',{COLUMN => 'column_name'}

      delete 'table_name', 'row_value','column_name'

delete刪除表名爲scores,行鍵值爲Jim,列名爲course:math的單元格,操作如下:

hbase(main):009:0> delete 'scores', 'Jim', 'course:math'
0 row(s) in 0.0340 seconds

檢查一下數據是否被刪除,操作如下:

hbase(main):010:0> scan 'scores'
ROW                     COLUMN+CELL
 Jim                    column=course:art, timestamp=1429439736957, value=80
 Jim                    column=grade:, timestamp=1429439693821, value=4
 Tom                    column=course:art, timestamp=1429439680468, value=87
 Tom                    column=course:math, timestamp=1429439671589, value=97</span>


檢查一下數據是否被刪除,操作如下:

hbase(main):010:0> scan 'scores'
ROW                     COLUMN+CELL
 Jim                    column=course:art, timestamp=1429439736957, value=80
 Jim                    column=grade:, timestamp=1429439693821, value=4
 Tom                    column=course:art, timestamp=1429439680468, value=87
 Tom                    column=course:math, timestamp=1429439671589, value=97
deleteall命令用法:

deleteall 'table_name', 'row_value'

刪除表名爲table_name,行鍵值爲row_value的整行數據

deleteall刪除表名爲scores,行鍵值爲Tom的整行數據,操作如下:

hbase(main):018:0> deleteall 'scores', 'Tom'
0 row(s) in 0.0040 seconds

檢查一下數據是否被刪除,操作如下:

hbase(main):019:0> scan 'scores'
ROW                     COLUMN+CELL
 Jim                    column=course:art, timestamp=1429439736957, value=80
 Jim                    column=grade:, timestamp=1429439693821, value=4
1 row(s) in 0.0110 seconds


disable命令用法:

disable 'table_name'

禁用表名爲'table_name'的表

disable先禁用表名爲scores的表,操作如下:

hbase(main):020:0> disable 'scores'
0 row(s) in 1.2230 seconds

alter命令用法:

1、alter 'table_name',NAME => 'column_family_name'

2、alter  'table_name', NAME => 'column_family_name',METHOD => 'delete'

      刪除表名爲'table_name', 列族名爲'column_family_name'的列族

刪除表名爲scores,列族名爲grade的列族,操作如下:

hbase(main):025:0> alter 'scores', NAME => 'grade', METHOD => 'delete'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.1000 seconds

count命令操作:

count 'table_name'

統計表名爲‘table_name'的行數

hbase(main):027:0> count 'scores'
1 row(s) in 0.0200 seconds

=> 1

drop命令操作:

drop 'table_name'

刪除表名爲table_name的表。PS:要先用disable命令停用表後纔可以刪除

 drop刪除表,操作如下:

hbase(main):035:0> drop 'scores'
0 row(s) in 0.1470 seconds

關閉shell

hbase(main):013:0> exit

5、停止Hbase Shell

運行停止腳本來停止HBase.

$ ./bin/stop-hbase.sh
stopping hbase..............
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章