HBase--Shell

  有段时间没更新了,今天就来个简单的吧–HBase的Shell。这个没什么技术含量,主要是帮助文档太强大了!可以说无需刻意记忆,help command就搞定了。那为什么还要写这篇博客呢?那就回到了第一句话“有段时间没更新了”来个简单的(●’◡’●)”!其实,不是啦,主要是用来提醒自己两件事情,一多看帮助文档和源码,二不要忽视简单的东西脚踏实地。(注:HBase版本0.98.8-hadoop2

HBase help

  • 进入Shell

    1
    2
    3
    4
    [root@xxo07 ~]# hbase shell
    HBase Shell; enter 'help<RETURN>' for list of supported commands.
    Type "exit<RETURN>" to leave the HBase Shell
    Version 0.98.8-hadoop2, r6cfc8d064754251365e070a10a82eb169956d5fe, Fri Nov 14 18:26:29 PST 2014
  • HBase help

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    hbase(main):001:0> hbase help
    HBase Shell, version 0.98.8-hadoop2, r6cfc8d064754251365e070a10axxo6d5fe, Fri Nov 14 18:26:29 PST 2014
    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.

    ################################## Shell 分组了,便于记忆 ########################################
    COMMAND GROUPS:
    Group name: general ##普通命令
    Commands: status, table_help, version, whoami

    Group name: ddl ##数据定义语言(data definition language) 不用解释 重点掌握
    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 ##这个命令空间命令相对table命令用的少,给table命令让步了然他加了后缀
    Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace,
    list_namespace_tables

    Group name: dml ##数据操作语言(Data Manipulation Language 不用解释 重点掌握
    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, compact_rs, 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 ##快照,和元数据相关 0.95之后默认开启snapshot功能,之前版本的需要手动开启
    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_visibility

    ################################# Shell 用法 #########################################
    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
    NoMethodError: undefined method `hbase' for #<Object:0x2b6b35f4>

其他命令

  • 其他就不在这里一一举例了,比如想查看careat的用法直接“ help 'create' ” 就 ok,如下图:
    help 'create'
    create Examples

举个例子

  • 以用户表user为例,如下图:
  1. 行键 row_key : uid(倒置) ;
  2. 列族 column_famliy : nm和Info
  3. 限定符(列族Info的限定符 ) : flos(关注) | fans(粉丝) | fw(鲜花)
  4. 表结构:如下
    一张用户表user
  • 操作
  1. 创建一个具有两个列族“nm”和“info”的表“user”,注意单引号和逗号。
    hbase(main):001:0> create ‘user’,’nm’,’info’

  2. list,查看当前 HBase 中具有哪些表。
    hbase(main):012:0> list

  3. describe 查看表构造。
    hbase(main):003:0> describe ‘user’
    Table user is ENABLED
    COLUMN FAMILIES DESCRIPTION
    {NAME => ‘info’, DATA_BLOCK_ENCODING => ‘NONE’, BLOOMFILTER => ‘ROW’, REPLICATION_SCOPE => ‘
    0’, VERSIONS => ‘1’, COMPRESSION => ‘NONE’, MIN_VERSIONS => ‘0’, TTL => ‘FOREVER’, KEEP_DELE
    TED_CELLS => ‘FALSE’, BLOCKSIZE => ‘65536’, IN_MEMORY => ‘false’, BLOCKCACHE => ‘true’}
    {NAME => ‘nm’, DATA_BLOCK_ENCODING => ‘NONE’, BLOOMFILTER => ‘ROW’, REPLICATION_SCOPE => ‘0’
    , VERSIONS => ‘1’, COMPRESSION => ‘NONE’, MIN_VERSIONS => ‘0’, TTL => ‘FOREVER’, KEEP_DELETE
    D_CELLS => ‘FALSE’, BLOCKSIZE => ‘65536’, IN_MEMORY => ‘false’, BLOCKCACHE => ‘true’}
    2 row(s) in 0.0750 seconds

  4. put 命令向表中插入数据,参数分别为表名、行名、列名和值
    a. 加入数据,第一行键为“25”,列族“nm”,值为 xiaoxiao。第二行略
    hbase(main):006:0> put ‘user’,’25’,’nm’,’xiaoxiao’
    hbase(main):007:0> put ‘user’,’71’,’nm’,’momo’
    hbase(main):010:0> put ‘user’,’25’,’info:flos’,’52’

  5. get 查询
    hbase(main):010:0> get ‘user’,’71’
    COLUMN CELL
    nm: timestamp=1466829836778, value=momo
    hbase(main):011:0> get ‘user’,’25’,’info:flos’
    COLUMN CELL
    info:flos timestamp=1466830066408, value=52

  6. scan 查询,这个比较强大
    hbase(main):012:0> scan ‘user’
    ROW COLUMN+CELL
    25 column=info:flos, timestamp=1466830066408, value=52
    25 column=nm:, timestamp=1466829764632, value=xiaoxiao
    71 column=nm:, timestamp=1466829836778, value=momo

  7. count 统计行数
    hbase(main):015:0> count ‘user’
    2 row(s) in 0.0530 seconds
    => 2

  8. delete 删除某条数据
    hbase(main):028:0> delete ‘user’,’72’,’info:fans’

  9. truncate 清空数据,谨慎使用
    hbase(main):029:0> truncate ‘user’

  10. drop 删除表,drop之前需要disbale table
    hbase(main):030:0> disable ‘user’
    hbase(main):032:0> drop ‘user’

  11. status 查看状态
    hbase(main):033:0> status
    1 servers, 0 dead, 4.0000 average load

  12. version 查看版本
    hbase(main):032:0> version
    0.98.8-hadoop2, r6cfc8d064754251365e070a10a82eb169956d5fe, Fri Nov 14 18:26:29 PST 2014

  13. exit 退出
    hbase(main):033:0> exit

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器