HBase Shell

来源:互联网 发布:js 正则表达式 编辑:程序博客网 时间:2024/04/27 23:39

当HBase集群配置好启动后,就可以使用HBase的Shell对HBase进行各种操作,如建表、添加记录、添加列族和删除表等。需使用HBase的bin目录下的HBase命令才能进入,具体操作如下:

进入HBase

[gznc@master ~]$ cd /home/gznc/hbase-0.98.7-hadoop2[gznc@master hbase-0.98.7-hadoop2]$ bin/hbase shell2016-11-01 19:40:21,367 INFO  [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.availableHBase Shell; enter 'help<RETURN>' for list of supported commands.Type "exit<RETURN>" to leave the HBase ShellVersion 0.98.7-hadoop2, r800c23e2207aa3f9bddb7e9514d8340bcfb89277, Wed Oct  8 15:58:11 PDT 2014hbase(main):001:0> 

帮助命令help

hbase(main):001:0> helpHBase Shell, version 0.98.7-hadoop2, r800c23e2207aa3f9bddb7e9514d8340bcfb89277, Wed Oct  8 15:58:11 PDT 2014Type '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, 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, incr, put, scan, truncate, truncate_preserve  Group name: tools  Commands: assign, balance_switch, balancer, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, flush, hlog_roll, major_compact, merge_region, move, split, trace, unassign, zk_dump  Group name: replication  Commands: add_peer, disable_peer, enable_peer, list_peers, list_replicated_tables, remove_peer, set_peer_tableCFs, show_peer_tableCFs  Group name: snapshots  Commands: clone_snapshot, delete_snapshot, list_snapshots, rename_snapshot, restore_snapshot, snapshot  Group name: security  Commands: grant, revoke, user_permission  Group name: visibility labels  Commands: add_labels, clear_auths, get_auths, set_auths, set_visibilitySHELL USAGE:Quote all names in HBase Shell such as table and column names.  Commas delimitcommand parameters.  Type <RETURN> after entering a command to run it.Dictionaries of configuration used in the creation and alteration of tables areRuby 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 asNAME, 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, usedouble-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/docs/current/book.htmlhbase(main):002:0> 

查看HBaseRegionServer运行状态

hbase(main):002:0> statusSLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/home/gznc/hbase-0.98.7-hadoop2/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/home/gznc/hadoop-2.5.2/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.2016-11-01 19:44:41,122 WARN  [main] util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable3 servers, 0 dead, 0.6667 average loadhbase(main):003:0> 

查看HBase版本

hbase(main):004:0> version0.98.7-hadoop2, r800c23e2207aa3f9bddb7e9514d8340bcfb89277, Wed Oct  8 15:58:11 PDT 2014

查看当前用户

hbase(main):005:0> whoamigznc (auth:SIMPLE)    groups: gznc

Hbase数据库操作命令
创建表

行健 basic_info school_info name gender birthday connect address college class subject

该表有两个列族,分别是basic_info和school_info,分别用来存储基本信息和学校信息。basic_info包含5个列name、gender、birthday、connect、address,分别代表姓名、性别、出生年月、联系方式、住址;school_info包含三个列college、class、subject,分别代表所属学院、班级、专业。

创建表students,格式如下:
create ‘表名’ ‘列族1’,’列族2’,…..‘列族n’

hbase(main):008:0> create 'students' ,'stu_id','basic_info','school_info'0 row(s) in 1.0970 seconds=> Hbase::Table - students

查看所有表

hbase(main):009:0> listTABLE                                                                           students                                                                        1 row(s) in 0.0660 seconds=> ["students"]

查看表结构

hbase(main):003:0> describe 'students'DESCRIPTION                                          ENABLED                     'students', {NAME => 'basic_info', DATA_BLOCK_ENCOD true                        ING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SC                             OPE => '0', VERSIONS => '1', COMPRESSION => 'NONE',                              MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETE                             D_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY                              => 'false', BLOCKCACHE => 'true'}, {NAME => 'schoo                             l_info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER                              => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1                             ', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL                              => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKS                             IZE => '65536', IN_MEMORY => 'false', BLOCKCACHE =>                              'true'}, {NAME => 'stu_id', DATA_BLOCK_ENCODING =>                              'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE =>                              '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_V                             ERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELL                             S => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'f                             alse', BLOCKCACHE => 'true'}                                                   1 row(s) in 0.2400 seconds

删除列族stu_id

删除列族要使用alter命令,用alter命令删除列族的格式有如下几种:
alter ‘表名’ , NAME=>’列族名’,METHOD=>’delete’
alter ‘表名’ , {NAME=>’列族名’,METHOD=>’delete’}
alter ‘表名’ , ‘delete’=> ‘列族名’

hbase(main):005:0> alter 'students','delete'=>'stu_id'Updating all regions with the new schema...0/1 regions updated.1/1 regions updated.Done.0 row(s) in 2.4690 seconds

此版本的HBase中,删除一个表的列族不需要使表在非使用的状态可以直接进行操作,在之前的某些版本中则不行,直接删除会出现以下错误

ERROR:Table memberis enable.Disable it first before altering

这时需要先disable要操作的表,删除后,在进行enable操作,格式为disable/enable ‘表名’

注意:ddl命令是区分大小写的,像ddl中的alter,create, drop, enable等都必需用小写。而{}中的属性名都必需用大写。
查看列族stu_id删除是否成功

hbase(main):006:0> describe 'students'DESCRIPTION                                          ENABLED                     'students', {NAME => 'basic_info', DATA_BLOCK_ENCOD true                        ING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SC                             OPE => '0', VERSIONS => '1', COMPRESSION => 'NONE',                              MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETE                             D_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY                              => 'false', BLOCKCACHE => 'true'}, {NAME => 'schoo                             l_info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER                              => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1                             ', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL                              => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKS                             IZE => '65536', IN_MEMORY => 'false', BLOCKCACHE =>                              'true'}                                                                       1 row(s) in 0.1090 seconds

删除一个表
删除一个表必须先disable,否则会出现错误,删除表使用drop命令,格式为

hbase(main):011:0> create 't','s'0 row(s) in 0.5130 seconds=> Hbase::Table - thbase(main):012:0> listTABLE                                                                           students                                                                        t                                                                               2 row(s) in 0.0710 seconds=> ["students", "t"]hbase(main):014:0> disable 't'0 row(s) in 1.4960 secondshbase(main):015:0> drop 't'0 row(s) in 0.2660 seconds

查询一个表是否存在

hbase(main):016:0> exists 't'Table t does not exist                                                          0 row(s) in 0.0470 secondshbase(main):017:0> exists 'students'Table students does exist                                                       0 row(s) in 0.0750 seconds

查询一个表的使用状态

hbase(main):019:0> is_enabled 'students'true                                                                            0 row(s) in 0.0580 secondshbase(main):020:0> is_disabled 'students'false                                                                           0 row(s) in 0.0570 seconds

dml操作
dml操作主要是对表的记录的操作,如插入记录、查询记录等。
1.使用put命令向students表中插入记录,put命令格式如下:
put ‘表名’ ‘行键’,‘列族:列’,‘值’

hbase(main):021:0> stu = get_table 'students'0 row(s) in 0.0040 seconds=> Hbase::Table - studentshbase(main):022:0> stu.put '2016111','basic_info:name','YangMing'0 row(s) in 0.4390 secondshbase(main):023:0> stu.put '2016111','basic_info:gender','male'0 row(s) in 0.0130 secondshbase(main):024:0> stu.put '2016111','basic_info:birthday','1988-05-23'0 row(s) in 0.0260 secondshbase(main):025:0> stu.put '2016111','basic_info:connect','13911111111'0 row(s) in 0.0240 secondshbase(main):026:0> stu.put '2016111','basic_info:address','SiChuan-Chengdu'0 row(s) in 0.0440 secondshbase(main):027:0> stu.put '2016111','school_info:college','ChengXing'0 row(s) in 0.0340 secondshbase(main):028:0> stu.put '2016111','school_info:class','class 1 grade 2'0 row(s) in 0.0120 secondshbase(main):029:0> stu.put '2016111','school_info:object','Software'0 row(s) in 0.0130 seconds

注意:第一步创建了students表的引用stu,这样操作更加方便
2.使用get获取数据,get命令的格式如下:

get 表名’ , ‘行健’[, ‘列族[:列]’] 其中[ ] 为可选内容

hbase(main):033:0> get 'students','2016111'COLUMN                CELL                                                       basic_info:address   timestamp=1478006969468, value=SiChuan-Chengdu             basic_info:birthday  timestamp=1478006906499, value=1988-05-23                  basic_info:connect   timestamp=1478006935108, value=13911111111                 basic_info:gender    timestamp=1478006880018, value=male                        basic_info:name      timestamp=1478006860715, value=YangMing                    school_info:class    timestamp=1478007026804, value=class 1 grade 2             school_info:college  timestamp=1478007005614, value=ChengXing                   school_info:object   timestamp=1478007066226, value=Software                   8 row(s) in 0.1400 seconds

获取一个行键的所有数据:

hbase(main):034:0> stu.get '2016111'COLUMN                CELL                                                       basic_info:address   timestamp=1478006969468, value=SiChuan-Chengdu             basic_info:birthday  timestamp=1478006906499, value=1988-05-23                  basic_info:connect   timestamp=1478006935108, value=13911111111                 basic_info:gender    timestamp=1478006880018, value=male                        basic_info:name      timestamp=1478006860715, value=YangMing                    school_info:class    timestamp=1478007026804, value=class 1 grade 2             school_info:college  timestamp=1478007005614, value=ChengXing                   school_info:object   timestamp=1478007066226, value=Software                   8 row(s) in 0.0960 seconds

从以上输出可以看出,各列族数据是按列排好序的。
获取一个行键某列族的所有数据:

hbase(main):035:0> stu.get '2016111','basic_info'COLUMN                CELL                                                       basic_info:address   timestamp=1478006969468, value=SiChuan-Chengdu             basic_info:birthday  timestamp=1478006906499, value=1988-05-23                  basic_info:connect   timestamp=1478006935108, value=13911111111                 basic_info:gender    timestamp=1478006880018, value=male                        basic_info:name      timestamp=1478006860715, value=YangMing                   5 row(s) in 0.0380 seconds

获取一个行键某列族最新的数据

hbase(main):036:0> stu.get '2016111','basic_info:name'COLUMN                CELL                                                       basic_info:name      timestamp=1478006860715, value=YangMing                   1 row(s) in 0.0260 seconds

3.为某条数据增加一个版本

hbase(main):037:0> stu.put '2016111','basic_info:connect','Tel:13252367856'0 row(s) in 0.0250 secondshbase(main):038:0> stu.get '2016111','basic_info:connect'COLUMN                CELL                                                       basic_info:connect   timestamp=1478019142670, value=Tel:13252367856            1 row(s) in 0.0220 seconds

4.通过时间戳获取两个版本的数据
花括号里面的属性必须要大写

hbase(main):039:0> stu.get '2016111',{COLUMN=>'basic_info:connect',TIMESTAMP=>1478019142670}COLUMN                CELL                                                       basic_info:connect   timestamp=1478019142670, value=Tel:13252367856            1 row(s) in 0.0250 secondshbase(main):040:0> stu.get '2016111',{COLUMN=>'basic_info:connect',TIMESTAMP=>1478006935108}COLUMN                CELL                                                       basic_info:connect   timestamp=1478006935108, value=13911111111                1 row(s) in 0.0800 seconds

5.全表扫描

hbase(main):041:0> stu.scanROW                   COLUMN+CELL                                                2016111              column=basic_info:address, timestamp=1478006969468, value=                      SiChuan-Chengdu                                            2016111              column=basic_info:birthday, timestamp=1478006906499, value                      =1988-05-23                                                2016111              column=basic_info:connect, timestamp=1478019142670, value=                      Tel:13252367856                                            2016111              column=basic_info:gender, timestamp=1478006880018, value=m                      ale                                                        2016111              column=basic_info:name, timestamp=1478006860715, value=Yan                      gMing                                                      2016111              column=school_info:class, timestamp=1478007026804, value=c                      lass 1 grade 2                                             2016111              column=school_info:college, timestamp=1478007005614, value                      =ChengXing                                                 2016111              column=school_info:object, timestamp=1478007066226, value=                      Software                                                  1 row(s) in 0.0850 seconds

6.删除某行键类的某列

hbase(main):005:0> stu.delete '2016111','basic_info:connect'0 row(s) in 0.4540 secondshbase(main):006:0> stu.get '2016111','basic_info'COLUMN                CELL                                                       basic_info:address   timestamp=1478006969468, value=SiChuan-Chengdu             basic_info:birthday  timestamp=1478006906499, value=1988-05-23                  basic_info:gender    timestamp=1478006880018, value=male                        basic_info:name      timestamp=1478006860715, value=YangMing                   4 row(s) in 0.1330 seconds

7.以行键为单位,查询表有多少行

hbase(main):007:0> stu.count1 row(s) in 0.1070 seconds=> 1

8.清空整张表

hbase(main):008:0> truncate 'students'Truncating 'students' table (it may take a while): - Disabling table... - Dropping table... - Creating table...0 row(s) in 2.9440 seconds

注意:
truncate 操作其实是先disable某张表,然后删除表,在根据表的结构重新创建同名称的表

0 0
原创粉丝点击