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相关命令

hbaseshell命令描述
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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用HBase Shell来操作HBase数据库。HBase Shell是官方提供的一组命令,用于管理和查询HBase数据库。你可以通过在命令输入"hbase shell"命令来打开HBase Shell。 如果你想一次性执行多个HBase Shell命令,你可以将这些命令写入一个文件,然后使用类似于Linux shell脚本的方式依次执行这些命令。你可以将所有的HBase Shell命令写入一个文件(比如hehe.txt),然后使用以下命令来执行文件命令: ``` [hadoop@h71 hbase-1.0.0-cdh5.5.2]$ bin/hbase shell hehe.txt ``` 如果你想查看HBase Shell支持的所有命令,你可以使用"help 'hbase'"命令。这将显示HBase Shell支持的所有命令,并将它们分组为DDL(数据定义语言)和DML(数据操作语言)。此外,你还可以使用"help '命令名称'"来查看具体命令的使用方法和作用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [HBase Shell命令大全](https://blog.csdn.net/vbirdbest/article/details/88236575)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [hbase shell操作命令大全](https://blog.csdn.net/m0_37739193/article/details/73618899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值