HBase Shell命令(DDL/DML等命令介紹)


啓動HBase Shell

要訪問HBase shell,必須導航進入到HBase的主文件夾。
cd /usr/localhost/Hbase

可以使用“hbase shell”命令來啓動HBase的交互shell,如命令所示。
./bin/hbase shell

在這裏插入圖片描述

從進入hbase shell中可以看到如下信息:
要退出交互shell命令,在任何時候鍵入exit 或使用<Ctrl + C>。
如果對某命令不瞭解使用,可以通過help command查看你想要瞭解到命令幫助信息

hbase shell中刪除命令:Ctrl+Backspace

HBase Shell相關命令

hbase shell命令 描述
create 創建表 < create ‘表名’, ‘列族名’, ‘列族名2’,‘列族名N’ >
list 查看所有表 < list all >
describe 顯示錶詳細信息 < describe ‘表名’ >
exists 判斷表是否存在 < exists ‘表名’ >
enable 使表有效 < enable ‘表名’ >
disable 使表無效 < disable ‘表名’ >
is_enabled 判斷是否啓動表 < is_enabled ‘表名’ >
is_disabled 判斷是否禁用表 < is_disabled ‘表名’ >
count 統計表中行的數量 < count ‘表名’ >
put 添加記錄 < put ‘表名’, ‘row key’, ‘列族1 : 列’, ‘值’ >
get 獲取記錄(row key下所有) < get ‘表名’, ‘row key’>
get 獲取記錄(某個列族) < get ‘表名’, ‘row key’, ‘列族’>
get 獲取記錄(某個列) < get ‘表名’,‘row key’,‘列族:列’ >
delete 刪除記錄 < delete ‘表名’, ‘row key’, ‘列族:列’ >
deleteall 刪除一行 < deleteall ‘表名’,‘row key’>
drop 刪除表 <disable ‘表名’> < drop ‘表名’>
alter 修改列族(column family)
incr 增加指定表,行或列的值
truncate 清空表 邏輯爲先刪除後創建
scan 通過對錶的掃描來獲取對用的值 <scan ‘表名’>
tools 列出hbase所支持的工具
status 返回hbase集羣的狀態信息
version 返回hbase版本信息
exit 退出hbase shell
shutdown 關閉hbase集羣(與exit不同)

通用命令

  • status: 提供HBase的狀態,例如,服務器的數量。
  • version: 提供正在使用HBase版本。
  • table_help: 表引用命令提供幫助。
  • whoami: 提供有關用戶的信息。

help命令

shell命令中help命令很重要,當你不知道有哪些命令,命令怎麼使用時,通過help命令獲取幫助。

hbase(main):003:0> help
HBase Shell, version 1.2.0-cdh5.7.6, rUnknown, Tue Feb 21 15:18:14 PST 2017
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, table_help, version, whoami

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

  Group name: namespace
  Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables

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

  Group name: tools
  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, compact_mob, compact_rs, flush, major_compact
, major_compact_mob, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, 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, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_
peer_tableCFs, show_peer_tableCFs
  Group name: snapshots
  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, list_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

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/book.html

例如不知道list怎麼使用,可以通過help ‘list’

hbase(main):008:0* help 'list' 
List all tables in hbase. Optional regular expression parameter could
be used to filter the output. Examples:

  hbase> list
  hbase> list 'abc.*'
  hbase> list 'ns:abc.*'
  hbase> list 'ns:.*'

由上面給出的信息可以看到list是列出所有的表或者加上命名空間列出指定某個命名空間所有的表

namespace操作

create_namespace

創建命名空間

hbase(main):039:0> create_namespace 'temp'
0 row(s) in 0.1320 seconds

list_namespace

列出所有命名空間

hbase(main):045:0> list_namespace
NAMESPACE                                                                                                                                                                                              
default                                                                                                                                                                                                
hbase                                                                                                                                                                                                  
ns1                                                                                                                                                                                                    
ns2                                                                                                                                                                                                    
ns3                                                                                                                                                                                                    
orders                                                                                                                                                                                                 
temp                                                                                                                                                                                                   
7 row(s) in 0.1520 seconds

list_namespace_tables

列出命令空間的所有表

