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清空表逻辑为先删除后创建 <truncate ‘表明’>
scan通过对表的扫描来获取对用的值<scan ‘表名’>
tools列出hbase所支持的工具
status返回hbase集群的状态信息
version返回hbase版本信息
exit退出hbase shell
shutdown关闭hbase集群
count查询表中有多少行
truncate清空整个表的数据truncate

详细操作

  • 登录 - hbase shell
[root@hadoop01 bin]# hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/app/hbase-1.3.1/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/app/hadoop/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.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr  6 19:36:54 PDT 2017

  • 查询 - help
hbase(main):001:0> help
HBase Shell, version 1.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr  6 19:36:54 PDT 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_rs, flush, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, splitormerge_enabled, splitormerge_switch, 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

  Group name: snapshots
  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, delete_table_snapshots, list_snapshots, list_table_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
hbase(main):002:0> list
TABLE                                                                                                                                                                      
0 row(s) in 0.5450 seconds

=> []
hbase(main):003:0> 
  • 查询服务器状态status
hbase(main):003:0> status
1 active master, 1 backup masters, 1 servers, 2 dead, 2.0000 average load

hbase(main):004:0> 

  • 查询Hbase版本version
hbase(main):004:0> version
1.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr  6 19:36:54 PDT 2017

hbase(main):005:0> 

增删改

  • 创建一个表create
hbase(main):005:0> create 'student','info','address'			# 创建一个student表
0 row(s) in 2.9320 seconds

=> Hbase::Table - student
hbase(main):006:0> list			#查看所有表
TABLE                                                                                                                                                                      
student                                                                                                                                                                    
1 row(s) in 0.0630 seconds

=> ["student"]
hbase(main):007:0> 
  • 获得表的描述describe
