HBase Shell 常用命令
阿新 • • 發佈:2018-12-12
HBase常用命令
hbase shell命令 | 描述 |
alter | 修改列族(column family)模式 |
count | 統計表中行的數量 |
create | 建立表 |
describe | 顯示錶相關的詳細資訊 |
delete | 刪除指定物件的值(可以為表,行,列對應的值,另外也可以指定時間戳的值) |
deleteall | 刪除指定行的所有元素值 |
disable | 使表無效 |
drop | 刪除表 |
enable | 使表有效 |
exists | 測試表是否存在 |
exit | 退出hbase shell |
get | 獲取行或單元(cell)的值 |
incr | 增加指定表,行或列的值 |
list | 列出hbase中存在的所有表 |
put | 向指向的表單元新增值 |
tools | 列出hbase所支援的工具 |
scan | 通過對錶的掃描來獲取對用的值 |
status | 返回hbase叢集的狀態資訊 |
shutdown | 關閉hbase叢集(與exit不同) |
truncate | 重新建立指定表 |
version | 返回hbase版本資訊 |
- 進入shell
[[email protected]
bin]$ echo $HBASE_HOME /data/program/hbase [[email protected] bin]$ /data/program/hbase/bin/hbase shell SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/data/program/hbase-1.2.0-cdh5.8.3/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/data/program/hadoop-2.6.0-cdh5.8.3/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] HBase Shell; enter 'help<RETURN>' for list of supported commands. Type "exit<RETURN>" to leave the HBase Shell Version 1.2.0-cdh5.8.3, rUnknown, Wed Oct 12 20:33:08 PDT 2016 hbase(main):035:0> whoami hadoop (auth:SIMPLE) groups: hadoop
- 表結構
1. 建立表
語法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}
建立一個User表,並且有一個info列族
hbase(main):002:0> create 'User','info'
0 row(s) in 1.5890 seconds
=> Hbase::Table - User
3. 檢視所有表
hbase(main):003:0> list
TABLE
SYSTEM.CATALOG
SYSTEM.FUNCTION
SYSTEM.SEQUENCE
SYSTEM.STATS
TEST.USER
User
6 row(s) in 0.0340 seconds
=> ["SYSTEM.CATALOG", "SYSTEM.FUNCTION", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "TEST.USER", "User"]
4. 查看錶詳情
hbase(main):004:0> describe 'User'
Table User is ENABLED
User
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FORE
VER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.1410 seconds
hbase(main):025:0> desc 'User'
Table User is ENABLED
User
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FORE
VER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.0380 seconds
5. 表修改
刪除指定的列族
hbase(main):002:0> alter 'User', 'delete' => 'info'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.5340 seconds
- 表資料
1. 插入資料
語法:put <table>,<rowkey>,<family:column>,<value>
hbase(main):005:0> put 'User', 'row1', 'info:name', 'xiaoming'
0 row(s) in 0.1200 seconds
hbase(main):006:0> put 'User', 'row2', 'info:age', '18'
0 row(s) in 0.0170 seconds
hbase(main):007:0> put 'User', 'row3', 'info:sex', 'man'
0 row(s) in 0.0030 seconds
2. 根據rowKey查詢某個記錄
語法:get <table>,<rowkey>,[<family:column>,....]
hbase(main):008:0> get 'User', 'row2'
COLUMN CELL
info:age timestamp=1502368069926, value=18
1 row(s) in 0.0280 seconds
hbase(main):028:0> get 'User', 'row3', 'info:sex'
COLUMN CELL
info:sex timestamp=1502368093636, value=man
hbase(main):036:0> get 'User', 'row1', {COLUMN => 'info:name'}
COLUMN CELL
info:name timestamp=1502368030841, value=xiaoming
1 row(s) in 0.0120 seconds
3. 查詢所有記錄
語法:scan <table>, {COLUMNS => [ <family:column>,.... ], LIMIT => num}
掃描所有記錄
hbase(main):009:0> scan 'User'
ROW COLUMN+CELL
row1 column=info:name, timestamp=1502368030841, value=xiaoming
row2 column=info:age, timestamp=1502368069926, value=18
row3 column=info:sex, timestamp=1502368093636, value=man
3 row(s) in 0.0380 seconds
掃描前2條
hbase(main):037:0> scan 'User', {LIMIT => 2}
ROW COLUMN+CELL
row1 column=info:name, timestamp=1502368030841, value=xiaoming
row2 column=info:age, timestamp=1502368069926, value=18
2 row(s) in 0.0170 seconds
範圍查詢
hbase(main):011:0> scan 'User', {STARTROW => 'row2'}
ROW COLUMN+CELL
row2 column=info:age, timestamp=1502368069926, value=18
row3 column=info:sex, timestamp=1502368093636, value=man
2 row(s) in 0.0170 seconds
hbase(main):012:0> scan 'User', {STARTROW => 'row2', ENDROW => 'row2'}
ROW COLUMN+CELL
row2 column=info:age, timestamp=1502368069926, value=18
1 row(s) in 0.0110 seconds
hbase(main):013:0> scan 'User', {STARTROW => 'row2', ENDROW => 'row3'}
ROW COLUMN+CELL
row2 column=info:age, timestamp=1502368069926, value=18
1 row(s) in 0.0120 seconds
另外,還可以新增TIMERANGE和FITLER等高階功能
STARTROW,ENDROW必須大寫,否則報錯;查詢結果不包含等於ENDROW的結果集
4. 統計表記錄數
語法:count <table>, {INTERVAL => intervalNum, CACHE => cacheNum}
INTERVAL設定多少行顯示一次及對應的rowkey,預設1000;CACHE每次去取的快取區大小,預設是10,調整該引數可提高查詢速度
hbase(main):020:0> count 'User'
3 row(s) in 0.0360 seconds
=> 3
5. 刪除
刪除列
hbase(main):008:0> delete 'User', 'row1', 'info:age'
0 row(s) in 0.0290 seconds
刪除所有行
hbase(main):014:0> deleteall 'User', 'row2'
0 row(s) in 0.0090 seconds
刪除表中所有資料
hbase(main):016:0> truncate 'User'
Truncating 'User' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 3.6610 seconds
- 表管理
1. 禁用表
hbase(main):014:0> disable 'User'
0 row(s) in 2.2660 seconds
hbase(main):015:0> describe 'User'
Table User is DISABLED
User
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FORE
VER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.0340 seconds
hbase(main):016:0> scan 'User', {STARTROW => 'row2', ENDROW => 'row3'}
ROW COLUMN+CELL
ERROR: User is disabled.
2. 啟用表
hbase(main):017:0> enable 'User'
0 row(s) in 1.3470 seconds
hbase(main):018:0> describe 'User'
Table User is ENABLED
User
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FORE
VER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.0310 seconds
hbase(main):019:0> scan 'User', {STARTROW => 'row2', ENDROW => 'row3'}
ROW COLUMN+CELL
row2 column=info:age, timestamp=1502368069926, value=18
1 row(s) in 0.0280 seconds
3. 測試表是否存在
hbase(main):022:0> exists 'User'
Table User does exist
0 row(s) in 0.0150 seconds
hbase(main):023:0> exists 'user'
Table user does not exist
0 row(s) in 0.0110 seconds
hbase(main):024:0> exists user
NameError: undefined local variable or method `user' for #<Object:0x412ebe64>
4. 刪除表
刪除前,必須先disable
hbase(main):030:0> drop 'TEST.USER'
ERROR: Table TEST.USER is enabled. Disable it first.
Here is some help for this command:
Drop the named table. Table must first be disabled:
hbase> drop 't1'
hbase> drop 'ns1:t1'
hbase(main):031:0> disable 'TEST.USER'
0 row(s) in 2.2640 seconds
hbase(main):033:0> drop 'TEST.USER'
0 row(s) in 1.2490 seconds
hbase(main):034:0> list
TABLE
SYSTEM.CATALOG
SYSTEM.FUNCTION
SYSTEM.SEQUENCE
SYSTEM.STATS
User
5 row(s) in 0.0080 seconds
=> ["SYSTEM.CATALOG", "SYSTEM.FUNCTION", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "User"]
- Shell指令碼調取
如下所示,EOF Hbase命令 EOF
#!/bin/sh
exec /home/hbase shell<<EOF
describe 'TableName'
EOF