hbase(main):055:0* list_namespace_tables 'orders'
TABLE                                                                                                                                                                                                  
history_orders                                                                                                                                                                                         
history_orders2                                                                                                                                                                                        
history_orders3                                                                                                                                                                                        
history_orders_hfile                                                                                                                                                                                   
history_orders_hfile2                                                                                                                                                                                  
5 row(s) in 0.0880 seconds

alter_namespace

修改命名空間屬性

hbase(main):047:0* help 'alter_namespace'
Alter namespace properties.

To add/modify a property:

  hbase> alter_namespace 'ns1', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}

To delete a property:

  hbase> alter_namespace 'ns1', {METHOD => 'unset', NAME=>'PROPERTY_NAME'}

describe_namespace

查看命名空間信息

hbase(main):048:0> describe_namespace 'temp'
DESCRIPTION                                                                                                                                                                                            
{NAME => 'temp'}                                                                                                                                                                                       
1 row(s) in 0.2570 seconds

drop_namespace

刪除命名空間,只能刪除空的命名空間(即命名空間中沒有任何表),不然會報錯
刪除飛空命名空間:

hbase(main):069:0* drop_namespace 'ns3'

ERROR: org.apache.hadoop.hbase.constraint.ConstraintException: Only empty namespaces can be removed. Namespace ns3 has 1 tables
	at org.apache.hadoop.hbase.master.TableNamespaceManager.remove(TableNamespaceManager.java:200)
	at org.apache.hadoop.hbase.master.HMaster.deleteNamespace(HMaster.java:2515)
	at org.apache.hadoop.hbase.master.MasterRpcServices.deleteNamespace(MasterRpcServices.java:496)
	at org.apache.hadoop.hbase.protobuf.generated.MasterProtos$MasterService$2.callBlockingMethod(MasterProtos.java:55730)
	at org.apache.hadoop.hbase.ipc.RpcServer.call(RpcServer.java:2170)
	at org.apache.hadoop.hbase.ipc.CallRunner.run(CallRunner.java:109)
	at org.apache.hadoop.hbase.ipc.RpcExecutor.consumerLoop(RpcExecutor.java:133)
	at org.apache.hadoop.hbase.ipc.RpcExecutor$1.run(RpcExecutor.java:108)
	at java.lang.Thread.run(Thread.java:745)

Here is some help for this command:
Drop the named namespace. The namespace must be empty.

刪除空的命名空間:

hbase(main):071:0* drop_namespace 'temp'
0 row(s) in 0.1620 seconds

DDL操作

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, locate_region, show_filters
這裏就不對每個命令一一介紹了,有用到後再查找怎麼使用,以下只介紹常用的

create

創建表

create 'temp:temp_ttl', 'info', VERSION => 3, TTL => '60'

describe

表的描述信息,可簡寫爲desc

hbase(main):110:0> describe 'temp:temp_ttl'
Table temp:temp_ttl is ENABLED                                                                                                                                                                         
temp:temp_ttl                                                                                                                                                                                          
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.1790 seconds

hbase(main):111:0> desc 'temp:temp_ttl'
Table temp:temp_ttl is ENABLED                                                                                                                                                                         
temp:temp_ttl                                                                                                                                                                                          
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.1290 seconds

exists

查看錶是否存在

hbase(main):112:0> exists 'temp:temp_ttl'
Table temp:temp_ttl does exist                                                                                                                                                                         
0 row(s) in 0.1200 seconds

alter

表的修改
添加列簇

hbase(main):117:0* alter 'temp:temp_ttl', 'info2'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.0640 seconds

