绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Hbase shell 常用命令1
2020-05-18 16:14:13

下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下:

名称

命令表达式

创建表

create '表名称', '列名称1','列名称2','列名称N'

添加记录      

put '表名称', '行名称', '列名称:', '值'

查看记录

get '表名称', '行名称'

查看表中的记录总数

count  '表名称'

删除记录

delete  '表名' ,'行名称' , '列名称'

删除一张表

先要屏蔽该表,才能对该表进行删除,步 disable '表名称' 第二步  drop '表名称'

查看所有记录

scan "表名称"  

查看某个表某个列中所有数据

scan "表名称" , ['列名称:']

更新记录 

就是重写一遍进行覆盖

下面是一些常见命令的说明,在hbaseshell中输入help的帮助信息,在本文中,我们先介绍前3个,后面2个,将在下一篇博文中介绍。

点击(此处)折叠或打开

  1. 输入help,查看命令帮助
  2. hbase(main):001:> help
  3. HBase Shell, version 1.2., r25b281972df2f5b15c426c8963cbf77dd853a5ad, Thu Feb 18 23:01:49 CST 2016
  4. Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
  5. Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.

  6. COMMAND GROUPS:
  7.   Group name: general
  8.   Commands: status, table_help, version, whoami

  9.   Group name: ddl
  10.   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

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

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

  15.   Group name: tools
  16.   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, trace, unassign, wal_roll, zk_dump

  17.   Group name: replication
  18.   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

  19.   Group name: snapshots
  20.   Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, list_snapshots, restore_snapshot, snapshot

  21.   Group name: configuration
  22.   Commands: update_all_config, update_config

  23.   Group name: quotas
  24.   Commands: list_quotas, set_quota

  25.   Group name: security
  26.   Commands: grant, list_security_capabilities, revoke, user_permission

  27.   Group name: procedures
  28.   Commands: abort_procedure, list_procedures

  29.   Group name: visibility labels
  30.   Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility

  31. SHELL USAGE:
  32. Quote all names in HBase Shell such as table and column names. Commas delimit
  33. command parameters. Type <RETURN> after entering a command to run it.
  34. Dictionaries of configuration used in the creation and alteration of tables are
  35. Ruby Hashes. They look like this:

  36.   {'key1' => 'value1', 'key2' => 'value2', ...}

  37. and are opened and closed with curley-braces. Key/values are delimited by the
  38. '=>' character combination. Usually keys are predefined constants such as
  39. NAME, VERSIONS, COMPRESSION, etc. Constants do not need to be quoted. Type
  40. 'Object.constants' to see a (messy) list of all constants in the environment.

  41. If you are using binary keys or values and need to enter them in the shell, use
  42. double-quote'd hexadecimal representation. For example:

  43.   hbase> get 't1', "key\x03\x3f\xcd"
  44.   hbase> get 't1', "key\003\023\011"
  45.   hbase> put 't1', "test\xef\xff", 'f1:

一、一般操作

1.查询服务器状态

hbase(main):024:0>status

3 servers, 0 dead,1.0000 average load

2.查询hbase版本

hbase(main):025:0>version

0.90.4, r1150278,Sun Jul 24 15:53:29 PDT 2011

二、DDL操作

1.创建一个表

hbase(main):011:0>create 'member','member_id','address','info'   

0 row(s) in 1.2210seconds

2.获得表的描述

hbase(main):012:0>list

TABLE                                                                                                                                                       

member                                                                                                                                                      

1 row(s) in 0.0160seconds

hbase(main):006:0>describe 'member'

DESCRIPTION                                                                                          ENABLED                                               

 {NAME => 'member', FAMILIES => [{NAME=> 'address', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', true                                                 

  VERSIONS => '3', COMPRESSION => 'NONE',TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'fa                                                       

 lse', BLOCKCACHE => 'true'}, {NAME =>'info', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSI                                                       

 ONS => '3', COMPRESSION => 'NONE', TTL=> '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'false',                                                        

 BLOCKCACHE => 'true'}]}                                                                                                                                    

1 row(s) in 0.0230seconds

3.删除一个列族,alter,disable,enable

我们之前建了3个列族,但是发现member_id这个列族是多余的,因为他就是主键,所以我们要将其删除。

hbase(main):003:0>alter 'member',{NAME=>'member_id',METHOD=>'delete'}

ERROR: Table memberis enabled. Disable it first before altering.

