Hbase学习第 5 天:HBase 命令行操作(下)

今天继续学习 HBase 命令行操作,详细操作如下

四、DML 操作语言

DML 是数据操作语言,用户可以通过这些语言实现对数据的基本操作,包括数据的增删改查
HBase DML 命令有 append、 count、 delete、 deleteall、 get、 get_counter、 get_splits、 incr、 put、 scan、 truncate、 truncate_preserve 等

1、HBase 表中添加数据

put 语法如下:

  hbase> put 'ns1:t1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value', ts1
  hbase> put 't1', 'r1', 'c1', 'value', {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

实例:

hbase(main):020:0> describe 'test'
Table test is ENABLED                                     
test                                                      
COLUMN FAMILIES DESCRIPTION                               
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', 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.0260 seconds

# 向表 test 中放入两条数据
hbase(main):017:0* put 'test','001','info','luo'
put 'test','002','info','luo_ff'
0 row(s) in 0.0640 seconds

hbase(main):022:0> put 'test','002','info','luo_ff'
0 row(s) in 0.0090 seconds

2、统计表中数据条数

hbase(main):023:0> count 'test'
2 row(s) in 0.0040 seconds

=> 2

3、查看表中数据

HBase 中数据的浏览有两种方式,一种是全表扫描,一种是指定获取到一条数据,全表扫描 用 scan 命令,指定获取某条数据 用 get 命令,具体使用如下:

3.1 scan 全表扫描

命令语法如下:

Some examples:

  hbase> scan 'hbase:meta'
  hbase> scan 'hbase:meta', {COLUMNS => 'info:regioninfo'}
  hbase> scan 'ns1:t1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
  hbase> scan 't1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
  hbase> scan 't1', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]}
  hbase> scan 't1', {REVERSED => true}
  hbase> scan 't1', {ALL_METRICS => true}
  hbase> scan 't1', {METRICS => ['RPC_RETRIES', 'ROWS_FILTERED']}
  hbase> scan 't1', {ROWPREFIXFILTER => 'row2', FILTER => "
    (QualifierFilter (>=, 'binary:xyz')) AND (TimestampsFilter ( 123, 456))"}
  hbase> scan 't1', {FILTER =>
    org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)}
  hbase> scan 't1', {CONSISTENCY => 'TIMELINE'}
For setting the Operation Attributes 
  hbase> scan 't1', { COLUMNS => ['c1', 'c2'], ATTRIBUTES => {'mykey' => 'myvalue'}}
  hbase> scan 't1', { COLUMNS => ['c1', 'c2'], AUTHORIZATIONS => ['PRIVATE','SECRET']}
For experts, there is an additional option -- CACHE_BLOCKS -- which
switches block caching for the scanner on (true) or off (false).  By
default it is enabled.  Examples:

  hbase> scan 't1', {COLUMNS => ['c1', 'c2'], CACHE_BLOCKS => false}

Also for experts, there is an advanced option -- RAW -- which instructs the
scanner to return all cells (including delete markers and uncollected deleted
cells). This option cannot be combined with requesting specific COLUMNS.
Disabled by default.  Example:

 hbase> scan 't1', {RAW => true, VERSIONS => 10}
 

实例:

hbase(main):033:0> scan 'test'
ROW                                                         COLUMN+CELL 
 001                                                        column=info:, timestamp=1574130792106, value=luo                                                                
 002                                                        column=info:, timestamp=1574131392911, value=luo_ff       
2 row(s) in 0.0120 seconds

3.2 HBase 单条数据查询(get)

get 查询单条数据语法如下:

hbase> t.get 'r1'
  hbase> t.get 'r1', {TIMERANGE => [ts1, ts2]}
  hbase> t.get 'r1', {COLUMN => 'c1'}
  hbase> t.get 'r1', {COLUMN => ['c1', 'c2', 'c3']}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
  hbase> t.get 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
  hbase> t.get 'r1', 'c1'
  hbase> t.get 'r1', 'c1', 'c2'
  hbase> t.get 'r1', ['c1', 'c2']
  hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE'}
  hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE', REGION_REPLICA_ID => 1}

实例:

hbase(main):003:0> get 'test','001'
COLUMN                                                      CELL
 info:                                                      timestamp=1574146101757, value=luo     
1 row(s) in 0.0230 seconds

4、在某一列的值后面追加