hbase(main):002:0> describe 'student'		##语法 describe '表名'
Table student is ENABLED                                                                                                                                                   
student                                                                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                                                                                
{NAME => 'address', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSIO
N => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                                    
{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'}                                                                       
2 row(s) in 0.2980 seconds

hbase(main):003:0> 
  • 表是否存在exists
hbase(main):005:0> exists 'student'
Table student does exist                                                                                                                                                   
0 row(s) in 0.0260 seconds

hbase(main):006:0> 

  • 添加一个列族alter

hbase(main):002:0> alter 'student','id'			## 语法 alter '表名', '列族名'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 5.3610 seconds

hbase(main):003:0> 

  • 删除一个列族alter
hbase(main):003:0> alter 'student',{NAME=>'id',METHOD=> 'delete'}			##语法 alter '表名', {NAME=> '列族名', METHOD=> 'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.5240 seconds

hbase(main):004:0> 

  • 启用表enable和禁用表disable

通过enable和disable来启用/禁用这个表,相应的可以通过is_enabled和is_disabled来检查表是否被禁用

hbase(main):004:0> enable 'student'  ## 语法 enable '表名' 启用
0 row(s) in 0.0830 seconds

hbase(main):005:0> is_enabled 'student' ##is_enabled '表名'		检查是否启用
true                                                                                                                                                                       
0 row(s) in 0.0320 seconds

hbase(main):006:0> disable 'student'	##disable '表名'  禁用
0 row(s) in 2.4720 seconds

hbase(main):007:0> is_disabled 'student'		##is_disabled '表名' 检查是否禁用
true                                                                                                                                                                       
0 row(s) in 0.0530 seconds

hbase(main):008:0> 
  • 删除表drop

需要先禁用表,然后再删除表,启用的表是不允许删除的

hbase(main):010:0> disable 'student'		##语法 disable '表名'	先禁用表
0 row(s) in 2.4480 seconds

hbase(main):011:0> drop 'student'		## drop '表名'	再删除表
0 row(s) in 2.4620 seconds

hbase(main):012:0> list			##然后看到student表就消失了
TABLE                                                                                                                                                                      
0 row(s) in 0.0190 seconds

=> []
hbase(main):013:0> 

  • 插入或者修改数据put
# 创建表
create 'tbl_user', 'info', 'detail', 'address'

# 第一行数据
put 'tbl_user', 'mengday', 'info:id', '1'
put 'tbl_user', 'mengday', 'info:name', '张三'
put 'tbl_user', 'mengday', 'info:age', '28'

put 'tbl_user', 'mengday', 'detail:birthday', '1990-06-26'
put 'tbl_user', 'mengday', 'detail:email', 'abc@163.com'
put 'tbl_user', 'mengday', 'detail:create_time', '2019-03-04 14:26:10'

put 'tbl_user', 'mengday', 'address', '上海市'

# 第二行数据
put 'tbl_user', 'vbirdbest', 'info:id', '2'
put 'tbl_user', 'vbirdbest', 'info:name', '李四'
put 'tbl_user', 'vbirdbest', 'info:age', '27'

put 'tbl_user', 'vbirdbest', 'detail:birthday', '1990-06-27'
put 'tbl_user', 'vbirdbest', 'detail:email', 'xxx@gmail.com'
put 'tbl_user', 'vbirdbest', 'detail:create_time', '2019-03-05 14:26:10'

put 'tbl_user', 'vbirdbest', 'address', '北京市'

如图就是当前数据插入tbl_user状态
在这里插入图片描述

hbase(main):001:0> create 'tbl_user', 'info', 'detail', 'address'	##创建表
0 row(s) in 2.6940 seconds

=> Hbase::Table - tbl_user
hbase(main):002:0> put 'tbl_user', 'mengday', 'info:id', '1'		
### 语法
# 当列族中只有一个列时'列族名:列名'使用'列族名'
put '表名', '行键', '列族名', '列值'
put '表名', '行键', '列族名:列名', '列值'
0 row(s) in 0.2630 seconds

hbase(main):003:0> 

...........

  • 全表扫描scan
hbase(main):002:0> scan 'tbl_user'			##在这里我插入三条数据 语法 scan '表名'
ROW                                         COLUMN+CELL                                                                                                                    
 mengday                                    column=info:age, timestamp=1584033243936, value=28                                                                             
 mengday                                    column=info:id, timestamp=1584032107299, value=1                                                                               
 mengday                                    column=info:name, timestamp=1584033240116, value=\xE5\xBC\xA0\xE4\xB8\x89                                                      
1 row(s) in 0.2760 seconds

hbase(main):003:0> 
  • 扫描整个列簇
# 语法
scan '表名', {COLUMN=>'列族名'}

# 示例
scan 'tbl_user', {COLUMN=>'info'}
  • 扫描整个列簇的某个列
# 语法
scan '表名', {COLUMN=>'列族名:列名'}

# 示例
scan 'tbl_user', {COLUMN=>'info:age'}
  • 获取数据get
hbase(main):003:0> get 'tbl_user', 'mengday'	### 语法get '表名', '行键'
COLUMN                                      CELL                                                                                                                           
 info:age                                   timestamp=1584033243936, value=28                                                                                              
 info:id                                    timestamp=1584032107299, value=1                                                                                               
 info:name                                  timestamp=1584033240116, value=\xE5\xBC\xA0\xE4\xB8\x89                                                                        
1 row(s) in 0.1140 seconds

hbase(main):004:0> 

  • 根据某一行某列族的数据
# 语法
get '表名', '行键', '列族名'

# 示例
get 'tbl_user', 'mengday', 'info'
hbase(main):004:0> get 'tbl_user', 'mengday', 'info'
COLUMN                                      CELL                                                                                                                           
 info:age                                   timestamp=1584033243936, value=28                                                                                              
 info:id                                    timestamp=1584032107299, value=1                                                                                               
 info:name                                  timestamp=1584033240116, value=\xE5\xBC\xA0\xE4\xB8\x89                                                                        
1 row(s) in 0.0490 seconds

hbase(main):005:0> 
  • 清空整个表的数据truncate
hbase(main):005:0> truncate 'tbl_user'		##truncate '表名'
Truncating 'tbl_user' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 6.2550 seconds

hbase(main):006:0> scan 'tbl_user'		##全表扫描
ROW                                         COLUMN+CELL                                                                                                                    
0 row(s) in 0.2210 seconds

hbase(main):007:0> 

  • 查询表中有多少行count
hbase(main):007:0> count 'tbl_user'		##	count '表名'
0 row(s) in 0.1170 seconds

=> 0
hbase(main):008:0> 

  • LIMIT 返回的行数
# 语法
scan '表名', { LIMIT => 行数}

# 示例
scan 'tbl_user', { LIMIT => 数字 }
  • 删除某个列族中的某个列delete
hbase(main):008:0> create 'tbl_test', 'columnFamily1'
0 row(s) in 2.4180 seconds

=> Hbase::Table - tbl_test
hbase(main):009:0> put 'tbl_test', 'rowKey1', 'columnFamily1:column1', 'value1'
0 row(s) in 0.1850 seconds
## 语法	delete '表名', '行键', '列族名:列名'
hbase(main):010:0> put 'tbl_test', 'rowKey1', 'columnFamily1:column2', 'value2'
0 row(s) in 0.0150 seconds

hbase(main):011:0> delete 'tbl_test', 'rowKey1', 'columnFamily1:column1'
0 row(s) in 0.0760 seconds

hbase(main):012:0> delete 'tbl_test', 'rowKey1', 'columnFamily1:column2'
0 row(s) in 0.1040 seconds

hbase(main):013:0> 

  • 删除某行数据deleteall
# 语法
deleteall '表名', '行键'

# 示例
deleteall 'tbl_test', 'rowKey1'

参考文献
https://blog.csdn.net/vbirdbest/article/details/88236575

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值