我们现在通过下列步骤来展示如何使用DDL命令来管理HBASE表.
-
在客户机节点上执行以下命仅启动HBASE SHELL
1
#hbase shell
-
在HBASE SHELL中使用create命令创建一张带有一个列族(f1)的表(t1)
1
hbase(main):001:0> create 't1','f1'
-
使用list命令列出所有表的列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
hbase(main):008:0> list TABLE MIANTIAO_USERNAME PERFORMANCE_10000 SYSTEM.CATALOG SYSTEM.FUNCTION SYSTEM.SEQUENCE SYSTEM.STATS US_POPULATION US_POPULATION2 UUU UUU33 hly_temp lxw1234 question ssss t1 t2 t3 t4 18 row(s) in 0.0370 seconds => ["MIANTIAO_USERNAME", "PERFORMANCE_10000", "SYSTEM.CATALOG", "SYSTEM.FUNCTION", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "US_POPULATION", "US_POPULATION2", "UUU", "UUU33", "hly_temp", "lxw1234", "question", "ssss", "t1", "t2", "t3", "t4"] hbase(main):009:0>
-
使用describe命令来显示该表的属性
1 2 3 4 5 6 7 8
hbase(main):009:0> describe 't1' Table t1 is ENABLED t1 COLUMN FAMILIES DESCRIPTION {NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '5', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => ' NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 1 row(s) in 0.1840 seconds
-
使用disable 命令来禁用该表
1 2 3 4 5 6
hbase(main):013:0> disable 't1' 0 row(s) in 0.0170 seconds hbase(main):014:0> is_enabled 't1' false 0 row(s) in 0.0160 seconds
-
使用alter命令来修改表的属性.下面这段代码会先将f1修改为只有一个版本,然后再添加了一个新列族的f2
1 2 3 4 5 6 7 8
hbase(main):015:0> alter 't1',{NAME=>'f1',VERSIONS=>'1'},{NAME=>'f2'} Updating all regions with the new schema... 1/1 regions updated. Done. Updating all regions with the new schema... 1/1 regions updated. Done. 0 row(s) in 3.8100 seconds
-
使用enable命令来启用该表
1 2 3 4 5 6
hbase(main):016:0> enable 't1' 0 row(s) in 1.4190 seconds hbase(main):017:0> is_enabled 't1' true 0 row(s) in 0.0090 seconds
-
输入以下命令来再次禁用该表.然后将期删除
1 2 3 4 5
hbase(main):018:0> disable 't1' 0 row(s) in 2.2440 seconds hbase(main):019:0> drop 't1' 0 row(s) in 1.2650 seconds
-
使用put命令将下列数据插入到该表中
1 2 3 4 5 6 7 8
hbase(main):021:0> put 't1','row1','f1:c1','value1' 0 row(s) in 0.1030 seconds hbase(main):022:0> put 't1','row1','f1:c2','value2' 0 row(s) in 0.0130 seconds hbase(main):023:0> put 't1','row2','f1:c1','value3' 0 row(s) in 0.0230 seconds
put命令使用表名(t1)/行键(row1)/列族和限定符(f1:c1)以及要赋予的值(value1)为参数,别外它还有一个可选的时间戳参数.在f1:c1中.冒号(:)是列族(f1)与限定符(c1)之前的间隔符.在HBASE SHELL中,如果想要把一个值当成字符串,你就用单引号包括起来就行.
- 执行count命令来获取该表的行数
1 2 3 4
hbase(main):024:0> count 't1' 2 row(s) in 0.0930 seconds => 2
count命令可以统计一张表中记录数,它会以”行键”为基础进行统计,所以为2.
-
使用scan命令来扫描数据.在对记录很多的表进行scan时,不要忘记指定LIMIT属性
1 2 3 4 5 6 7
hbase(main):029:0> scan 't1',{LIMIT=>10} ROW COLUMN+CELL row1 column=f1:c1, timestamp=1460644326819, value=value1 row1 column=f1:c2, timestamp=1460644335627, value=value2 row2 column=f1:c1, timestamp=1460644754895, value=value7 row3 column=f1:c1, timestamp=1460644762929, value=value7 3 row(s) in 0.0600 seconds
-
使用get命令来读取某一行
1 2 3 4 5
hbase(main):030:0> get 't1','row1' COLUMN CELL f1:c1 timestamp=1460644326819, value=value1 f1:c2 timestamp=1460644335627, value=value2 2 row(s) in 0.0200 seconds
-
使用delete命令来删除某一指定单元格
1 2 3 4 5 6 7 8 9
hbase(main):032:0> delete 't1','row1','f1:c1' 0 row(s) in 0.0250 seconds hbase(main):033:0> scan 't1',{LIMIT=>10} ROW COLUMN+CELL row1 column=f1:c2, timestamp=1460644335627, value=value2 row2 column=f1:c1, timestamp=1460644754895, value=value7 row3 column=f1:c1, timestamp=1460644762929, value=value7 3 row(s) in 0.0240 seconds
-
使用deleteall命令删除某一指定行的所有单元格
1 2
hbase(main):002:0> delete 't1','row1','f1:c1' 0 row(s) in 0.0610 seconds
-
再次执行get命令;你会发现整个row1行都已被从表中删除了.
1 2 3 4
hbase(main):001:0> get 't1','row1' COLUMN CELL f1:c2 timestamp=1460644335627, value=value2 1 row(s) in 0.3090 seconds
-
使用incr命令让计数器(row1:f1:c1)的值加1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
hbase(main):003:0> scan 't1' ROW COLUMN+CELL row1 column=f1:c2, timestamp=1460644335627, value=value2 row2 column=f1:c1, timestamp=1460644754895, value=value7 row3 column=f1:c1, timestamp=1460644762929, value=value7 3 row(s) in 0.1120 seconds hbase(main):004:0> incr 't1','row1','f1:c1',1 COUNTER VALUE = 1 0 row(s) in 0.0280 seconds hbase(main):005:0> scan 't1' ROW COLUMN+CELL row1 column=f1:c1, timestamp=1460801878627, value=\x00\x00\x00\x00\x00\x00\x00\x01 row1 column=f1:c2, timestamp=1460644335627, value=value2 row2 column=f1:c1, timestamp=1460644754895, value=value7 row3 column=f1:c1, timestamp=1460644762929, value=value7 3 row(s) in 0.0310 seconds
-
再使该计数器加10
1 2 3 4 5 6 7 8 9 10 11
hbase(main):006:0> incr 't1','row1','f1:c1',10 COUNTER VALUE = 11 0 row(s) in 0.0160 seconds hbase(main):007:0> scan 't1' ROW COLUMN+CELL row1 column=f1:c1, timestamp=1460802073632, value=\x00\x00\x00\x00\x00\x00\x00\x0B row1 column=f1:c2, timestamp=1460644335627, value=value2 row2 column=f1:c1, timestamp=1460644754895, value=value7 row3 column=f1:c1, timestamp=1460644762929, value=value7 3 row(s) in 0.0300 seconds
-
使用get_counter命令来读取该计数器的新值
1 2
hbase(main):008:0> get_counter 't1','row1','f1:c1' COUNTER VALUE = 11
-
执行truncate命令来截断该表
truncate命令会进行一系列的操作,对表进行禁用/删除/然后再重建1 2 3 4 5
hbase(main):009:0> truncate 't1' Truncating 't1' table (it may take a while): - Disabling table... - Truncating table... 0 row(s) in 3.4570 seconds