Hbase常用shell及API

在项目中使用到了HBASE,我也只是用到了其中一点API,更深层的东西了解不多,还需要以后继续学习和研究.
[b][size=large]1.Hbase Shell常用命令[/size][/b]
(1)创建表:create '表名称', 'family','列名称1','列名称2'
以student表为例,创建以下表结构:
create 'student','name','fistname','lastname'

(2)查看表结构: describe '表名'
查看student表:
	describe 'student'

(3)表中增加记录:put '表名','rowkey','family:column','值'
往student表中新增一条记录:
put 'student','111','name:firstname','kim'   

(4)查看某记录:get '表名','rowkey'
查看刚才插入的一条记录:
get 'student','111'

(5)查看所有记录: scan '表名'
如:scan 'student',数据如下:
ROW                                COLUMN+CELL                                                                                                                                            
111 column=name:firstname, timestamp=1323844528158, value=kim


(6)查看多个version的数据:
get 'student','111',{COLUMN=>'name:firstname',VERSIONS=>10},数据如下:
COLUMN                                   CELL                                                                                                                             

name:firstname timestamp=1323845067789, value=kim4
name:firstname timestamp=1323845064974, value=kim3
name:firstname timestamp=1323845062682, value=kim2


(7)删除一张表:
先disable '表名',然后再drop '表名'即可.


[b][size=large]2.Hbase Client相关API使用[/size][/b]

别的不多说,这是一个访问HBASE数据类.代码如下:

