HBase操作之HBase Shell(轉)

Hbase的訪問方式

1、Native Java API:最常規和高效的訪問方式;
2、HBase Shell:HBase的命令行工具,最簡單的接口,適合HBase管理使用;
3、Thrift Gateway:利用Thrift序列化技術,支持C++,PHP,Python等多種語言,適合其他異構系統在線訪問HBase表數據;
4、REST Gateway:支持REST 風格的Http API訪問HBase, 解除了語言限制;
5、MapReduce:直接使用MapReduce作業處理Hbase數據;
6、使用Pig/hive處理Hbase數據。

Hbase shell基本用法

hbase shell 的help對語法的介紹很全,搞明白help上的實例,對hbase shell的操作就很熟練了。
hbase shell 的操作分爲 10類,本文只介紹前4類,分別是:

Group name commands
general status, table_help, version, whoami
ddl alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, show_filters
namespace alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables
dml append, count, delete, deleteall, get, get_counter, incr, put, scan, truncate, truncate_preserve
tools assign, balance_switch, balancer, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, compact_rs, flush, major_compact, merge_region, move, split, trace, unassign, wal_roll, zk_dump
replication add_peer, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_tableCFs, show_peer_tableCFs
snapshots clone_snapshot, delete_all_snapshot, delete_snapshot, list_snapshots, restore_snapshot, snapshot
configuration update_all_config, update_config
security grant, revoke, user_permission
visibility labels add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility

Hbase shell 命令具體介紹

general

status

作用:查詢當前服務器狀態。
實例:

hbase(main):006:0> status
1 servers, 0 dead, 5.0000 average load
  • 1
  • 2

更多用法:

hbase(main):002:0> help 'status'
  hbase> status    
  hbase> status 'simple'
  hbase> status 'summary'
  hbase> status 'detailed'
  hbase> status 'replication'
  hbase> status 'replication', 'source'
  hbase> status 'replication', 'sink'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

version

作用:查看hbase版本
實例:

hbase(main):010:0> version
1.0.3, rf1e1312f9790a7c40f6a4b5a1bab2ea1dd559890, Tue Jan 19 19:26:53 PST 2016
  • 1
  • 2

whoami

作用:查詢當前hbase用戶
實例:

hbase(main):011:0> whoami
datanode1 (auth:SIMPLE)
    groups: datanode1
  • 1
  • 2
  • 3

ddl

create

作用:創建一個表
實例:

#在命名空間ns1下,創建表t1,其中有一個列族f1,f1的版本數爲5
hbase> create 'ns1:t1', {NAME => 'f1', VERSIONS => 5}

#在默認命名空間下,創建表t1,有三個列族f1,f2,f3
  hbase> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
#等價於
  hbase> create 't1', 'f1', 'f2', 'f3'

#創建表t1,列族f1,並設置f1的版本數爲1,屬性TTL爲2592000,屬性BLOCKCACHE爲true。屬性的含義在這就不解釋了。
  hbase> create 't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}

# 創建表t1,列族f1,並設置f1的配置hbase.hstore.blockingStoreFiles 爲 10
  hbase> create 't1', {NAME => 'f1', CONFIGURATION => {'hbase.hstore.blockingStoreFiles' => '10'}}


#創建表時,配置信息可以放在最後,例如:
  hbase> create 'ns1:t1', 'f1', SPLITS => ['10', '20', '30', '40']
  hbase> create 't1', 'f1', SPLITS => ['10', '20', '30', '40']
  hbase> create 't1', 'f1', SPLITS_FILE => 'splits.txt', OWNER => 'johndoe'
  hbase> create 't1', {NAME => 'f1', VERSIONS => 5}, METADATA => { 'mykey' => 'myvalue' }
  hbase> # Optionally pre-split the table into NUMREGIONS, using
  hbase> # SPLITALGO ("HexStringSplit", "UniformSplit" or classname)

#指定Pre-splitting的region的塊數,和分割函數。
  hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
  hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit', REGION_REPLICATION => 2, CONFIGURATION => {'hbase.hregion.scan.loadColumnFamiliesOnDemand' => 'true'}}

#也可以用另一個表t2的引用去創建一個新表t1,t1表具有t2的所有列族,並且加上f1列族。
  hbase> t1 = create 't2', 'f1'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