报错,删除列族的时候必须先将表给disable掉。

hbase(main):004:0>disable 'member'                                  

0 row(s) in 2.0390seconds

hbase(main):005:0>alter'member',{NAME=>'member_id',METHOD=>'delete'}

0 row(s) in 0.0560seconds

hbase(main):006:0>describe 'member'

DESCRIPTION                                                                                          ENABLED                                               

 {NAME => 'member', FAMILIES => [{NAME=> 'address', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0',false                                                 

  VERSIONS => '3', COMPRESSION => 'NONE',TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'fa                                                       

 lse', BLOCKCACHE => 'true'}, {NAME =>'info', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSI                                                       

 ONS => '3', COMPRESSION => 'NONE', TTL=> '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'false',                                                        

 BLOCKCACHE => 'true'}]}                                                                                                                                    

1 row(s) in 0.0230seconds

该列族已经删除,我们继续将表enable

hbase(main):008:0> enable 'member'  

0 row(s) in 2.0420seconds

 

4.列出所有的表

hbase(main):028:0>list

TABLE                                                                                                                                                       

member                                                                                                                                                      

temp_table                                                                                                                                                  

2 row(s) in 0.0150seconds

5.drop一个表

hbase(main):029:0>disable 'temp_table'

0 row(s) in 2.0590seconds

hbase(main):030:0>drop 'temp_table'

0 row(s) in 1.1070seconds


6.查询表是否存在

hbase(main):021:0>exists 'member'

Table member doesexist                                                                                                                                     

0 row(s) in 0.1610seconds

 

7.判断表是否enable

hbase(main):034:0>is_enabled 'member'

true                                                                                                                                                        

0 row(s) in 0.0110seconds

 

8.判断表是否disable

hbase(main):032:0>is_disabled 'member'

false                                                                                                                                                       

0 row(s) in 0.0110seconds


三、DML操作


1.插入几条记录

put'member','scutshuxue','info:age','24'

put'member','scutshuxue','info:birthday','1987-06-17'

put'member','scutshuxue','info:company','alibaba'

put'member','scutshuxue','address:contry','china'

put'member','scutshuxue','address:province','zhejiang'

put'member','scutshuxue','address:city','hangzhou'

put'member','xiaofeng','info:birthday','1987-4-17'

put'member','xiaofeng','info:favorite','movie' 

put'member','xiaofeng','info:company','alibaba'

put'member','xiaofeng','address:contry','china'

put'member','xiaofeng','address:province','guangdong'

put'member','xiaofeng','address:city','jieyang'

put'member','xiaofeng','address:town','xianqiao'

 

2.获取一条数据

获取一个id的所有数据

hbase(main):001:0>get 'member','scutshuxue'

COLUMN                                   CELL                                                                                                               

 address:city                           timestamp=1321586240244, value=hangzhou                                                                            

 address:contry                         timestamp=1321586239126, value=china                                                                               

 address:province                       timestamp=1321586239197, value=zhejiang                                                                            

 info:age                               timestamp=1321586238965, value=24                                                                                  

 info:birthday                          timestamp=1321586239015, value=1987-06-17                                                                          

 info:company                           timestamp=1321586239071, value=alibaba                                                                             

6 row(s) in 0.4720seconds

 

获取一个id,一个列族的所有数据

hbase(main):002:0>get 'member','scutshuxue','info'

COLUMN                                   CELL                                                                                                               

 info:age                               timestamp=1321586238965, value=24                                                                                  

 info:birthday                          timestamp=1321586239015, value=1987-06-17                                                                          

 info:company                           timestamp=1321586239071, value=alibaba                                                                             

3 row(s) in 0.0210seconds

 

获取一个id,一个列族中一个列的所有数据

hbase(main):002:0>get 'member','scutshuxue','info:age' 

COLUMN                                   CELL                                                                                                               

 info:age                               timestamp=1321586238965, value=24                                                                                  

1 row(s) in 0.0320seconds

 

6.更新一条记录

将scutshuxue的年龄改成99

hbase(main):004:0>put 'member','scutshuxue','info:age' ,'99'

0 row(s) in 0.0210seconds

 

hbase(main):005:0>get 'member','scutshuxue','info:age' 

COLUMN                                   CELL                                                                                                               

 info:age                               timestamp=1321586571843, value=99                                                                                  

1 row(s) in 0.0180seconds

 

