HBase常用shell操作
hive,hbase命令行无法回格
在secureCRT中,点击"选项"->“回话选项”->“终端”->“仿真”
右边的终端选择linux,在hbase shell中如输入出错,Ctrl+回格键即可删除
进入HBase客户端命令操作界面
$ bin/hbase shell
[root@hadoop01 bin]# hbase shell
2019-12-11 09:14:22,321 INFO [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/export/servers/hbase-1.2.0-cdh5.14.0/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/export/servers/hadoop-2.6.0-cdh5.14.0/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.14.0, rUnknown, Sat Jan 6 13:40:03 PST 2018
hbase(main):001:0>
查看帮助命令
hbase(main):001:0> help
hbase(main):001:0> help
HBase Shell, version 1.2.0-cdh5.14.0, rUnknown, Sat Jan 6 13:40:03 PST 2018
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, get_peer_config, list_peer_configs, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_tableCFs, show_peer_tableCFs, update_peer_config
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
Group name: rsgroup
Commands: add_rsgroup, balance_rsgroup, get_rsgroup, get_server_rsgroup, get_table_rsgroup, list_rsgroups, move_servers_rsgroup, move_tables_rsgroup, remove_rsgroup
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
查看当前数据库中有哪些表
hbase(main):002:0> list
hbase(main):002:0> list
TABLE
0 row(s) in 0.1630 seconds
=> []
创建一张表
创建user表,包含info、data两个列族
hbase(main):010:0> create 'user', 'info', 'data'
hbase(main):001:0> create 'user','info','data'
0 row(s) in 2.6920 seconds
=> Hbase::Table - user
hbase(main):002:0> list
TABLE
user
1 row(s) in 0.0140 seconds
=> ["user"]
或者
hbase(main):010:0> create 'test01', {NAME => 'info', VERSIONS => '3'},{NAME => 'data'}
hbase(main):001:0> create 'test01', {NAME => 'info', VERSIONS => '3'},{NAME => 'data'}
0 row(s) in 1.6410 seconds
=> Hbase::Table - test01
hbase(main):001:0> list
TABLE
test01
user
2 row(s) in 0.1570 seconds
=> ["test01", "user"]
添加数据操作
向user表中插入信息,row key为rk0001,列族info中添加name列标示符,值为zhangsan
hbase(main):011:0> put 'user', 'rk0001', 'info:name', 'zhangsan'
向user表中插入信息,row key为rk0001,列族info中添加gender列标示符,值为female
hbase(main):012:0> put 'user', 'rk0001', 'info:gender', 'female'
向user表中插入信息,row key为rk0001,列族info中添加age列标示符,值为20
hbase(main):013:0> put 'user', 'rk0001', 'info:age', 20
向user表中插入信息,row key为rk0001,列族data中添加pic列标示符,值为picture
hbase(main):014:0> put 'user', 'rk0001', 'data:pic', 'picture'
hbase(main):002:0> put 'user','rk0001','info:name','zhangsan'
0 row(s) in 1.0320 seconds
hbase(main):003:0> put 'user','rk0001','info:gender','female'
0 row(s) in 0.0110 seconds
hbase(main):004:0> put 'user','rk0001','info:age',20
0 row(s) in 0.0100 seconds
hbase(main):005:0> put 'user','rk0001','data:pic','picture'
0 row(s) in 0.0270 seconds
查询数据操作
通过rowkey进行查询
获取user表中row key为rk0001的所有信息
hbase(main):015:0> get 'user', 'rk0001'
hbase(main):006:0> get 'user','rk0001'
COLUMN CELL
data:pic timestamp=1576028095510, value=picture
info:age timestamp=1576027752683, value=20
info:gender timestamp=1576027728322, value=female
info:name timestamp=1576027686868, value=zhangsan
4 row(s) in 0.1230 seconds
查看rowkey下面的某个列族的信息
获取user表中row key为rk0001,info列族的所有信息
hbase(main):016:0> get 'user', 'rk0001', 'info'
hbase(main):001:0> get 'user','rk0001','info'
COLUMN CELL
info:age timestamp=1576027752683, value=20
info:gender timestamp=1576027728322, value=female
info:name timestamp=1576027686868, value=zhangsan
3 row(s) in 0.1870 seconds
查看rowkey指定列族指定字段的值
获取user表中row key为rk0001,info列族的name、age列标示符的信息
hbase(main):017:0> get 'user', 'rk0001', 'info:name', 'info:age'
hbase(main):002:0> get 'user','rk0001','info:name','info:age'
COLUMN CELL
info:age timestamp=1576027752683, value=20
info:name timestamp=1576027686868, value=zhangsan
2 row(s) in 0.1640 seconds
查看rowkey指定多个列族的信息
获取user表中row key为rk0001,info、data列族的信息
hbase(main):018:0> get 'user', 'rk0001', 'info', 'data'
hbase(main):001:0> get 'user','rk0001','info','data'
COLUMN CELL
data:pic timestamp=1576028095510, value=picture
info:age timestamp=1576027752683, value=20
info:gender timestamp=1576027728322, value=female
info:name timestamp=1576027686868, value=zhangsan
4 row(s) in 0.2740 seconds
或者你也可以这样写
hbase(main):019:0> get 'user', 'rk0001', {COLUMN => ['info', 'data']}
hbase(main):002:0> get 'user','rk0001',{COLUMN => ['info','data']}
COLUMN CELL
data:pic timestamp=1576028095510, value=picture
info:age timestamp=1576027752683, value=20
info:gender timestamp=1576027728322, value=female
info:name timestamp=1576027686868, value=zhangsan
4 row(s) in 0.0090 seconds
或者你也可以这样写,也行
hbase(main):020:0> get 'user', 'rk0001', {COLUMN => ['info:name', 'data:pic']}
hbase(main):003:0> get 'user','rk0001',{COLUMN => ['info:name','data:pic']}
COLUMN CELL
data:pic timestamp=1576028095510, value=picture
info:name timestamp=1576027686868, value=zhangsan
2 row(s) in 0.0090 seconds
指定rowkey与列值查询
获取user表中row key为rk0001,cell的值为zhangsan的信息
hbase(main):030:0> get 'user', 'rk0001', {FILTER => "ValueFilter(=, 'binary:zhangsan')"}
hbase(main):004:0> get 'user','rk0001',{FILTER => "ValueFilter(=,'binary:zhangsan')"}
COLUMN CELL
info:name timestamp=1576027686868, value=zhangsan
1 row(s) in 0.0400 seconds
指定rowkey与列值模糊查询
获取user表中row key为rk0001,列标示符中含有a的信息
hbase(main):031:0> get 'user', 'rk0001', {FILTER => "(QualifierFilter(=,'substring:a'))"}
hbase(main):005:0> get 'user','rk0001',{FILTER => "(QualifierFilter(=,'substring:a'))"}
COLUMN CELL
info:age timestamp=1576027752683, value=20
info:name timestamp=1576027686868, value=zhangsan
2 row(s) in 0.0180 seconds
继续插入一批数据
hbase(main):032:0> put 'user', 'rk0002', 'info:name', 'fanbingbing'
hbase(main):033:0> put 'user', 'rk0002', 'info:gender', 'female'
hbase(main):034:0> put 'user', 'rk0002', 'info:nationality', '中国'
hbase(main):035:0> get 'user', 'rk0002', {FILTER => "ValueFilter(=, 'binary:中国')"}
hbase(main):006:0> put 'user', 'rk0002', 'info:name', 'fanbingbing'
0 row(s) in 0.1090 seconds
hbase(main):007:0> put 'user', 'rk0002', 'info:gender', 'female'
0 row(s) in 0.0080 seconds
hbase(main):008:0> put 'user', 'rk0002', 'info:nationality', '中国'
0 row(s) in 0.0080 seconds
hbase(main):009:0> get 'user', 'rk0002', {FILTER => "ValueFilter(=, 'binary:中国')"}
COLUMN CELL
info:nationality timestamp=1576068745544, value=\xE4\xB8\xAD\xE5\x9B\xBD
1 row(s) in 0.0370 seconds
查询所有数据
查询user表中的所有信息
scan 'user'
hbase(main):010:0> scan 'user'
ROW COLUMN+CELL
rk0001 column=data:pic, timestamp=1576028095510, value=picture
rk0001 column=info:age, timestamp=1576027752683, value=20
rk0001 column=info:gender, timestamp=1576027728322, value=fema
le
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:gender, timestamp=1576068740081, value=fema
le
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
rk0002 column=info:nationality, timestamp=1576068745544, value
=\xE4\xB8\xAD\xE5\x9B\xBD
2 row(s) in 0.1080 seconds
列族查询
查询user表中列族为info的信息
scan 'user', {COLUMNS => 'info'}
scan 'user', {COLUMNS => 'info', RAW => true, VERSIONS => 5}
scan 'user', {COLUMNS => 'info', RAW => true, VERSIONS => 3}
hbase(main):011:0> scan 'user',{COLUMNS => 'info'}
ROW COLUMN+CELL
rk0001 column=info:age, timestamp=1576027752683, value=20
rk0001 column=info:gender, timestamp=1576027728322, value=fema
le
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:gender, timestamp=1576068740081, value=fema
le
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
rk0002 column=info:nationality, timestamp=1576068745544, value
=\xE4\xB8\xAD\xE5\x9B\xBD
2 row(s) in 0.0160 seconds
hbase(main):012:0> scan 'user', {COLUMNS => 'info', RAW => true, VERSIONS => 5}
ROW COLUMN+CELL
rk0001 column=info:age, timestamp=1576027752683, value=20
rk0001 column=info:gender, timestamp=1576027728322, value=fema
le
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:gender, timestamp=1576068740081, value=fema
le
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
rk0002 column=info:nationality, timestamp=1576068745544, value
=\xE4\xB8\xAD\xE5\x9B\xBD
2 row(s) in 0.0150 seconds
hbase(main):013:0> scan 'user', {COLUMNS => 'info', RAW => true, VERSIONS => 3}
ROW COLUMN+CELL
rk0001 column=info:age, timestamp=1576027752683, value=20
rk0001 column=info:gender, timestamp=1576027728322, value=fema
le
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:gender, timestamp=1576068740081, value=fema
le
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
rk0002 column=info:nationality, timestamp=1576068745544, value
=\xE4\xB8\xAD\xE5\x9B\xBD
2 row(s) in 0.0130 seconds
多列族查询
查询user表中列族为info和data的信息
scan 'user', {COLUMNS => ['info', 'data']}
scan 'user', {COLUMNS => ['info:name', 'data:pic']}
hbase(main):014:0> scan 'user', {COLUMNS => ['info', 'data']}
ROW COLUMN+CELL
rk0001 column=data:pic, timestamp=1576028095510, value=picture
rk0001 column=info:age, timestamp=1576027752683, value=20
rk0001 column=info:gender, timestamp=1576027728322, value=fema
le
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:gender, timestamp=1576068740081, value=fema
le
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
rk0002 column=info:nationality, timestamp=1576068745544, value
=\xE4\xB8\xAD\xE5\x9B\xBD
2 row(s) in 0.0190 seconds
hbase(main):015:0> scan 'user', {COLUMNS => ['info:name', 'data:pic']}
ROW COLUMN+CELL
rk0001 column=data:pic, timestamp=1576028095510, value=picture
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
2 row(s) in 0.0160 seconds
指定列族与某个列名查询
查询user表中列族为info、列标示符为name的信息
scan 'user', {COLUMNS => 'info:name'}
hbase(main):016:0> scan 'user', {COLUMNS => 'info:name'}
ROW COLUMN+CELL
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
2 row(s) in 0.0300 seconds
指定列族与列名以及限定版本查询
查询user表中列族为info、列标示符为name的信息,并且版本最新的5个
scan 'user', {COLUMNS => 'info:name', VERSIONS => 5}
hbase(main):017:0> scan 'user', {COLUMNS => 'info:name', VERSIONS => 5}
ROW COLUMN+CELL
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
2 row(s) in 0.0110 seconds
指定多个列族与按照数据值模糊查询
查询user表中列族为info和data且列标示符中含有a字符的信息
scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}
hbase(main):018:0> scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}
ROW COLUMN+CELL
rk0001 column=info:age, timestamp=1576027752683, value=20
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
rk0002 column=info:nationality, timestamp=1576068745544, value
=\xE4\xB8\xAD\xE5\x9B\xBD
2 row(s) in 0.0230 seconds
rowkey的范围值查询
查询user表中列族为info,rk范围是[rk0001, rk0003)的数据
scan 'user', {COLUMNS => 'info', STARTROW => 'rk0001', ENDROW => 'rk0003'}
hbase(main):019:0> scan 'user', {COLUMNS => 'info', STARTROW => 'rk0001', ENDROW => 'rk0003'}
ROW COLUMN+CELL
rk0001 column=info:age, timestamp=1576027752683, value=20
rk0001 column=info:gender, timestamp=1576027728322, value=fema
le
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:gender, timestamp=1576068740081, value=fema
le
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
rk0002 column=info:nationality, timestamp=1576068745544, value
=\xE4\xB8\xAD\xE5\x9B\xBD
2 row(s) in 0.0170 seconds
指定rowkey模糊查询
查询user表中row key以rk字符开头的
scan 'user',{FILTER=>"PrefixFilter('rk')"}
hbase(main):020:0> scan 'user',{FILTER=>"PrefixFilter('rk')"}
ROW COLUMN+CELL
rk0001 column=data:pic, timestamp=1576028095510, value=picture
rk0001 column=info:age, timestamp=1576027752683, value=20
rk0001 column=info:gender, timestamp=1576027728322, value=fema
le
rk0001 column=info:name, timestamp=1576027686868, value=zhangs
an
rk0002 column=info:gender, timestamp=1576068740081, value=fema
le
rk0002 column=info:name, timestamp=1576068734612, value=fanbin
gbing
rk0002 column=info:nationality, timestamp=1576068745544, value
=\xE4\xB8\xAD\xE5\x9B\xBD
2 row(s) in 0.0210 seconds
指定数据范围值查询
查询user表中指定范围的数据
scan 'user', {TIMERANGE => [1392368783980, 1392380169184]}
hbase(main):021:0> scan 'user', {TIMERANGE => [1392368783980, 1392380169184]}
ROW COLUMN+CELL
0 row(s) in 0.0090 seconds
更新数据操作
更新数据值
更新操作同插入操作一模一样,只不过有数据就更新,没数据就添加
更新版本号
将user表的f1列族版本号改为5
hbase(main):050:0> alter 'user', NAME => 'info', VERSIONS => 5
hbase(main):022:0> alter 'user', NAME => 'info', VERSIONS => 5
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 3.4960 seconds
删除数据以及删除表操作
指定rowkey以及列名进行删除
删除user表row key为rk0001,列标示符为info:name的数据
hbase(main):045:0> delete 'user', 'rk0001', 'info:name'
hbase(main):023:0> delete 'user', 'rk0001', 'info:name'
0 row(s) in 0.0540 seconds
指定rowkey,列名以及字段值进行删除
删除user表row key为rk0001,列标示符为info:name,timestamp为1392383705316的数据
delete 'user', 'rk0001', 'info:name', 1392383705316
hbase(main):024:0> delete 'user', 'rk0001', 'info:name', 1392383705316
0 row(s) in 0.0070 seconds
删除一个列族
删除一个列族:
alter 'user', NAME => 'info', METHOD => 'delete'
或
alter 'user', 'delete' => 'info'
清空表数据
hbase(main):017:0> truncate 'user'
删除表
首先需要先让该表为disable状态,使用命令:
hbase(main):049:0> disable 'user'
然后才能drop这个表,使用命令:
hbase(main):050:0> drop 'user'
(注意:如果直接drop表,会报错:Drop the named table. Table must first be disabled)
统计一张表有多少行数据
hbase(main):053:0> count 'user'
hbase(main):025:0> count 'user'
2 row(s) in 0.0220 seconds
=> 2
HBase的高级shell管理命令
status
例如:显示服务器状态
hbase(main):058:0> status 'hadoop01'
hbase(main):028:0> status 'hadoop01'
1 active master, 1 backup masters, 3 servers, 0 dead, 1.3333 average load
hbase(main):029:0>
whoami
显示HBase当前用户,例如:
hbase> whoami
hbase(main):026:0> whoami
root (auth:SIMPLE)
groups: root
list
显示当前所有的表
hbase(main):027:0> list
TABLE
test01
user
2 row(s) in 0.0240 seconds
=> ["test01", "user"]
count
统计指定表的记录数,例如:
hbase> count 'user'
hbase(main):025:0> count 'user'
2 row(s) in 0.0220 seconds
=> 2
describe
展示表结构信息
exists
检查表是否存在,适用于表量特别多的情况
is_enabled、is_disabled
检查表是否启用或禁用
alter
该命令可以改变表和列族的模式,例如:
为当前表增加列族:
hbase> alter 'user', NAME => 'CF2', VERSIONS => 2
为当前表删除列族:
hbase(main):002:0> alter 'user', 'delete' => 'CF2'
disable/enable
禁用一张表/启用一张表
drop
删除一张表,记得在删除表之前必须先禁用
truncate
禁用表-删除表-创建表