alter

作用:可以修改,增加,刪除表的列族信息、屬性、配置等。
實例:

#對於表t1,如果t1含有f1列族,則將f1列族的版本數設爲5.
#         如果t1不含f1列數,則添加f1列族到表t1上。並將f1的版本數設置爲5.
  hbase> alter 't1', NAME => 'f1', VERSIONS => 5

#添加或修改多個列族
  hbase> alter 't1', 'f1', {NAME => 'f2', IN_MEMORY => true}, {NAME => 'f3', VERSIONS => 5}

#刪除 命名空間ns1 中的 表t1 的 列族f1 的兩種方法
  hbase> alter 'ns1:t1', NAME => 'f1', METHOD => 'delete'
  hbase> alter 'ns1:t1', 'delete' => 'f1'

#修改表t1的MAX_FILESIZE屬性的值。
  hbase> alter 't1', MAX_FILESIZE => '134217728'

# 修改表t1或者列族f2的配置
  hbase> alter 't1', CONFIGURATION => {'hbase.hregion.scan.loadColumnFamiliesOnDemand' => 'true'}
  hbase> alter 't1', {NAME => 'f2', CONFIGURATION => {'hbase.hstore.blockingStoreFiles' => '10'}}

#刪除屬性
  hbase> alter 't1', METHOD => 'table_att_unset', NAME => 'MAX_FILESIZE'

  hbase> alter 't1', METHOD => 'table_att_unset', NAME => 'coprocessor$1'

#一次性修改多個屬性值
  hbase> alter 't1', { NAME => 'f1', VERSIONS => 3 }, 
   { MAX_FILESIZE => '134217728' }, { METHOD => 'delete', NAME => 'f2' },
   OWNER => 'johndoe', METADATA => { 'mykey' => 'myvalue' }
hbase(main):014:0> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

alter_async

作用:異步更新,與alter的作用相同。

describe / desc

作用:顯示錶的屬性,表的列族的屬性。
實例:

# 命令:顯示錶t1信息
  hbase> describe 't3'
# 顯示出的信息:
Table t3 is ENABLED                                                                
t3                                                                                 
COLUMN FAMILIES DESCRIPTION                                                        
{NAME => 'colfa', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP
_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TT
L => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', RE
PLICATION_SCOPE => '0'}                                                            
1 row(s) in 0.0200 seconds
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

disable

作用:disable表,刪除一個表之前,必須把表disable
實例:

#disable表t1
  hbase> disable 't1'
  • 1
  • 2

disable_all

作用: disable多個表,接受正則表達好似。
實例:

# disable 所有以t開頭的表
hbase> disable_all 't.*'
  • 1
  • 2

drop

作用:刪除表。但是刪除之前,必須disable該表
實例:

# 刪除表t2
hbase(main):005:0> disable 't2'
0 row(s) in 1.2270 seconds
hbase(main):006:0> drop 't2'
0 row(s) in 0.1750 seconds
  • 1
  • 2
  • 3
  • 4
  • 5

drop_all

作用:刪除多個表,接受正則表達式。
實例:

# 刪除所有表名以t開頭的表
hbase> drop_all 't.*'
  • 1
  • 2

enable

作用:與disble相反,enable表

enable_all

作用:enable多個表,接受正則表達式

exists

作用:查詢表是否存在
實例:

# 查詢表名爲t1的表是否存在
hbase(main):003:0>  exists 't1'
Table t1 does exist                                                                
0 row(s) in 0.3170 seconds
  • 1
  • 2
  • 3
  • 4

get_table

作用:返回一個表引用對象
實例:

# 將表t1的應用對象賦給t1d
  hbase> t1d = get_table 't1'
#t1d操作
t1d.scan
t1d.describe
...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

is_disabled

作用:查詢表是否disable

is_enabled

作用:查詢表是否enable

list

作用:顯示出hbase中的表,接受正則表達式
實例:

#顯示所有命名空間的所有表
  hbase> list
#顯示錶名以abc開頭的表
  hbase> list 'abc.*'
#顯示命名空間ns下的表名以abc開頭的表
  hbase> list 'ns:abc.*'
