hbase shell (1)

要存取hbase, 最簡單的方法就是透過hbase shell,
hbase shell是hbase內建的存取介面,
透過即時的命令輸入, 可以操作hbase中資料表的操作,
我們以hbase 0.94版本為例, 介紹hbase shell的功能:

首先, 我們先確認hbase和hadoop的程式都仍在執行,
接著, 進入hbase的路徑(/opt/hbase), 進入hbase shell:

$ /opt/hbase/bin/hbase shell

進入hbase shell後, 我們先輸入help查看說明:

hbase(main):003:0> help
HBase Shell, version 0.94.2, r1395367, Sun Oct  7 19:11:01 UTC 2012
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, en  able, enable_all, exists, is_disabled, is_enabled, list, show_filters

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

  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, remove_peer, start_replication, stop_replication

  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 shell的指令可以分成三種:
取得hbase系統資訊: zk_dump, status, list, etc..
對於表格的處理: create, disable, drop, truncate, etc...
對於表格內容的處理: put, get, scan, delete, count, etc...
我們將舉一些例子作為參考:

zk_dump是用來觀察hbase中zookeeper的狀態,
透過zk_dump, 我們可以看到zookeeper當前的負載,
同時也會顯示region server的狀態,
透過zk_dump是很簡單的方式檢查hbase cluster是否正常運作,

hbase(main):004:0> zk_dump
HBase is rooted at /hbase
Active master address: hbasemaster,60000,1427897189934
Backup master addresses:
Region server holding ROOT: hbaseslave02,60020,1427897192454
Region servers:
 hbaseslave01,60020,1427897191876
 hbasemaster,60020,1427897192810
 hbaseslave02,60020,1427897192454
Quorum Server Statistics:
 hbaseslave01:2222
  Zookeeper version: 3.4.3-1240972, built on 02/06/2012 10:48 GMT
  Clients:
   /192.168.144.11:50642[1](queued=0,recved=2264,sent=2264)
   /192.168.144.11:59582[1](queued=0,recved=12,sent=12)
   /192.168.144.11:50351[1](queued=0,recved=2383,sent=2398)
   /192.168.144.11:59586[0](queued=0,recved=1,sent=0)

  Latency min/avg/max: 0/0/58
  Received: 4704
  Sent: 4718
  Outstanding: 0
  Zxid: 0xc00000193
  Mode: leader
  Node count: 33
 hbasemaster:2222

[...]

drop是用來刪除table的指令,
在drop table之前, 必須先進行disable, 才不會出錯,
disable table後, table內的資料將無法存取,
以免在drop table時發生資料的不一致.

hbase(main):005:0> disable 'myHbaseTable'
0 row(s) in 2.2200 seconds
hbase(main):006:0> drop 'myHbaseTable'
0 row(s) in 1.1630 seconds

create table顧名思義, 就是產生hbase中的table,
格式為: create 'table name', 'col1', 'col2' .....

hbase(main):007:0> create 'myHbaseTable', 'myFamily'
0 row(s) in 1.7200 seconds

truncate會把原有的table先disable, drop,
再根據原有的table格式產生一個新的table,
在初始化hbase table時很有用, 格式為: truncate 'table name'

hbase(main):008:0> truncate 'myHbaseTable'
Truncating 'myHbaseTable' table (it may take a while):
 - Disabling table...
 - Dropping table...
 - Creating table...
0 row(s) in 5.4860 seconds

PUT, GET, SCAN和DELETE都是對hbase中數值的操作
PUT是把資料放入hbase的table中,
格式為: put 'table name', 'row name', 'column:qualifer', 'value'
GET是把資料從table特定位置中讀出,
格式為: get 'table name', 'row name', 'column:qualifer'
SCAN是把整張table的資料讀取出來,
若想要比較清楚的結果, 可以使用filter來讀取特定欄位(column)的資訊,
格式為: scan 'table name'
DELETE則是把資料刪除, 注意, 若是寫入時有qualifer,
也須於刪除時一併寫入qualifer的資訊, 否則會刪除失敗,
格式為: delete 'table name', 'row name', 'column:qualifer'

hbase(main):009:0> put 'myHbaseTable', 'row1', 'myFamily:q1', 'value'
0 row(s) in 0.7010 seconds
hbase(main):010:0> get 'myHbaseTable', 'row1', 'myFamily:q1'
COLUMN                      CELL
 myFamily:q1                timestamp=1428039523302, value=value
1 row(s) in 0.0240 seconds
hbase(main):011:0> scan 'myHbaseTable'
ROW                         COLUMN+CELL
 row1                       column=myFamily:q1, timestamp=1428039523302, value=value
1 row(s) in 0.0260 seconds
hbase(main):012:0> delete 'myHbaseTable', 'row1', 'myFamily:q1'
0 row(s) in 0.0140 seconds

留言

熱門文章

LTE筆記: RSRP, RSSI and RSRQ

[WiFi] WiFi 網路的識別: BSS, ESS, SSID, ESSID, BSSID

LTE筆記: 5G NR Measurement Events