Hbase惯用shell及API
Hbase常用shell及API
在项目中使用到了HBASE,我也只是用到了其中一点API,更深层的东西了解不多,还需要以后继续学习和研究.
1.Hbase Shell常用命令
(1)创建表:create '表名称', 'family','列名称1','列名称2'
以student表为例,创建以下表结构:
(2)查看表结构: describe '表名'
查看student表:
(3)表中增加记录:put '表名','rowkey','family:column','值'
往student表中新增一条记录:
(4)查看某记录:get '表名','rowkey'
查看刚才插入的一条记录:
(5)查看所有记录: scan '表名'
如:scan 'student',数据如下:
(6)查看多个version的数据:
get 'student','111',{COLUMN=>'name:firstname',VERSIONS=>10},数据如下:
(7)删除一张表:
先disable '表名',然后再drop '表名'即可.
2.Hbase Client相关API使用
别的不多说,这是一个访问HBASE数据类.代码如下:
在项目中使用到了HBASE,我也只是用到了其中一点API,更深层的东西了解不多,还需要以后继续学习和研究.
1.Hbase Shell常用命令
(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 '表名'即可.
2.Hbase Client相关API使用
别的不多说,这是一个访问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; } }