Hbase學習第 5 天:HBase 命令行操作(下)

今天繼續學習 HBase 命令行操作,詳細操作如下

四、DML 操作語言

DML 是數據操作語言,用戶可以通過這些語言實現對數據的基本操作,包括數據的增刪改查
HBase DML 命令有 append、 count、 delete、 deleteall、 get、 get_counter、 get_splits、 incr、 put、 scan、 truncate、 truncate_preserve 等

1、HBase 表中添加數據

put 語法如下:

  hbase> put 'ns1:t1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value', ts1
  hbase> put 't1', 'r1', 'c1', 'value', {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

實例:

hbase(main):020:0> describe 'test'
Table test is ENABLED                                     
test                                                      
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.0260 seconds

# 向表 test 中放入兩條數據
hbase(main):017:0* put 'test','001','info','luo'
put 'test','002','info','luo_ff'
0 row(s) in 0.0640 seconds

hbase(main):022:0> put 'test','002','info','luo_ff'
0 row(s) in 0.0090 seconds

2、統計表中數據條數

hbase(main):023:0> count 'test'
2 row(s) in 0.0040 seconds

=> 2

3、查看錶中數據

HBase 中數據的瀏覽有兩種方式,一種是全表掃描,一種是指定獲取到一條數據,全表掃描 用 scan 命令,指定獲取某條數據 用 get 命令,具體使用如下:

3.1 scan 全表掃描

命令語法如下:

Some examples:

  hbase> scan 'hbase:meta'
  hbase> scan 'hbase:meta', {COLUMNS => 'info:regioninfo'}
  hbase> scan 'ns1:t1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
  hbase> scan 't1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
  hbase> scan 't1', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]}
  hbase> scan 't1', {REVERSED => true}
  hbase> scan 't1', {ALL_METRICS => true}
  hbase> scan 't1', {METRICS => ['RPC_RETRIES', 'ROWS_FILTERED']}
  hbase> scan 't1', {ROWPREFIXFILTER => 'row2', FILTER => "
    (QualifierFilter (>=, 'binary:xyz')) AND (TimestampsFilter ( 123, 456))"}
  hbase> scan 't1', {FILTER =>
    org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)}
  hbase> scan 't1', {CONSISTENCY => 'TIMELINE'}
For setting the Operation Attributes 
  hbase> scan 't1', { COLUMNS => ['c1', 'c2'], ATTRIBUTES => {'mykey' => 'myvalue'}}
  hbase> scan 't1', { COLUMNS => ['c1', 'c2'], AUTHORIZATIONS => ['PRIVATE','SECRET']}
For experts, there is an additional option -- CACHE_BLOCKS -- which
switches block caching for the scanner on (true) or off (false).  By
default it is enabled.  Examples:

  hbase> scan 't1', {COLUMNS => ['c1', 'c2'], CACHE_BLOCKS => false}

Also for experts, there is an advanced option -- RAW -- which instructs the
scanner to return all cells (including delete markers and uncollected deleted
cells). This option cannot be combined with requesting specific COLUMNS.
Disabled by default.  Example:

 hbase> scan 't1', {RAW => true, VERSIONS => 10}
 

實例:

hbase(main):033:0> scan 'test'
ROW                                                         COLUMN+CELL 
 001                                                        column=info:, timestamp=1574130792106, value=luo                                                                
 002                                                        column=info:, timestamp=1574131392911, value=luo_ff       
2 row(s) in 0.0120 seconds

3.2 HBase 單條數據查詢(get)

get 查詢單條數據語法如下:

hbase> t.get 'r1'
  hbase> t.get 'r1', {TIMERANGE => [ts1, ts2]}
  hbase> t.get 'r1', {COLUMN => 'c1'}
  hbase> t.get 'r1', {COLUMN => ['c1', 'c2', 'c3']}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
  hbase> t.get 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
  hbase> t.get 'r1', 'c1'
  hbase> t.get 'r1', 'c1', 'c2'
  hbase> t.get 'r1', ['c1', 'c2']
  hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE'}
  hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE', REGION_REPLICA_ID => 1}

實例:

hbase(main):003:0> get 'test','001'
COLUMN                                                      CELL
 info:                                                      timestamp=1574146101757, value=luo     
1 row(s) in 0.0230 seconds

4、在某一列的值後面追加

