文章目录
启动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