2、Shell控制

进入到shell命令行界面,执行hbase命令,并附加shell关键字:

    [grid@hdnode3 ~]$ hbase shell

    HBase Shell; enter ¨help<RETURN>¨ for list of supported commands.

    Type "exit<RETURN>" to leave the HBase Shell

    Version 0.90.5, r1212209, Fri Dec  9 05:40:36 UTC 2011

    hbase(main):001:0> 

虽然成功登录进去了,可是我们也不知道现在能做什么,也不了解SHELL下都有哪些命令。这个时候,我们可以选择,去看官方文档中的说明,或者,敲个help上去看看。

    hbase(main):002:0> help

    ..................

    ..................

    COMMAND GROUPS:

      Group name: general

      Commands: status, version

      Group name: ddl

      Commands: alter, create, describe, disable, drop, enable, exists, is_disabled, is_enabled, list

      Group name: dml

      Commands: count, delete, deleteall, get, get_counter, incr, put, scan, truncate

      Group name: tools

      Commands: assign, balance_switch, balancer, close_region, compact, flush, major_compact, move, split, unassign, zk_dump

      Group name: replication

      Commands: add_peer, disable_peer, enable_peer, remove_peer, start_replication, stop_replication

    ..................

    ..................

帮助信息果然有帮助,通过输出的信息,我们大致了解能够做什么。可以看到hbase中也是分有ddl/dml这类语句,此外还有与复制相关的,与管理相关的命令等等。

先来试试通用(general)命令,查询状态:

    hbase(main):003:0> status

    5 servers, 0 dead, 0.4000 average load

查询版本:

    hbase(main):004:0> version

    0.90.5, r1212209, Fri Dec  9 05:40:36 UTC 2011

接下来重点项,DDL和DML(想不到HBase也分了DML/DDL语句)。HBase中没有库的概念,做为BigTable的山寨产品,尽管没名山寨到名字,但山寨到了精髓,从设计上来说,它也不需要分库,甚至不需要分表,所有数据放到同一张表中也是可以的,这就是真正的BigTable嘛。

创建表对象:

    hbase(main):005:0> create ¨t¨,¨t_id¨,¨t_vl¨

    0 row(s) in 2.3490 seconds

HBase中创建对象的语法比较灵活,前面这个示例是简写法,其功能等效于完整写法,"hbase> create ¨t¨, {NAME => ¨t_id¨}, {NAME => ¨t_vl¨}",第一个参数用于指定表名,后面跟的所有参数都是列族的名称。每个表的列族需要在表创建时定义好(尽管后期也可以修改,但最好一开始就定义好),从这个角度来看,HBase中的对象是结构化的。

查看表对象:

    hbase(main):006:0> list

    TABLE                                                                                                                                                                   

    t                                                                                                                                                                       

    1 row(s) in 0.0080 seconds

    hbase(main):018:0> describe ¨t¨

    DESCRIPTION                                                                                                  ENABLED                                                    

     {NAME => ¨t¨, FAMILIES => [{NAME => ¨t_id¨, BLOOMFILTER => ¨NONE¨, REPLICATION_SCOPE => ¨0¨, COMPRESSION => true                                                       

      ¨NONE¨, VERSIONS => ¨3¨, TTL => ¨2147483647¨, BLOCKSIZE => ¨65536¨, IN_MEMORY => ¨false¨, BLOCKCACHE => ¨t                                                            

     rue¨}, {NAME => ¨t_vl¨, BLOOMFILTER => ¨NONE¨, REPLICATION_SCOPE => ¨0¨, COMPRESSION => ¨NONE¨, VERSIONS =>                                                            

      ¨3¨, TTL => ¨2147483647¨, BLOCKSIZE => ¨65536¨, IN_MEMORY => ¨false¨, BLOCKCACHE => ¨true¨}]}                                                                         

    1 row(s) in 0.0100 seconds

输出的格式也是JSON串的形式,从中可以看到保留的版本数,TTL号(Time to Live,保留时间),列的定义,块大小等等。

修改表对象,修改(含删除)前必须首先禁用对象,执行修改命令成功后,再启用对象。

禁用对象:

    hbase(main):004:0> disable ¨t¨

    0 row(s) in 2.0430 seconds

判断当前表对象启用或禁用:

    hbase(main):007:0> is_enabled ¨t¨

    false                                                                                                                                                                   

    0 row(s) in 0.0040 seconds

    hbase(main):008:0> is_disabled ¨t¨

    true                                                                                                                                                                    

    0 row(s) in 0.0040 seconds

修改表对象,增加一个列族:

    hbase(main):021:0> alter ¨t¨, {NAME => ¨t_info¨, VERSIONS => 3}      

    0 row(s) in 0.0360 seconds

    hbase(main):023:0> enable ¨t¨

    0 row(s) in 2.0250 seconds

