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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值