Changes between Version 1 and Version 2 of hbasecommand


Ignore:
Timestamp:
09/07/2012 03:57:22 PM (14 years ago)
Author:
liaojiaohe
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • hbasecommand

    v1 v2  
    1 建表例子,设定版本数和压缩方式,可以每个CF单独设置[[BR]] 
    2  
     1 
     2== 常用操作== 
     3 
     4(1)建立一个表scores,有两个列族grad和courese 
     5 
     6 
     7{{{ 
     8hbase(main):001:0> create ‘scores’,'grade’, ‘course’ 
     9}}} 
     10 
     11 
     12可以使用list命令来查看当前HBase里有哪些表。使用describe命令来查看表结构。(记得所有的表明、列名都需要加上引号) 
     13 
     14(2)按设计的表结构插入值: 
     15 
     16 
     17{{{ 
     18put ‘scores’,'Tom’,'grade:’,’5′ 
     19put ‘scores’,'Tom’,'course:math’,’97′ 
     20put ‘scores’,'Tom’,'course:art’,’87′ 
     21put ‘scores’,'Jim’,'grade’,’4′ 
     22put ‘scores’,'Jim’,'course:’,’89′ 
     23put ‘scores’,'Jim’,'course:’,’80′ 
     24}}} 
     25 
     26 
     27这样表结构就起来了,其实比较自由,列族里边可以自由添加子列很方便。如果列族下没有子列,加不加冒号都是可以的。 
     28 
     29put命令比较简单,只有这一种用法: 
     30 
     31 
     32{{{ 
     33hbase> put ‘t1′, ‘r1′, ‘c1′, ‘value’, ts1 
     34}}} 
     35 
     36 
     37t1指表名,r1指行键名,c1指列名,value指单元格值。ts1指时间戳,一般都省略掉了。 
     38 
     39(3)根据键值查询数据 
     40 
     41 
     42{{{ 
     43get ‘scores’,'Jim’ 
     44get ‘scores’,'Jim’,'grade’ 
     45}}} 
     46 
     47 
     48可能你就发现规律了,HBase的shell操作,一个大概顺序就是操作关键词后跟表名,行名,列名这样的一个顺序,如果有其他条件再用花括号加上。 
     49get有用法如下: 
     50 
     51 
     52{{{ 
     53hbase> get ‘t1′, ‘r1′ 
     54hbase> get ‘t1′, ‘r1′, {TIMERANGE => [ts1, ts2]} 
     55hbase> get ‘t1′, ‘r1′, {COLUMN => ‘c1′} 
     56hbase> get ‘t1′, ‘r1′, {COLUMN => ['c1', 'c2', 'c3']} 
     57hbase> get ‘t1′, ‘r1′, {COLUMN => ‘c1′, TIMESTAMP => ts1} 
     58hbase> get ‘t1′, ‘r1′, {COLUMN => ‘c1′, TIMERANGE => [ts1, ts2], VERSIONS => 4} 
     59hbase> get ‘t1′, ‘r1′, {COLUMN => ‘c1′, TIMESTAMP => ts1, VERSIONS => 4} 
     60hbase> get ‘t1′, ‘r1′, ‘c1′ 
     61hbase> get ‘t1′, ‘r1′, ‘c1′, ‘c2′ 
     62hbase> get ‘t1′, ‘r1′, ['c1', 'c2'] 
     63}}} 
     64 
     65 
     66(4)扫描所有数据 
     67 
     68 
     69{{{ 
     70scan ‘scores’ 
     71}}} 
     72 
     73 
     74也可以指定一些修饰词:TIMERANGE, FILTER, LIMIT, STARTROW, STOPROW, TIMESTAMP, MAXLENGTH,or COLUMNS。没任何修饰词,就是上边例句,就会显示所有数据行。 
     75 
     76例句如下: 
     77 
     78 
     79{{{ 
     80hbase> scan ‘.META.’ 
     81hbase> scan ‘.META.’, {COLUMNS => ‘info:regioninfo’} 
     82hbase> scan ‘t1′, {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => ‘xyz’} 
     83hbase> scan ‘t1′, {COLUMNS => ‘c1′, TIMERANGE => [1303668804, 1303668904]} 
     84hbase> scan ‘t1′, {FILTER => “(PrefixFilter (‘row2′) AND (QualifierFilter (>=, ‘binary:xyz’))) AND (TimestampsFilter ( 123, 456))”} 
     85hbase> scan ‘t1′, {FILTER => org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)} 
     86}}} 
     87 
     88 
     89过滤器filter有两种方法指出: 
     90a. Using a filterString – more information on this is available in the 
     91Filter Language document attached to the [https://issues.apache.org/jira/browse/HBASE-4176 HBASE-4176 JIRA] 
     92b. Using the entire package name of the filter. 
     93 
     94还有一个CACHE_BLOCKS修饰词,开关scan的缓存的,默认是开启的(CACHE_BLOCKS=>true),可以选择关闭(CACHE_BLOCKS=>false)。 
     95 
     96(5)删除指定数据 
     97 
     98 
     99{{{ 
     100delete ‘scores’,'Jim’,'grade’ 
     101delete ‘scores’,'Jim’ 
     102}}} 
     103 
     104 
     105删除数据命令也没太多变化,只有一个: 
     106 
     107 
     108{{{ 
     109hbase> delete ‘t1′, ‘r1′, ‘c1′, ts1 
     110}}} 
     111 
     112 
     113另外有一个deleteall命令,可以进行整行的范围的删除操作,慎用! 
     114如果需要进行全表删除操作,就使用truncate命令,其实没有直接的全表删除命令,这个命令也是disable,drop,create三个命令组合出来的。 
     115 
     116(6)修改表结构 
     117 
     118 
     119{{{ 
     120disable ‘scores’ 
     121alter ‘scores’,NAME=>’info’ 
     122enable ‘scores’ 
     123}}} 
     124 
     125 
     126alter命令使用如下(如果无法成功的版本,需要先通用表disable): 
     127a、改变或添加一个列族: 
     128 
     129 
     130{{{ 
     131hbase> alter ‘t1′, NAME => ‘f1′, VERSIONS => 5 
     132}}} 
     133 
     134 
     135b、删除一个列族: 
     136 
     137 
     138{{{ 
     139hbase> alter ‘t1′, NAME => ‘f1′, METHOD => ‘delete’ 
     140hbase> alter ‘t1′, ‘delete’ => ‘f1′ 
     141}}} 
     142 
     143 
     144c、也可以修改表属性如MAX_FILESIZE 
     145MEMSTORE_FLUSHSIZE, READONLY,和 DEFERRED_LOG_FLUSH: 
     146 
     147{{{ 
     148hbase> alter ‘t1′, METHOD => ‘table_att’, MAX_FILESIZE => ’134217728′ 
     149}}} 
     150 
     151d、可以添加一个表协同处理器 
     152 
     153 
     154{{{ 
     155hbase> alter ‘t1′, METHOD => ‘table_att’, ‘coprocessor’=> ‘hdfs:///foo.jar|com.foo.FooRegionObserver|1001|arg1=1,arg2=2′ 
     156}}} 
     157 
     158 
     159一个表上可以配置多个协同处理器,一个序列会自动增长进行标识。加载协同处理器(可以说是过滤程序)需要符合以下规则: 
     160 
     161[coprocessor jar file location] | class name | [priority] | [arguments] 
     162 
     163e、移除coprocessor如下: 
     164 
     165 
     166{{{ 
     167hbase> alter ‘t1′, METHOD => ‘table_att_unset’, NAME => ‘MAX_FILESIZE’ 
     168hbase> alter ‘t1′, METHOD => ‘table_att_unset’, NAME => ‘coprocessor$1′ 
     169}}} 
     170 
     171 
     172f、可以一次执行多个alter命令: 
     173 
     174 
     175{{{ 
     176hbase> alter ‘t1′, {NAME => ‘f1′}, {NAME => ‘f2′, METHOD => ‘delete’} 
     177}}} 
     178 
     179 
     180(7)统计行数: 
     181 
     182 
     183{{{ 
     184hbase> count ‘t1′ 
     185hbase> count ‘t1′, INTERVAL => 100000 
     186hbase> count ‘t1′, CACHE => 1000 
     187hbase> count ‘t1′, INTERVAL => 10, CACHE => 1000 
     188}}} 
     189 
     190 
     191count一般会比较耗时,使用mapreduce进行统计,统计结果会缓存,默认是10行。统计间隔默认的是1000行(INTERVAL)。 
     192 
     193(8)disable 和 enable 操作 
     194很多操作需要先暂停表的可用性,比如上边说的alter操作,删除表也需要这个操作。disable_all和enable_all能够操作更多的表。 
     195 
     196(9)表的删除 
     197先停止表的可使用性,然后执行删除命令。 
     198 
     199 
     200{{{ 
     201drop ‘t1′ 
     202}}} 
     203 
     204 
     205 
     206 
     207---- 
     208 
     209 
     210== 我们自己的一些例子 == 
     211 
     212 
     213*  建表例子,设定版本数和压缩方式,可以每个CF单独设置[[BR]] 
    3214 
    4215{{{ 
    5216create 'user',{NAME=>'base',VERSIONS=>90,COMPRESSION => 'LZO'} 
    6217}}} 
     218 
     219 
     220* 转换表格从没压缩变为有压缩,enable后表格进行Compact操作进行压缩 
     221 
     222{{{ 
     223disable 'ad2_report_base' 
     224alter 'ad2_report_base',NAME=>'ad_launch_report_detail',COMPRESSION => 'SNAPPY' 
     225enable 'ad2_report_base' 
     226}}}