#顯示命名空間ns下的所有表
  hbase> list 'ns:.*'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

show_filters

作用:顯示出所有過濾器
實例:

#顯示出所有過濾器
  hbase> show_filters
  • 1
  • 2

namespace

create_namespace

作用:創建命名空間
實例:

# 創建命名空間ns1
  hbase> create_namespace 'ns1'

# 創建命名空間ns1,並且配置ns1
  hbase> create_namespace 'ns1', {'PROPERTY_NAME'=>'PROPERTY_VALUE'}
  • 1
  • 2
  • 3
  • 4
  • 5

alter_namespace

作用:修改,添加,刪除命名空間的屬性
實例:

# 設置命名空間ns1的屬性
  hbase> alter_namespace 'ns1', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}

# 刪除命名空間ns1的屬性
  hbase> alter_namespace 'ns1', {METHOD => 'unset', NAME=>'PROPERTY_NAME'}
  • 1
  • 2
  • 3
  • 4
  • 5

describe_namespace

作用:描述命名空間
實例:

# 描述命名空間ns1
hbase(main):008:0> describe_namespace 'ns1'
DESCRIPTION                                                                        
{NAME => 'ns1', PROPERTY_NAME => 'PROPERTY_VALUE'}                                 
1 row(s) in 0.0040 seconds
  • 1
  • 2
  • 3
  • 4
  • 5

drop_namespace

作用:刪除命名空間,命名空間必須爲空, 不包含表

list_namespace

作用:列出所有命名空間
實例:

# 列出所有命名空間
hbase(main):008:0> describe_namespace 'ns1'
DESCRIPTION                                                                        
{NAME => 'ns1', PROPERTY_NAME => 'PROPERTY_VALUE'}                                 
1 row(s) in 0.0040 seconds
  • 1
  • 2
  • 3
  • 4
  • 5

list_namespace_tables

作用:顯示出某一個命名空間下的所有表
實例:

# 顯示出默認命名空間下的所有表
hbase(main):004:0> list_namespace_tables 'default'
TABLE                                                                              
peoples                                                                            
t1                                                                                 
t3                                                                                 
3 row(s) in 0.0210 seconds
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

dml

scan

作用:掃描某一個表
實例:

# 掃描命名空間hbase下的meta表,顯示出meta表的所有數據
  hbase> scan 'hbase:meta'

# 掃描命名空間hbase下的meta表的列族info的列regioninfo,顯示出meta表的列族info下的regioninfo列的所有數據
  hbase> scan 'hbase:meta', {COLUMNS => 'info:regioninfo'}

# 掃描命名空間ns1下表t1的列族'c1'和'c2'。顯示出命名空間ns1下表t1的列族'c1'和'c2'的所有數據
     hbase> scan 'ns1:t1', {COLUMNS => ['c1', 'c2']}

# 掃描命名空間ns1下表t1的列族'c1'和'c2'。顯示出命名空間ns1下表t1的列族'c1'和'c2',且只顯示前10個rowkey的數據。
  hbase> scan 'ns1:t1', {COLUMNS => ['c1', 'c2'], LIMIT => 10}

# 掃描命名空間ns1下表t1的列族'c1'和'c2'。顯示出命名空間ns1下表t1的列族'c1'和'c2',且只顯示從rowkey=“xyz”開始的前10個rowkey的數據。
  hbase> scan 'ns1:t1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'} 

# 掃描默認命名空間下表t1的列族c1時間戳從'1303668804'到'1303668904'的數據
  hbase> scan 't1', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]}

# 反向顯示錶t1的數據
  hbase> scan 't1', {REVERSED => true}

# 過濾顯示錶t1的數據
  hbase> scan 't1', {FILTER => "(PrefixFilter ('row2') AND
    (QualifierFilter (>=, 'binary:xyz'))) AND (TimestampsFilter ( 123, 456))"}

# RAW爲true,顯示出表t1的所有數據,包括已經刪除的
  hbase> scan 't1', {RAW => true, VERSIONS => 10}

# 表t1的引用的掃描
  hbase> t11 = get_table 't1'
  hbase> t11.scan
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

append

作用:
實例:

# 向表t1的rowkey爲r1的列c1的值後面添加字符串value
  hbase> append 't1', 'r1', 'c1', 'value'