public class HbaseManagerImpl implements HbaseManager {

private static final Logger logger = LoggerFactory
.getLogger(HbaseManagerImpl.class);

private final static String SEPARATE_CHAR = ":";
private HTablePool hTablePool = null;


public HbaseManagerImpl() {

try {

Configration configration = ConfigrationFactory.getConfigration();
Configuration hbaseConfiguration = HBaseConfiguration.create();
hTablePool = new HTablePool(hbaseConfiguration, 100);

} catch (Exception e) {
logger.error("HbaseManager启动失败", e);
}

}
/**
* 根据表名,rowKey,family及时间戳[startTs,endTs)查询相关数据.
*
*/
public Map<String, Map<Long, String>> queryMultiVersions(String tableName, String rowKey,
String family, long startTS, long endTS) {
//返回结果
Map<String, Map<Long, String>> resultMap = new HashMap<String, Map<Long, String>>();

if (startTS > endTS) {
logger.warn("rowKey[" + rowKey + "],开始时间戳大于结束时间戳");
return resultMap;
}

HTableInterface hTable = null;

try {
hTable = hTablePool.getTable(Bytes.toBytes(tableName));

Get get = new Get(Bytes.toBytes(rowKey));
get.setMaxVersions();
get.setTimeRange(startTS, endTS);

Result result = hTable.get(get);

NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result
.getMap();

if (map == null) {
return resultMap;
}

//取得指定family下的数据
NavigableMap<byte[], NavigableMap<Long, byte[]>> familyMap = map.get(Bytes
.toBytes(family));
if (familyMap == null) {
return resultMap;
}

Set<byte[]> familyMapSet = familyMap.keySet();

for (Iterator<byte[]> familyIt = familyMapSet.iterator(); familyIt.hasNext();) {
byte[] keyColumn = (byte[]) familyIt.next();
NavigableMap<Long, byte[]> columnMap = familyMap.get(keyColumn);

Map<Long, String> mapColumn = new HashMap<Long, String>();

Set<Long> columnMapSet = columnMap.keySet();
for (Iterator<Long> columnIt = columnMapSet.iterator(); columnIt.hasNext();) {
Long columnKey = (Long) columnIt.next();
String columnValue = new String(columnMap.get(columnKey));
mapColumn.put(columnKey, columnValue);
}

resultMap.put(new String(keyColumn), mapColumn);
}

} catch (Exception e) {
logger.error("查询HBASE出错,rowKey[" + rowKey + "]", e);

} finally {
if (hTable != null) {
hTablePool.putTable(hTable);
}
}
return resultMap;
}

/**
* HBASE中存入数据.
*/
public boolean save(String tableName, String family, String rowkey, String column,
String value, long timestamp) {

boolean result = false;
HTableInterface hTable = null;


try {
hTable = hTablePool.getTable(Bytes.toBytes(tableName));
final Put put = new Put(Bytes.toBytes(rowkey));

byte[] familyBytes = Bytes.toBytes(family);
byte[] qualifier = Bytes.toBytes(column);
byte[] valueBytes = value.getBytes();
put.add(familyBytes, qualifier, timestamp, valueBytes);
hTable.put(put);
result = true;
} catch (Exception e) {
logger.error("保存HBASE出错,rowKey[" + rowkey + "],timestamp[" + timestamp + "],value["
+ value + "]", e);

} finally {
if (hTable != null) {
hTablePool.putTable(hTable);
}
}

return result;
}

/**
* 删除指定表名的rowKey下某时间戳的数据。
*/
public boolean delete(String tableName, String rowKey, long timestamp) {
boolean result = false;
HTableInterface hTable = null;
try {
hTable = hTablePool.getTable(Bytes.toBytes(tableName));

if (hTable == null) {
return result;
}

byte[] rowKeys = Bytes.toBytes(rowKey);
Delete delete = new Delete(rowKeys, timestamp, null);
hTable.delete(delete);
result = true;
} catch (Exception e) {
logger.error("delete(),rowKey[" + rowKey + "],ts[" + timestamp + "].", e);

} finally {
if (hTable != null) {
hTablePool.putTable(hTable);
}
}

return result;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: hbase-shell命令是HBase提供的一个交互式命令行工具,用于管理HBase数据库。通过hbase-shell命令,可以执行各种操作,如创建表、插入数据、查询数据、删除数据等。同时,hbase-shell还支持HBase的各种高级功能,如过滤器、计数器等。使用hbase-shell命令可以方便地管理HBase数据库,提高工作效率。 ### 回答2: HBase是一个高性能、分布式的NoSQL数据库,它的存储结构是基于列族的,并且可以处理非常海量的数据。HBase中内置了一个HBase Shell命令行工具,它可以用来快速操作HBase数据库。以下是一些常用HBase Shell命令。 1. 查看帮助信息 使用help命令可以查看HBase Shell的帮助信息,可以使用help <command>查看某个具体命令的帮助信息。 2. 连接HBase数据库 使用connect命令可以连接到HBase数据库,例如:connect 'localhost'。 3. 列出表 使用list命令可以列出所有表,例如:list。 4. 创建表 使用create命令可以创建表,例如:create 'table_name', 'family1', 'family2'。 5. 查看表结构 使用describe命令可以查看表结构,例如:describe 'table_name'。 6. 删除表 使用drop命令可以删除表,例如:disable 'table_name',然后使用drop 'table_name'。 7. 插入数据 使用put命令可以插入数据,例如:put 'table_name', 'row_key', 'family1:column1', 'value1'。 8. 获取数据 使用get命令可以获取数据,例如:get 'table_name', 'row_key'。 9. 删除数据 使用delete命令可以删除数据,例如:delete 'table_name', 'row_key', 'family1:column1'。 10. 批量操作 使用批量操作命令,可以批量插入、获取、删除数据,例如:batch 'table_name', [ { 'delete' => 'row_key', 'column' => 'family:column' }, { 'put' => 'row_key', 'column' => 'family:column', 'value' => 'value' }, { 'get' => 'row_key' } ] 以上是一些常用HBase Shell命令,可以帮助用户快速操作HBase数据库。在实际使用过程中,还可以结合编程语言(如Java)使用HBase客户端API来操作HBase数据库。 ### 回答3: HBase-shell命令是Apache HBase数据库的交互式命令行界面,使用它可以进行HBase数据库的数据查询、插入和更新等操作。该命令支持的操作包括表的管理、数据的 CRUD 操作、Scan、Filter 等。 一、表管理: 通过hbase shell,可以将HBase的表进行管理操作。首先创建一个新表的话,需要为其指定表名,列簇和列。创建表时,列簇和列是必须参数,不能缺少。 1、 创建表 hbase(main):001:0> create 'testtable', 'colfamily' 输出: 0 row(s) in 1.5540 seconds 2、 删除表 hbase(main):001:0> drop '[table name]' 3、 关闭表 hbase(main):002:0> disable '[table name]' 4、 启用表 hbase(main):003:0> enable '[table name]' 5、 列出所有表 hbase(main):004:0> list 输出: testtable 二、数据CRUD操作: 1、查询数据 hbase(main):001:0> get '[table name]', '[row key]' 2、 插入数据 hbase(main):002:0> put '[table name]', '[rowkey]', '[columnfamily:column]', '[value]' 3、 批量插入数据 hbase(main):003:0> put '[table name]', '[rowkey]', '[columnfamily:column]', '[value]', timestamp 4、 删除数据 hbase(main):004:0> delete '[table name]', '[row]', '[column]', '[value]', timestamp 5、 批量删除数据 hbase(main):005:0> deleteall '[table name]', '[row]', '[columnfamily]' 6、 查询指定行键范围的数据 hbase(main):006:0> scan 'testtable', {STARTROW => 'row1', ENDROW => 'row2'} 三、Scan操作: 在HBase表中,Scan操作被定义为基于行的迭代器的集合。以下是一些常用的scan操作。 1、 扫描整个表 hbase(main):006:0> scan 'testtable' 2、 根据指定的列扫描整个表 hbase(main):007:0> scan 'testtable', {COLUMNS => ['colfamily', 'col']} 输出: row1 colfamily:col1 value1 row2 colfamily:col2 value2 row3 colfamily:col3 value3 3、 根据指定的行扫描整个表 hbase(main):008:0> scan 'testtable', {ROWPREFIXFILTER => 'row1'} 输出: row1 colfamily:col1 value1 row1 colfamily:col2 value2 row1 colfamily:col3 value3 4、 根据指定的列和行扫描整个表 hbase(main):009:0> scan 'testtable', {FILTER => "(PrefixFilter('row') AND (QualifierFilter (>=,‘binary:col:')))"} 输出: row1 colfamily:col1 value1 row1 colfamily:col2 value2 row1 colfamily:col3 value3 综上所述,HBase-shell命令是操作HBase数据库的一个重要工具,可用于管理表和进行数据的读写删除等操作。尤其是对于非Java开发人员,HBase-shell命令的简单操作可以减少学习和使用HBase的难度,提高工作效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值