追加值使用 append 命令,命令語法如下:

  hbase> append 't1', 'r1', 'c1', 'value', ATTRIBUTES=>{'mykey'=>'myvalue'}
  hbase> append 't1', 'r1', 'c1', 'value', {VISIBILITY=>'PRIVATE|SECRET'

同樣的命令也可以在表引用上運行,語法如下:

 The same commands also can be run on a table reference. Suppose you had a reference
t to table 't1', the corresponding command would be:

  hbase> t.append 'r1', 'c1', 'value', ATTRIBUTES=>{'mykey'=>'myvalue'}
  hbase> t.append 'r1', 'c1', 'value', {VISIBILITY=>'PRIVATE|SECRET'}

實例:

hbase(main):012:0> append 'test','001','info:luo','luoluo'
CURRENT VALUE = luoluo
0 row(s) in 0.0280 seconds

hbase(main):013:0> scan 'test'
ROW                                                         COLUMN+CELL                                                                                                                                                                  
 001                                                        column=info:, timestamp=1574146101757, value=luo                                                                                                                             
 001                                                        column=info:luo, timestamp=1574154430334, value=luoluo                                                                                                                       
 002                                                        column=info:, timestamp=1574146107500, value=luo_ff 

5、刪除數據

HBase 刪除數據語法如下:

hbase> delete 'ns1:t1', 'r1', 'c1', ts1
hbase> delete 't1', 'r1', 'c1', ts1
hbase> delete 't1', 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

hbase> t.delete 'r1', 'c1',  ts1
hbase> t.delete 'r1', 'c1',  ts1, {VISIBILITY=>'PRIVATE|SECRET'}

實例:

# 刪除表 test 中的數據
hbase(main):018:0* delete "test","001","info:luo",1574130792106

0 row(s) in 0.0070 seconds

6、刪除行或者整個列族

使用 deleteall 命令刪除行或者整個列族,使用命令如下:

  hbase> deleteall 'ns1:t1', 'r1'
  hbase> deleteall 't1', 'r1'
  hbase> deleteall 't1', 'r1', 'c1'
  hbase> deleteall 't1', 'r1', 'c1', ts1
  hbase> deleteall 't1', 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

The same commands also can be run on a table reference. Suppose you had a reference
t to table 't1', the corresponding command would be:

  hbase> t.deleteall 'r1'
  hbase> t.deleteall 'r1', 'c1'
  hbase> t.deleteall 'r1', 'c1', ts1
  hbase> t.deleteall 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

實例:

hbase(main):017:0* deleteall 'test','001' 
0 row(s) in 0.0120 seconds

hbase(main):018:0> scan 'test'
ROW                                                         COLUMN+CELL                                                                                                                                                                  
 002                                                        column=info:, timestamp=1574146107500, value=luo_ff                                                                                                                          
1 row(s) in 0.0110 seconds

7、清空表數據

清空表數據有兩個命令,一個是truncate,另一個是truncate_preserve,truncate_preserve 只清空數據,不刪除region的劃分規則

實例:

hbase(main):025:0> truncate 'test'
Truncating 'test' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.3770 seconds

hbase(main):029:0> truncate_preserve 'test1'
Truncating 'test1' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 1.1490 seconds

通過上述實例可以看出,清空表數據的步驟首先先禁用此表,然後在清空表中數據

五、其他操作命令

HBase 除了第 4 天學習的 和以上基礎命令外,還有其他一些運用更廣泛的命令,這些命令我將在後面的時間瞭解透徹後作詳細的介紹,一起靜待,除了基礎命令外,HBase 其他命令如下:

 Group name: tools
  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, cleaner_chore_enabled, cleaner_chore_run, cleaner_chore_switch, clear_deadservers, close_region, compact, compact_rs, compaction_state, flush, is_in_maintenance_mode, list_deadservers, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, splitormerge_enabled, splitormerge_switch, trace, unassign, wal_roll, zk_dump
  Group name: replication
  Commands: add_peer, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, get_peer_config, list_peer_configs, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_bandwidth, set_peer_tableCFs, show_peer_tableCFs, update_peer_config
  Group name: snapshots
  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, delete_table_snapshots, list_snapshots, list_table_snapshots, restore_snapshot, snapshot
  Group name: configuration
  Commands: update_all_config, update_config
  Group name: quotas
  Commands: list_quotas, set_quota
  Group name: security
  Commands: grant, list_security_capabilities, revoke, user_permission
  Group name: procedures
  Commands: abort_procedure, list_procedures
  Group name: visibility labels
  Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility
  Group name: rsgroup
  Commands: add_rsgroup, balance_rsgroup, get_rsgroup, get_server_rsgroup, get_table_rsgroup, list_rsgroups, move_servers_rsgroup, move_servers_tables_rsgroup, move_tables_rsgroup, remove_rsgroup, remove_servers_rsgroup

總結,今天的文章是對 HBase 基礎命令的瞭解與運用,除了基礎命令以外,HBase 還有其他更有意思的命令,就如以上第二點的命令,這些命令我將在後面的作詳細介紹。

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