• HBase Shell 的基本操作


    概述

    前面写了一些 Hadoop, Zookeeper 及 Hbase 分布式环境搭建的文章。或许你在搭建的过程中遇到了一些不如意的事情,但我相信总是可以解决的。如果你已经完成了环境的搭建,那么就可以尽情玩耍了。
    本文就先来玩玩 HBase Shell。


    版权说明

    著作权归作者所有。
    商业转载请联系作者获得授权,非商业转载请注明出处。
    本文作者:Q-WHai
    发表日期: 2016年6月12日
    本文链接:http://blog.csdn.net/lemon_tree12138/article/details/51646388
    来源:CSDN
    更多内容:分类 >> 大数据之 Hadoop


    Shell

    login shell

    想要玩 shell,首先你得登录进去。登录很简单

    $ hbase shell
    2016-06-11 20:05:29,095 INFO  [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available
    HBase Shell; enter 'help<RETURN>' for list of supported commands.
    Type "exit<RETURN>" to leave the HBase Shell
    Version 0.96.2-hadoop2, r1581096, Mon Mar 24 16:03:18 PDT 2014
    
    hbase(main):001:0>

    这样就进入了 hbase 的 shell 环境了。

    通用命令

    序号 命令 描述
    1 status 提供 HBase 的状态,例如,服务器的数量
    2 version 提供正在使用 HBase 版本
    3 table_help 表引用命令提供帮助
    4 whoami 提供有关用户的信息

    check status

    查询服务器状态

    hbase(main):001:0> status
    SLF4J: Class path contains multiple SLF4J bindings.
    SLF4J: Found binding in [jar:file:/home/hadoop/hbase/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: Found binding in [jar:file:/home/hadoop/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.
    3 servers, 0 dead, 0.6667 average load

    这里显示了有三台服务器,没有服务器挂掉。

    version

    查询服务器版本

    hbase(main):002:0> version
    0.96.2-hadoop2, r1581096, Mon Mar 24 16:03:18 PDT 2014

    whoami

    hbase(main):040:0> whoami
    hadoop (auth:SIMPLE)

    create table

    现在我们来创建一张数据表,比如数据表 student,字段有: student_id, name, sex, age. 那么我们可以这样来写创建语句:
    基本格式:create ‘表名称’, ‘列名称1’,’列名称2’,…,’列名称N’

    hbase(main):003:0> create 'student','student_id','name','sex','age'
    0 row(s) in 5.5210 seconds
    
    => Hbase::Table - student

    show table

    对于刚刚创建的表,我们可以这样来查询是否已经创建成功。list 命令有点类似于 MySQL 中的 show.

    hbase(main):004:0> list
    TABLE
    student
    1 row(s) in 0.3350 seconds
    
    => ["student"]

    但是如果你的数据表太多,用肉眼去扫描显然是一件吃力的事情,这时你可以使用 exists 命令直接去判断这个数据表是否存在。这里我用两个数据表进行测试,一个是已经存在的 student,还有一个就是并不存在的 student1,这样就可以进行对比了。

    hbase(main):006:0> exists 'student'
    Table student does exist
    0 row(s) in 0.1580 seconds
    hbase(main):007:0> exists 'student1'
    Table student1 does not exist
    0 row(s) in 0.0430 seconds

    而如果想要查看具体表的详情,可以这样来操作。其实在 MySQL 中也有类似的操作,如果你还记得的话。(偷偷告诉你,是 desc table_name;

    hbase(main):005:0> describe 'student'
    DESCRIPTION                                                                                                                                              ENABLED
     'student', {NAME => 'age', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', T true
     TL => '2147483647', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}, {NAME => 'name',
      BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => '2147483647', COMPRE
     SSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}, {NAME => 'sex', BLOOMFILTER => 'ROW', VERS
     IONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => '2147483647', COMPRESSION => 'NONE', MIN_VERSIO
     NS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}, {NAME => 'student_id', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMO
     RY => 'false', KEEP_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => '2147483647', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACH
     E => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
    1 row(s) in 0.3310 seconds

    insert data

    现在就可以进行添加数据。

    hbase(main):008:0> put 'student','1001','name','Bob'
    0 row(s) in 0.3850 seconds
    
    hbase(main):009:0> put 'student','1001','sex','Male'
    0 row(s) in 0.0960 seconds
    
    hbase(main):010:0> put 'student','1001','age','18'
    0 row(s) in 0.0500 seconds

    上面的这些操作是向 student 表中的学号为 1001 的行添加了 { name=Bob;sex=Male;age=18 } 的记录。

    select data

    我们可以根据学号查询某一个学号下的所有信息,如下:

    hbase(main):012:0> get 'student','1001'
    COLUMN                                                       CELL
     age:                                                        timestamp=1465701894640, value=18
     name:                                                       timestamp=1465701805984, value=Bob
     sex:                                                        timestamp=1465701881247, value=Male
    3 row(s) in 0.0690 seconds

    也可以根据学号及列族查询,如下:

    hbase(main):013:0> get 'student','1001','name'
    COLUMN                                                       CELL
     name:                                                       timestamp=1465701805984, value=Bob
    1 row(s) in 0.0100 seconds

    如果你想扫描整个数据表(虽然不建议,但是如果你想),你可以使用 scan.

    hbase(main):024:0> scan 'student'
    ROW                                                          COLUMN+CELL
     1001                                                        column=age:, timestamp=1465702655984, value=19
     1001                                                        column=name:, timestamp=1465701805984, value=Bob
     1001                                                        column=sex:, timestamp=1465701881247, value=Male
     1002                                                        column=age:, timestamp=1465703066425, value=17
     1002                                                        column=name:, timestamp=1465703048791, value=Alice
     1002                                                        column=sex:, timestamp=1465703060804, value=female
    2 row(s) in 0.0400 seconds

    查询数据表有多少行,请使用 count 命令。

    hbase(main):027:0> count 'student'
    2 row(s) in 0.0650 seconds
    
    => 2

    update data

    在学习 HBase 的一开始我们就知道了,Hbase 中的 update 其实是一个伪 update 操作。因为所有的 update 都是在 put 一条新的记录。

    hbase(main):014:0> put 'student','1001','age','19'
    0 row(s) in 0.0280 seconds
    hbase(main):015:0> get 'student','1001'
    COLUMN                                                       CELL
     age:                                                        timestamp=1465702655984, value=19
     name:                                                       timestamp=1465701805984, value=Bob
     sex:                                                        timestamp=1465701881247, value=Male
    3 row(s) in 0.0380 seconds

    如果你怀疑这种说法的可靠性,你可以进行验证,验证的方法就查找两个不同版本的数据。

    hbase(main):016:0> get 'student','1001',{COLUMN=>'age',TIMESTAMP=>1465701894640}
    COLUMN                                                       CELL
     age:                                                        timestamp=1465701894640, value=18
    1 row(s) in 0.0460 seconds
    hbase(main):017:0> get 'student','1001',{COLUMN=>'age',TIMESTAMP=>1465702655984}
    COLUMN                                                       CELL
     age:                                                        timestamp=1465702655984, value=19
    1 row(s) in 0.0180 seconds

    delete data

    删除某一个字段

    hbase(main):025:0> delete 'student','1001','sex'
    0 row(s) in 0.5230 seconds

    而在验证的时候,sex 这个列没有被打印出来。这就说明删除成功了。

    hbase(main):026:0> get 'student','1001'
    COLUMN                                                       CELL
     age:                                                        timestamp=1465702655984, value=19
     name:                                                       timestamp=1465701805984, value=Bob
    2 row(s) in 0.0410 seconds

    根据学号删除一整行数据
    如果你想删除学号为 1001 这一整行数据,这其实是一件麻烦的事情,因为你需要依次“删除” 1001 下的所有字段。当所有字段的数据被“删除”干净的时候,这一行的数据才会被“删除”。

    hbase(main):025:0> delete 'student','1001','sex'
    0 row(s) in 0.5230 seconds
    
    hbase(main):030:0> delete 'student','1001','age'
    0 row(s) in 0.0210 seconds
    
    hbase(main):031:0> delete 'student','1001','name'
    0 row(s) in 0.0100 seconds
    
    hbase(main):032:0> get 'student','1001'
    COLUMN                                                       CELL
    0 row(s) in 0.0080 second

    清空数据表
    清空表的操作跟 MySQL 中的清空很像。都是使用 truncate 命令。不同的是清空的过程。

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

    在上面打印出来的信息中可以看出,清空的三个关键步骤:disable、drop、create.
    这也是由于 hbase 不能修改数据的造成的。下面进行验证:

    hbase(main):036:0> scan 'student'
    ROW                                                          COLUMN+CELL
    0 row(s) in 0.0150 seconds

    attributes

    这一节是说明一下 HBase 数据库属性说明及其设置。
    由于之前清空了数据表,这里我们重新创建一个表 staff。字段为 ‘name’,’number’,’info’。

    序号 命令 描述
    1 disable 禁用表
    2 is_disabled 验证表是否被禁用
    3 enable 启用一个表
    4 is_enabled 验证表是否已启用
    5 alter 改变一个表
    6 exists 验证表是否存在

    is_enabled & is_disabled

    hbase(main):002:0> is_enabled 'staff'
    true
    0 row(s) in 0.6660 seconds
    
    hbase(main):003:0> is_disabled 'staff'
    false
    0 row(s) in 0.1190 seconds

    disable & enable

    hbase(main):004:0> disable 'staff'
    0 row(s) in 1.6220 seconds

    这样我们就设置了 ‘staff’ 表被禁用了,验证信息如下:

    hbase(main):006:0> is_enabled 'staff'
    false
    0 row(s) in 0.0750 seconds
    
    hbase(main):007:0> is_disabled 'staff'
    true
    0 row(s) in 0.0350 seconds

    现在我们在 ‘staff’ 表被禁用的情况下对表进行一个 put 操作,就会出现异常状况。而且从异常信息可以看出,这里并没有说明是由于表被禁用引起的异常,所以在以后的代码编写中这一点需要注意一下。

    hbase(main):008:0> put 'staff','John','number','13812345879'
    
    ERROR: Failed 1 action: NotServingRegionException: 1 time,
    
    Here is some help for this command:
    Put a cell 'value' at specified table/row/column and optionally
    timestamp coordinates.  To put a cell value into table 'ns1:t1' or 't1'
    at row 'r1' under column 'c1' marked with the time 'ts1', do:
    
      hbase> put 'ns1:t1', 'r1', 'c1', 'value', ts1
    
    The same commands also can be run on a table reference. Suppose you had a reference
    t to table 't1', the corresponding command would be:
    
      hbase> t.put 'r1', 'c1', 'value', ts1

    我们再来启用 ‘staff’ 表,再进行上面的测试试一下。

    hbase(main):009:0> enable 'staff'
    0 row(s) in 1.1610 seconds
    hbase(main):010:0> put 'staff','John','number','13812345879'
    0 row(s) in 0.0210 seconds

    alter

    设置单元的最大数目

    hbase(main):003:0> alter 'staff', NAME => 'info', VERSIONS => 5
    Updating all regions with the new schema...
    0/1 regions updated.
    1/1 regions updated.
    Done.
    0 row(s) in 2.4370 seconds

    设置只读

    hbase(main):010:0> alter 'staff', READONLY
    Updating all regions with the new schema...
    0/1 regions updated.
    1/1 regions updated.
    Done.
    0 row(s) in 2.5380 seconds

    exit

    退出 shell 就使用 exit 就 ok 了。

    hbase(main):015:0> exit

    Ref

  • 相关阅读:
    作业 20180918-2 每周例行报告
    将Python文件打包为exe文件,并在控制台运行之简易教程
    作业20181011-1 每周例行报告
    用WebView加载本地图片的方法
    ios webview自适应实际内容高度4种方法
    UIWebView加载本地网页与图片的方法
    nonatomic与atomic的区别与作用
    @dynamic与@synthesize的区别与用法
    xib与storyboard的区别
    iOS与H5的交互
  • 原文地址:https://www.cnblogs.com/fengju/p/6335981.html
Copyright © 2020-2023  润新知