插入记录:

    hbase(main):025:0> put ¨t¨,¨10001¨,¨t_vl:name¨,¨jss¨

    0 row(s) in 0.0060 seconds

    hbase(main):026:0> put ¨t¨,¨10001¨,¨t_vl:age¨,¨99¨

    0 row(s) in 0.0070 seconds

    hbase(main):027:0> put ¨t¨,¨10001¨,¨t_info:general¨,¨his fullname is junsanis!¨ 

    0 row(s) in 0.0040 seconds

记录获取:

    hbase(main):028:0> get ¨t¨,¨10001¨

    COLUMN                                      CELL                                                                                                                        

     t_info:general                             timestamp=1365670813664, value=his fullname is junsanis!                                                                    

     t_vl:age                                   timestamp=1365670733223, value=99                                                                                           

     t_vl:name                                  timestamp=1365670723056, value=jss                                                                                          

    3 row(s) in 0.0450 seconds

获取指定记录中指定列族的数据:

    hbase(main):029:0> get ¨t¨,¨10001¨,¨t_vl¨

    COLUMN                                      CELL                                                                                                                        

     t_vl:age                                   timestamp=1365670733223, value=99                                                                                           

     t_vl:name                                  timestamp=1365670723056, value=jss                                                                                          

    2 row(s) in 0.0070 seconds

获取指定记录中指定列族中指定列的数据:

    hbase(main):030:0> get ¨t¨,¨10001¨,¨t_vl:age¨

    COLUMN                                      CELL                                                                                                                        

     t_vl:age                                   timestamp=1365670733223, value=99                                                                                           

    1 row(s) in 0.0070 seconds

记录更新(跟插入没有区别):

    hbase(main):031:0> put ¨t¨,¨10001¨,¨t_vl:age¨,¨10¨

    0 row(s) in 0.0050 seconds

    hbase(main):032:0> get ¨t¨,¨10001¨,¨t_vl:age¨

    COLUMN                                      CELL                                                                                                                        

     t_vl:age                                   timestamp=1365670912700, value=10                                                                                           

    1 row(s) in 0.0080 seconds

全表扫描:

    hbase(main):033:0> scan ¨t¨

    ROW                                         COLUMN+CELL                                                                                                                 

     10001                                      column=t_info:general, timestamp=1365670813664, value=his fullname is junsanis!                                             

     10001                                      column=t_vl:age, timestamp=1365670912700, value=10                                                                          

     10001                                      column=t_vl:name, timestamp=1365670723056, value=jss                                                                        

    1 row(s) in 0.0370 seconds

全表描述某个列:

    hbase(main):036:0> scan ¨t¨, {COLUMNS => ¨t_vl¨}

    ROW                                         COLUMN+CELL                                                                                                                 

     10001                                      column=t_vl:age, timestamp=1365670912700, value=10                                                                          

     10001                                      column=t_vl:name, timestamp=1365670723056, value=jss                                                                        

    1 row(s) in 0.0080 seconds

删除记录行:

    hbase(main):043:0> delete ¨t¨,¨10001¨,¨t_vl:age¨

    0 row(s) in 0.0050 seconds

    hbase(main):045:0> get ¨t¨,¨10001¨

    COLUMN                                      CELL                                                                                                                        

     t_info:general                             timestamp=1365670813664, value=his fullname is junsanis!                                                                    

     t_vl:name                                  timestamp=1365670723056, value=jss                                                                                          

    2 row(s) in 0.0070 seconds

删除表:

    hbase(main):047:0> disable ¨t¨

    0 row(s) in 2.0230 seconds

    hbase(main):048:0> drop ¨t¨

    0 row(s) in 1.1170 seconds

看完前面的例子,大家有没有问题,或者想到了什么?我脑子里反正是蹦出问号了:HBase中没有UPDATE操作,只有INSERT,可是我们每次put新记录都替换掉了旧的版本,怎么保存大量记录呢?难道每个row key的columns中只能存在一条记录?这不科学!这也显然不是人民群众期待并且喜闻乐见的表现嘛。

这个问题呀,其实是列值保存版本(VERSIONS)或保留时间(TTL, Time to Liv)在起作用。

比如,我们希望统计某用户的最近(n条)浏览记录,那么,创建HBase表对象如下:

    hbase> create ¨rlog¨,¨userid¨,{NAME=>¨article¨,VERSIONS=>100}

当前设定,保留最近的100个版本。当用户浏览帖子时,就向rlog表中插入一条记录,形式如下:

    hbase> put ¨rlog¨,$userid,¨article:id¨,$aid

这里仅选择记录浏览的用户ID和浏览页面ID,也可以根据实际情况,保存页面的URL地址,文章标题等等信息。HBase表列族是非结构化的,大家可以根据需求任意增加列值。

那么,要获取用户最近浏览记录,应该怎么查呢?,比如说获取最近浏览的10条记录:

    hbase> get ¨rlog¨,$userid,{COLUMN=>¨article:id¨, VERSIONS=>10}

除了通过VERSIONS控制外,还可以考虑通过版本的保存时间TTL来控制,TTL的单位是秒,默认一般是保存30天。