HBase Shell & Java API

HBase Shell

help命令

查看帮助:

hbase(main):001:0> help 'status'
Show cluster status. Can be 'summary', 'simple', 'detailed', or 'replication'. The
default is 'summary'. Examples:

  hbase> status
  hbase> status 'simple'
  hbase> status 'summary'
  hbase> status 'detailed'
  hbase> status 'replication'
  hbase> status 'replication', 'source'
  hbase> status 'replication', 'sink'

status命令

查看集群状态:

hbase(main):002:0> status
1 active master, 0 backup masters, 1 servers, 0 dead, 3.0000 average load

create命令 & list命令

创建表:

hbase(main):001:0> create 'FileTable','fileInfo','saveInfo'
0 row(s) in 1.9600 seconds

=> Hbase::Table - FileTable

查看有几张表:

hbase(main):002:0> list
TABLE 
FileTable                                                                                                                                                                          
1 row(s) in 0.0420 seconds

=> ["FileTable"]

desc命令

表的描述:

hbase(main):003:0> desc 'FileTable'
Table FileTable is ENABLED
FileTable
COLUMN FAMILIES DESCRIPTION
{NAME => 'fileInfo', 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'}
{NAME => 'saveInfo', 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'}
2 row(s) in 0.1370 seconds

alter命令

为FileTable表添加列族cf:

hbase(main):001:0> alter 'FileTable','cf'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 3.6990 seconds

为FileTable表删除列族cf:

hbase(main):004:0> alter 'FileTable',{NAME=>'cf',METHOD=>'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.3110 seconds

put命令

插入数据:

hbase(main):006:0> put 'FileTable','rowkey1','fileInfo:name','file1.txt'
0 row(s) in 0.1640 seconds

hbase(main):007:0> put 'FileTable','rowkey1','fileInfo:type','txt'
0 row(s) in 0.0220 seconds

hbase(main):008:0> put 'FileTable','rowkey1','fileInfo:size','1024'
0 row(s) in 0.0070 seconds

hbase(main):009:0> put 'FileTable','rowkey1','saveInfo:path','/home'
0 row(s) in 0.1910 seconds

hbase(main):010:0> put 'FileTable','rowkey1','saveInfo:creator','tom'
0 row(s) in 0.1080 seconds

hbase(main):011:0> put 'FileTable', 'rowkey2','fileInfo:name','file2.jpg'
0 row(s) in 0.0090 seconds

hbase(main):012:0> put 'FileTable', 'rowkey2','fileInfo:type','jpg'
0 row(s) in 0.0170 seconds

hbase(main):013:0> put 'FileTable', 'rowkey2','fileInfo:size','2048'
0 row(s) in 0.0100 seconds

hbase(main):014:0> put 'FileTable', 'rowkey2','saveInfo:path','/home/pic'
0 row(s) in 0.0220 seconds

hbase(main):015:0> put 'FileTable', 'rowkey2','saveInfo:creator','jerry'
0 row(s) in 0.0110 seconds

count命令

统计FileTable表中有多少行:

hbase(main):016:0> count 'FileTable'
2 row(s) in 0.0590 seconds

=> 2

get命令

查看FileTable表,rowkey2的数据:

hbase(main):001:0> get 'FileTable','rowkey2'
COLUMN                   CELL                                                                  
 fileInfo:name           timestamp=1531868157371, value=file2.jpg                              
 fileInfo:size           timestamp=1531868173736, value=2048                                   
 fileInfo:type           timestamp=1531868165142, value=jpg                                    
 saveInfo:creator        timestamp=1531868184641, value=jerry                                  
 saveInfo:path           timestamp=1531868178723, value=/home/pic                              
5 row(s) in 0.4510 seconds

查看FileTable表,rowkey1、fileInfo列族的数据:

hbase(main):002:0> get 'FileTable','rowkey1','fileInfo'
COLUMN                   CELL                                                                  
 fileInfo:name           timestamp=1531867971478, value=file1.txt                              
 fileInfo:size           timestamp=1531868016619, value=1024                                   
 fileInfo:type           timestamp=1531867999127, value=txt                                    
3 row(s) in 0.0340 seconds

scan命令

查询整张表的数据:

hbase(main):003:0> scan 'FileTable'
ROW                      COLUMN+CELL
 rowkey1                 column=fileInfo:name, timestamp=1531867971478, value=file1.txt
 rowkey1                 column=fileInfo:size, timestamp=1531868016619, value=1024
 rowkey1                 column=fileInfo:type, timestamp=1531867999127, value=txt
 rowkey1                 column=saveInfo:creator, timestamp=1531868053770, value=tom
 rowkey1                 column=saveInfo:path, timestamp=1531868035745, value=/home
 rowkey2                 column=fileInfo:name, timestamp=1531868157371,value=file2.jpg
 rowkey2                 column=fileInfo:size, timestamp=1531868173736, value=2048
 rowkey2                 column=fileInfo:type, timestamp=1531868165142, value=jpg
 rowkey2                 column=saveInfo:creator, timestamp=1531868184641, value=jerry
 rowkey2               column=saveInfo:path, timestamp=1531868178723,value=/home/pic
2 row(s) in 0.1200 seconds

查询某个列族的数据(指定列限定符):

hbase(main):004:0> scan 'FileTable',{COLUMN=>'fileInfo:name'}
ROW                      COLUMN+CELL                                                           
 rowkey1                 column=fileInfo:name, timestamp=1531867971478, value=file1.txt
 rowkey2                column=fileInfo:name, timestamp=1531868157371, value=file2.jpg
2 row(s) in 0.0480 seconds

查询某个列族的数据:

hbase(main):005:0> scan 'FileTable',{COLUMN=>'fileInfo'}
ROW                      COLUMN+CELL                                                           
 rowkey1                 column=fileInfo:name, timestamp=1531867971478, value=file1.txt
 rowkey1                 column=fileInfo:size, timestamp=1531868016619, value=1024
 rowkey1                 column=fileInfo:type, timestamp=1531867999127, value=txt
 rowkey2                 column=fileInfo:name, timestamp=1531868157371,value=file2.jpg
 rowkey2                 column=fileInfo:size, timestamp=1531868173736, value=2048
 rowkey2                 column=fileInfo:type, timestamp=1531868165142, value=jpg
2 row(s) in 0.0490 second

LIMIT&VERSION 结合 Scan命令的使用

查询1行数据,且是最新的:

hbase(main):006:0> scan 'FileTable',{STARTROW=>'rowkey1',LIMIT=>1,VERSIONS=>1}
ROW                      COLUMN+CELL                                                           
 rowkey1                 column=fileInfo:name, timestamp=1531867971478, value=file1.txt
 rowkey1                 column=fileInfo:size, timestamp=1531868016619, value=1024
 rowkey1                 column=fileInfo:type, timestamp=1531867999127, value=txt
 rowkey1                 column=saveInfo:creator, timestamp=1531868053770, value=tom
 rowkey1                 column=saveInfo:path, timestamp=1531868035745, value=/home
1 row(s) in 0.0290 seconds

查询2行数据,且是最新的:

hbase(main):007:0> scan 'FileTable',{STARTROW=>'rowkey1',LIMIT=>2,VERSIONS=>1}
ROW                      COLUMN+CELL 
 rowkey1                 column=fileInfo:name, timestamp=1531867971478, value=file1.txt
 rowkey1                 column=fileInfo:size, timestamp=1531868016619, value=1024
 rowkey1                 column=fileInfo:type, timestamp=1531867999127, value=txt
rowkey1                 column=saveInfo:creator, timestamp=1531868053770, value=tom
 rowkey1                 column=saveInfo:path, timestamp=1531868035745, value=/home
 rowkey2                column=fileInfo:name, timestamp=1531868157371, value=file2.jpg
 rowkey2                 column=fileInfo:size, timestamp=1531868173736, value=2048
 rowkey2                 column=fileInfo:type, timestamp=1531868165142, value=jpg
 rowkey2                 column=saveInfo:creator, timestamp=1531868184641, value=jerry
 rowkey2              column=saveInfo:path, timestamp=1531868178723, value=/home/pic
2 row(s) in 0.0400 seconds

delete命令

删除fileInfo:size下所有的数据:

hbase(main):001:0> delete 'FileTable','rowkey1','fileInfo:size'
0 row(s) in 0.4650 seconds

hbase(main):002:0> get 'FileTable','rowkey1','fileInfo:size'
COLUMN                   CELL
0 row(s) in 0.0370 seconds

删除rowkey1下所有的数据:

hbase(main):003:0> deleteall 'FileTable','rowkey1'
0 row(s) in 0.0240 seconds

hbase(main):004:0> get 'FileTable','rowkey1'
COLUMN                   CELL
0 row(s) in 0.0150 seconds

disable & is_enabled & is_disabled & drop & exist命令

删除表之前必须先禁用表

hbase(main):005:0> disable 'FileTable'
0 row(s) in 2.5510 seconds

hbase(main):006:0> is_enabled 'FileTable'
false                                                                                          
0 row(s) in 0.0240 seconds

hbase(main):007:0> is_disabled 'FileTable'
true                                                                                           
0 row(s) in 0.0290 seconds

hbase(main):008:0> drop 'FileTable'
0 row(s) in 1.2910 seconds

hbase(main):009:0> exists 'FileTable'
Table FileTable does not exist                                                                 
0 row(s) in 0.0240 seconds

通过Java API操作HBase

Github地址:https://github.com/lemonahit/hbase-example

作用
HBaseConn.java获取HBase连接
HBaseConnTest.java测试:获取数据库连接和获取table实例
HBaseUtil.javaHBase增删改查模块
HbaseUtilTest.java测试:HBase增删改查模块
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值