3.通过timestamp来获取两个版本的数据

hbase(main):010:0>get 'member','scutshuxue',{COLUMN=>'info:age',TIMESTAMP=>1321586238965}

COLUMN                                   CELL                                                                                                               

 info:age                               timestamp=1321586238965, value=24                                                                                  

1 row(s) in 0.0140seconds

 

hbase(main):011:0>get 'member','scutshuxue',{COLUMN=>'info:age',TIMESTAMP=>1321586571843}

COLUMN                                   CELL                                                                                                               

 info:age                               timestamp=1321586571843, value=99                                                                                  

1 row(s) in 0.0180seconds

 

4.全表扫描:

hbase(main):013:0>scan 'member'

ROW                                     COLUMN+CELL                                                                                                        

 scutshuxue                             column=address:city, timestamp=1321586240244, value=hangzhou                                                       

 scutshuxue                             column=address:contry, timestamp=1321586239126, value=china                                                        

 scutshuxue                             column=address:province, timestamp=1321586239197, value=zhejiang                                                   

 scutshuxue                              column=info:age,timestamp=1321586571843, value=99                                                                 

 scutshuxue                             column=info:birthday, timestamp=1321586239015, value=1987-06-17                                                    

 scutshuxue                             column=info:company, timestamp=1321586239071, value=alibaba                                                        

 temp                                   column=info:age, timestamp=1321589609775, value=59                                                                 

 xiaofeng                               column=address:city, timestamp=1321586248400, value=jieyang                                                        

 xiaofeng                               column=address:contry, timestamp=1321586248316, value=china                                                        

 xiaofeng                               column=address:province, timestamp=1321586248355, value=guangdong                                                  

 xiaofeng                               column=address:town, timestamp=1321586249564, value=xianqiao                                                       

 xiaofeng                               column=info:birthday, timestamp=1321586248202, value=1987-4-17                                                     

 xiaofeng                               column=info:company, timestamp=1321586248277, value=alibaba                                                        

 xiaofeng                               column=info:favorite, timestamp=1321586248241, value=movie                                                         

3 row(s) in 0.0570seconds

5.删除id为temp的值的‘info:age’字段

hbase(main):016:0>delete 'member','temp','info:age'

0 row(s) in 0.0150seconds

hbase(main):018:0>get 'member','temp'

COLUMN                                   CELL                                                                                                               

0 row(s) in 0.0150seconds

 

6.删除整行

hbase(main):001:0>deleteall 'member','xiaofeng'

0 row(s) in 0.3990seconds

 

7.查询表中有多少行:

hbase(main):019:0>count 'member'                                        

2 row(s) in 0.0160seconds

 

8.给‘xiaofeng’这个id增加'info:age'字段,并使用counter实现递增

hbase(main):057:0*incr 'member','xiaofeng','info:age'                    

COUNTER VALUE = 1

 

hbase(main):058:0>get 'member','xiaofeng','info:age' 

COLUMN                                   CELL                                                                                                               

 info:age                               timestamp=1321590997648, value=\x00\x00\x00\x00\x00\x00\x00\x01                                                    

1 row(s) in 0.0140seconds

 

hbase(main):059:0>incr 'member','xiaofeng','info:age'

COUNTER VALUE = 2

 

hbase(main):060:0>get 'member','xiaofeng','info:age' 

COLUMN                                   CELL                                                                                                               

 info:age                               timestamp=1321591025110, value=\x00\x00\x00\x00\x00\x00\x00\x02                                                    

1 row(s) in 0.0160seconds

 

获取当前count的值

hbase(main):069:0>get_counter 'member','xiaofeng','info:age' 

COUNTER VALUE = 2

  

9.将整张表清空:

hbase(main):035:0>truncate 'member'

Truncating 'member'table (it may take a while):

 - Disabling table...

 - Dropping table...

 - Creating table...

0 row(s) in 4.3430seconds

可以看出,hbase是先将掉disable掉,然后drop掉后重建表来实现truncate的功能的。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30089851/viewspace-2064934/,如需转载,请注明出处,否则将追究法律责任。

分享好友

分享这个小栈给你的朋友们,一起进步吧。

Hbase资料库
创建时间:2020-05-08 14:42:11
Hbase资料库一站式查询。
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 小雨滴
    专家
  • Leila
    专家
  • 飘絮絮絮丶
    专家
戳我,来吐槽~