在项目中使用到了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;
}
}
分享到:
相关推荐
(2)熟练使用HBase操作常用的 Shell命令。(3)熟悉HBase操作常用的 Java API。 A.3.2 实验平台 (1)操作系统:Linux。 (2)Hadoop 版本:2.7.1或以上版本。(3)HBase版本:1.1.2或以上版本。(4) JDK 版本:1.7或以上...
2. 熟练使用HBase操作常用的Shell命令; 3. 熟悉HBase操作常用的Java API。 二、实验平台 1. 操作系统:Linux(建议Ubuntu16.04或Ubuntu18.04); 2. Hadoop版本:3.1.3; 3. HBase版本:2.2.2; 4. JDK版本:1.8; ...
使用HBase常用Java API创建表、插入数据、浏览数据。 二.实验目的 1、理解HBase在Hadoop体系结构中的角色。 2、熟练使用HBase操作常用的Shell命令。 3、熟悉HBase操作常用的JavaAPI。 三.实验过程截图及说明 1、...
并对HBase进行实战操作,讲解基本的shell命令及java api。重点讲解如何通过过滤器筛选数据... 3-1 HBase写流程 3-2 HBase读流程 3-3 HBase模块协作 3-4 HBase实战:Shell命令实战 3-5 HBase实 战:Java Api实现HBase...
(2)熟练使用HDFS操作常用的 Shell命令。(3)熟悉HDFS操作常用的Java API。 A.2.2 实验平台 (1)操作系统:Linux(建议Ubuntu 16.04)。(2) Hadoop版本:2.7.1。 (3)JDK版本:1.7或以上版本。(4) Java IDE:Eclipse。
Linux使用;Hadoop安装;HDFS常用操作;HDFS常用操作java api;MapReduce计数操作,排序操作;HBase操作shell命令,HBase操作java api
常用 Shell 命令 Java API 的使用 基于 Zookeeper 搭建 Hadoop 高可用集群 二、Hive 简介及核心概念 Linux 环境下 Hive 的安装部署 CLI 和 Beeline 命令行的基本使用 常用 DDL 操作 分区表和分桶表 视图和索引 常用 ...
HDFS 常用 Shell 命令 HDFS Java API 的使用 基于 Zookeeper 搭建 Hadoop 高可用集群 Hive Hive 简介及核心概念 Linux 环境下 Hive 的安装部署 Hive CLI 和 Beeline 命令行的基本使用 Hive 常用 DDL 操作 Hive 分区...
(1)理解4种数据库(MySQL、HBase,Redis和 MongoDB)的概念及不同点。(2)熟练使用4种数据库操作常用的 Shell命令。 (3)熟悉4种数据库操作常用的Java API。 A.4.2实验平台 (1)操作系统:Linux(建议Ubuntu 16.04)。(2...
2. 熟练使用四种数据库操作常用的Shell命令; 3. 熟悉四种数据库操作常用的Java API。 二、实验平台 1. 操作系统:Linux(建议Ubuntu16.04或Ubuntu18.04); 2. Hadoop版本:3.1.3; 3. MySQL版本:5.6; 4. HBase...
07-hbase集群架构及表存储机制.avi 08-hbase-shell.avi 09-hbase的java api.avi 第七天 storm+kafka 006-kafka整合storm.avi 01-storm基本概念.avi 02-storm编程规范及demo编写.avi 03-storm的topology提交...
1.理解四种数据库(MySQL,HBase,Redis,MongoDB)的概念以及不同点;2.熟练使用四种数据库操作常用的Shell命令;3.熟悉四种数据库操作常用的Java API。
RowKey设计,协处理,Shell访问,Java API访问和Hbase性能调优ElasticSearch入门,概念基础,基础原理,索引,映射,搜索,聚合和性能优化Scala入门,函数,类,对象,特征,模式匹配,常用类和方法,异常处理,编程...
Hbase存储原理、RowKey设计、协处理、Shell访问、Java API访问和Hbase性能调优 ElasticSearch入门、概念基础、基础原理、索引、映射、搜索、聚合和性能优化 Scala入门、函数、类、对象、特征、模式匹配、常用类和...
125_keep-deleted-cells控制是否保留删除的shell$ V8 |; Q7 g" ]- C# j% |! y 126_过滤器rowkey-family 127_过滤器-分页-row-col 128_filterList 129_rowkey2 h5 Y+ y9 _1 j0 K0 Q) n 130_区域观察者 131_区域观察者...
常见的分布式系统存储解决方案,包括MySQL的分布式扩展、HBase的API及使用场景、Redis的使用等。 如何使用分布式消息系统ActiveMQ来降低系统之间的耦合度,以及进行应用间的通信。 垂直化的搜索引擎在分布式...