hbase(main):118:0> desc 'temp:temp_ttl'
Table temp:temp_ttl is ENABLED                                                                                                                                                                         
temp:temp_ttl                                                                                                                                                                                          
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'}                                                                                                                               
{NAME => 'info2', 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'}                                                                                                                              
2 row(s) in 0.1500 seconds

刪除列簇

hbase(main):123:0> alter 'temp:temp_ttl', {NAME => 'info2', METHOD => 'delete'}, {NAME => 'info', VERSION => '50'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
Unknown argument ignored for column family info: 1.8.7
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 4.0050 seconds

hbase(main):124:0> desc 'temp:temp_ttl'
Table temp:temp_ttl is ENABLED                                                                                                                                                                         
temp:temp_ttl                                                                                                                                                                                          
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.0330 seconds

disable

禁用表,刪除表之前必須要先禁用表

hbase(main):152:0* disable 'temp:temp_ttl'
0 row(s) in 2.2780 seconds

enable

解除禁用

hbase(main):153:0> enable 'temp:temp_ttl'
0 row(s) in 1.2510 seconds

drop

刪除表,刪除表之前必須先禁用表,不然會報錯

hbase(main):155:0* drop 'temp:temp_ttl'

ERROR: Table temp:temp_ttl 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):166:0* disable 't1'
0 row(s) in 2.3620 seconds

hbase(main):167:0> drop 't1'
0 row(s) in 1.2590 seconds

DML操作

DML操作命令有:append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve
這裏就不對每個命令一一介紹了,有用到後再查找怎麼使用,以下只介紹常用的

scan

查找表中數據

hbase(main):170:0* scan 'temp:temp_ttl'
ROW                                                COLUMN+CELL                                                                                                                                         
 row1                                              column=info:age, timestamp=1586621297861, value=20                                                                                                  
 row1                                              column=info:gender, timestamp=1586621366709, value=\xE7\x94\xB7                                                                                     
 row1                                              column=info:name, timestamp=1586621875469, value=zhangsan2                                                                                          
1 row(s) in 0.0520 seconds

查詢指定的row_key和列名

hbase(main):004:0* scan 'temp:temp_ttl', {ROWPREFIXFILTER => 'row1', COLUMNS => ['info:name', 'info:age']}
ROW                                                COLUMN+CELL                                                                                                                                         
 row1                                              column=info:age, timestamp=1586621297861, value=20                                                                                                  
 row1                                              column=info:name, timestamp=1586621875469, value=zhangsan2                                                                                          
1 row(s) in 0.0870 seconds

put

插入數據

hbase(main):005:0> put 'temp:temp_ttl', 'row2', 'info:age', '21'
0 row(s) in 0.0970 seconds

get

獲取數據

hbase(main):016:0> get 'temp:temp_ttl', 'row1', {COLUMN => ['info:name', 'info:age'], TIMERANGE => [1586621297861, 1586621875469]}
COLUMN                                             CELL                                                                                                                                                
 info:age                                          timestamp=1586621297861, value=20                                                                                                                   
1 row(s) in 0.0270 seconds

**注意:**如果根據時間範圍獲取數據,第一個時間戳必須比第二個時間戳小(按時間從小到大),否則會報錯

hbase(main):012:0* get 'temp:temp_ttl', 'row1', {COLUMN => ['info:name', 'info:age'], TIMERANGE => [1586621875469, 1586621297861]}
COLUMN                                             CELL                                                                                                                                                

ERROR: maxStamp is smaller than minStamp

Here is some help for this command:
Get row or cell contents; pass table name, row, and optionally
a dictionary of column(s), timestamp, timerange and versions. Examples:

count

查看錶中有多少記錄數,注意同一個rowkey屬於一條記錄

hbase(main):018:0* count 'temp:temp_ttl'
2 row(s) in 0.0460 seconds

=> 2

delete

刪除指定數據

hbase(main):027:0* delete 'temp:temp_ttl', 'row2', 'info:name'
0 row(s) in 0.0670 seconds

hbase(main):028:0> 
hbase(main):029:0* scan 'temp:temp_ttl'
ROW                                                COLUMN+CELL                                                                                                                                         
 row1                                              column=info:age, timestamp=1586621297861, value=20                                                                                                  
 row1                                              column=info:gender, timestamp=1586621366709, value=\xE7\x94\xB7                                                                                     
 row1                                              column=info:name, timestamp=1586621875469, value=zhangsan2                                                                                          
 row2                                              column=info:age, timestamp=1586625576508, value=21                                                                                                  
2 row(s) in 0.1440 seconds

truncate

清空整個表的數據,先disable表,然後再drop表,最後重新create表

hbase(main):037:0* truncate 'temp:temp_ttl'
Truncating 'temp:temp_ttl' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 4.0810 seconds
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章