追加值使用 append 命令,命令语法如下:

  hbase> append 't1', 'r1', 'c1', 'value', ATTRIBUTES=>{'mykey'=>'myvalue'}
  hbase> append 't1', 'r1', 'c1', 'value', {VISIBILITY=>'PRIVATE|SECRET'

同样的命令也可以在表引用上运行,语法如下:

 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.append 'r1', 'c1', 'value', ATTRIBUTES=>{'mykey'=>'myvalue'}
  hbase> t.append 'r1', 'c1', 'value', {VISIBILITY=>'PRIVATE|SECRET'}

实例:

hbase(main):012:0> append 'test','001','info:luo','luoluo'
CURRENT VALUE = luoluo
0 row(s) in 0.0280 seconds

hbase(main):013:0> scan 'test'
ROW                                                         COLUMN+CELL                                                                                                                                                                  
 001                                                        column=info:, timestamp=1574146101757, value=luo                                                                                                                             
 001                                                        column=info:luo, timestamp=1574154430334, value=luoluo                                                                                                                       
 002                                                        column=info:, timestamp=1574146107500, value=luo_ff 

5、删除数据

HBase 删除数据语法如下:

hbase> delete 'ns1:t1', 'r1', 'c1', ts1
hbase> delete 't1', 'r1', 'c1', ts1
hbase> delete 't1', 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

hbase> t.delete 'r1', 'c1',  ts1
hbase> t.delete 'r1', 'c1',  ts1, {VISIBILITY=>'PRIVATE|SECRET'}

实例:

# 删除表 test 中的数据
hbase(main):018:0* delete "test","001","info:luo",1574130792106

0 row(s) in 0.0070 seconds

6、删除行或者整个列族

使用 deleteall 命令删除行或者整个列族,使用命令如下:

  hbase> deleteall 'ns1:t1', 'r1'
  hbase> deleteall 't1', 'r1'
  hbase> deleteall 't1', 'r1', 'c1'
  hbase> deleteall 't1', 'r1', 'c1', ts1
  hbase> deleteall 't1', 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

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.deleteall 'r1'
  hbase> t.deleteall 'r1', 'c1'
  hbase> t.deleteall 'r1', 'c1', ts1
  hbase> t.deleteall 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

实例:

hbase(main):017:0* deleteall 'test','001' 
0 row(s) in 0.0120 seconds

hbase(main):018:0> scan 'test'
ROW                                                         COLUMN+CELL                                                                                                                                                                  
 002                                                        column=info:, timestamp=1574146107500, value=luo_ff                                                                                                                          
1 row(s) in 0.0110 seconds

7、清空表数据

清空表数据有两个命令,一个是truncate,另一个是truncate_preserve,truncate_preserve 只清空数据,不删除region的划分规则

实例:

hbase(main):025:0> truncate 'test'
Truncating 'test' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.3770 seconds

hbase(main):029:0> truncate_preserve 'test1'
Truncating 'test1' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 1.1490 seconds

通过上述实例可以看出,清空表数据的步骤首先先禁用此表,然后在清空表中数据

五、其他操作命令

HBase 除了第 4 天学习的 和以上基础命令外,还有其他一些运用更广泛的命令,这些命令我将在后面的时间了解透彻后作详细的介绍,一起静待,除了基础命令外,HBase 其他命令如下:

 Group name: tools
  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, cleaner_chore_enabled, cleaner_chore_run, cleaner_chore_switch, clear_deadservers, close_region, compact, compact_rs, compaction_state, flush, is_in_maintenance_mode, list_deadservers, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, splitormerge_enabled, splitormerge_switch, trace, unassign, wal_roll, zk_dump
  Group name: replication
  Commands: add_peer, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, get_peer_config, list_peer_configs, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_bandwidth, set_peer_tableCFs, show_peer_tableCFs, update_peer_config
  Group name: snapshots
  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, delete_table_snapshots, list_snapshots, list_table_snapshots, restore_snapshot, snapshot
  Group name: configuration
  Commands: update_all_config, update_config
  Group name: quotas
  Commands: list_quotas, set_quota
  Group name: security
  Commands: grant, list_security_capabilities, revoke, user_permission
  Group name: procedures
  Commands: abort_procedure, list_procedures
  Group name: visibility labels
  Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility
  Group name: rsgroup
  Commands: add_rsgroup, balance_rsgroup, get_rsgroup, get_server_rsgroup, get_table_rsgroup, list_rsgroups, move_servers_rsgroup, move_servers_tables_rsgroup, move_tables_rsgroup, remove_rsgroup, remove_servers_rsgroup

总结,今天的文章是对 HBase 基础命令的了解与运用,除了基础命令以外,HBase 还有其他更有意思的命令,就如以上第二点的命令,这些命令我将在后面的作详细介绍。

你可能感兴趣的:(HBase,大数据)