#表t1的引用對象t11使用append。
  hbase> t11.append 'r1', 'c1', 'value'
  • 1
  • 2
  • 3
  • 4
  • 5

count

作用:統計表的行數
實例:

#統計表t1的行數
count 't1'

#統計表t1的行數,其中參數的含義如下
# INTERVAL設置多少行顯示一次及對應的rowkey,默認1000;CACHE每次去取的緩存區大小,默認是10,調整該參數可提高查詢速度
# 例如,查詢表t1中的行數,每10條顯示一次,緩存區爲1000
count 't1', INTERVAL => 10, CACHE => 1000


#對應的表應用對象的用法
 hbase> t.count
 hbase> t.count INTERVAL => 100000
 hbase> t.count CACHE => 1000
 hbase> t.count INTERVAL => 10, CACHE => 1000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

delete

作用:刪除表中cell數據
實例:

# 刪除命名空間ns1下的表t1的rowkey的r1的列c1,時間戳爲ts1 
 hbase> delete 'ns1:t1', 'r1', 'c1', ts1

# 刪除默認命名空間下的表t1的rowkey的r1的列c1,時間戳爲ts1 
 hbase> delete 't1', 'r1', 'c1', ts1

#應用對象的用法
 hbase> t.delete 'r1', 'c1',  ts1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

deleteall

作用:一次性刪除多個cell數據
實例:

#刪除命名空間ns1下表t1的rowkey爲r1的所有數據
  hbase> deleteall 'ns1:t1', 'r1'

#刪除默認命名空間下表t1的rowkey爲r1的所有數據
  hbase> deleteall 't1', 'r1'

#刪除命名空間ns1下表t1的rowkey爲r1的列c1的所有數據
  hbase> deleteall 't1', 'r1', 'c1'

# 刪除默認命名空間下的表t1的rowkey的r1的列c1,時間戳爲ts1 
  hbase> deleteall 't1', 'r1', 'c1', ts1

#應用對象的用法
  hbase> t.deleteall 'r1'
  hbase> t.deleteall 'r1', 'c1'
  hbase> t.deleteall 'r1', 'c1', ts1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

get

作用:得到某一列或cell的數據。
實例:

#得到命名空間ns1下表t1的rowkey爲r1的數據
  hbase> get 'ns1:t1', 'r1'

#得到默認命名空間下表t1的rowkey爲r1的數據
  hbase> get 't1', 'r1'

#得到默認命名空間下表t1的rowkey爲r1,時間戳範圍在ts1和ts2之間的數據
  hbase> get 't1', 'r1', {TIMERANGE => [ts1, ts2]}

#得到默認命名空間下表t1的rowkey爲r1的c1列的數據
  hbase> get 't1', 'r1', {COLUMN => 'c1'}

#得到默認命名空間下表t1的rowkey爲r1的c1,c2,c3列的數據
  hbase> get 't1', 'r1', {COLUMN => ['c1', 'c2', 'c3']}

#得到默認命名空間下表t1的rowkey爲r1的c1列,時間戳爲ts1的數據
  hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}

#得到默認命名空間下表t1的rowkey爲r1的c1列,時間戳範圍爲ts1到ts2,版本數爲4的數據
  hbase> get 't1', 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}

#應用對象的用法
  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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

put

作用:添加cell
實例:

# 向命名空間ns1下表t1的rowkey爲r1的列c1添加數據
  hbase> put 'ns1:t1', 'r1', 'c1', 'value'

# 向默認命名空間下表t1的rowkey爲r1的列c1添加數據
  hbase> put 't1', 'r1', 'c1', 'value'

# 向默認命名空間下表t1的rowkey爲r1的列c1添加數據,並設置時間戳爲ts1
  hbase> put 't1', 'r1', 'c1', 'value', ts1

# 向默認命名空間下表t1的rowkey爲r1的列c1添加數據,並設置時間戳爲ts1,並設置屬性
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}

#引用對象的用法
t.put 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

truncate

作用:刪除表,不用disable
實例:

#刪除表t3,不用disable
truncate 't3'
  • 1
  • 2

引用文檔:

http://blog.csdn.net/woshiwanxin102213/article/details/17611457
split的三種方式

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