这里介绍使用java api来访问和操作HBase,例如create、delete、select、update等操作。

1.HBase配置

配置HBase使用的zookeeper集群地址和端口。

private static Configuration configuration;

static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "ZK1,ZK2,ZK3");
}

2.创建表

// 创建表
public static boolean create(String tableName, String columnFamily) {
HBaseAdmin admin = null;
try {
admin = new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
System.out.println(tableName + " exists!");
return false;
} else {
// 逗号分隔,可以有多个columnFamily
String[] cfArr = columnFamily.split(",");
HColumnDescriptor[] hcDes = new HColumnDescriptor[cfArr.length];
for (int i = 0; i < cfArr.length; i++) {
hcDes[i] = new HColumnDescriptor(cfArr[i]);
}
HTableDescriptor tblDes = new HTableDescriptor(TableName.valueOf(tableName));
for (HColumnDescriptor hc : hcDes) {
tblDes.addFamily(hc);
}
admin.createTable(tblDes);
System.out.println(tableName + " create successfully!");
return true;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

3.插入数据

指定表名、rowkey、cf、qualifier和value,插入数据到HBase。

public static boolean put(String tableName, String rowkey, String columnFamily, String qualifier, String value) {
try {
HTable table = new HTable(configuration, tableName);
Put put = new Put(rowkey.getBytes());
put.add(columnFamily.getBytes(), qualifier.getBytes(), value.getBytes());
table.put(put);
System.out.println("put successfully! " + rowkey + "," + columnFamily + "," + qualifier + "," + value);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

4.查询数据

4.1.查询指定rowkey的整条记录,返回Result对象。

// 查询
public static Result getResult(String tableName, String rowkey) {
System.out.println("get result. table=" + tableName + " rowkey=" + rowkey);
try {
HTable table = new HTable(configuration, tableName);
Get get = new Get(rowkey.getBytes());
return table.get(get);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

4.2.展现Result内容

// Result转换成Map形式,便于输出
private static Map<String, Object> result2Map(Result result) {
Map<String, Object> ret = new HashMap<String, Object>();
if (result != null && result.listCells() != null) {
for (Cell cell : result.listCells()) {
String key = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(key + " => " + value);
ret.put(key, value);
}
}
return ret;
}

4.3.指定qualifier查询数据

// 查询
public static byte[] get(String tableName, String rowkey, String qualifier) {
System.out.println("get result. table=" + tableName + " rowkey=" + rowkey + " qualifier=" + qualifier);
Result result = getResult(tableName, rowkey);
if (result != null && result.listCells() != null) {
for (Cell cell : result.listCells()) {
String key = Bytes.toString(CellUtil.cloneQualifier(cell));
if (key.equals(qualifier)) {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(key + " => " + value);
return CellUtil.cloneValue(cell);
}
}
}
return null;
}

5.查看全表数据

如下只要指定表名,就可以通过Scan来查看全表数据。

// 查看全表
public static List<Map<String, Object>> scan(String tableName) {
System.out.println("scan table " + tableName);
try {
HTable table = new HTable(configuration, tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
for (Result r : rs) {
Map<String, Object> m = result2Map(r);
StringBuilder sb = new StringBuilder();
for(String k : m.keySet()) {
sb.append(k).append("=>").append(m.get(k)).append(" ");
}
System.out.println(sb.toString());
resList.add(m);
}
return resList;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

6.列出HBase中所有表名

// 列出所有表
public static List<String> list() {
System.out.println("list tables.");
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
TableName[] tableNames = admin.listTableNames();
List<String> tblArr = new ArrayList<String>();
for (int i = 0; i < tableNames.length; i++) {
tblArr.add(tableNames[i].getNameAsString());
System.out.println("Table: " + tableNames[i].getNameAsString());
}
return tblArr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

7.删除指定qualifier内容

// 指定qualifier删除内容
public static boolean deleteQualifier(String tableName, String rowkey, String columnFamily, String qualifier) {
System.out.println("delete qualifier. table=" + tableName
+ " rowkey=" + rowkey + " cf=" + columnFamily + " qualifier=" + qualifier);
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
HTable table = new HTable(configuration, tableName);
Delete delete = new Delete(rowkey.getBytes());
delete.deleteColumn(columnFamily.getBytes(), qualifier.getBytes());
table.delete(delete);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

8.删除指定rowkey的记录

// 指定rowkey删除记录
public static boolean deleteRow(String tableName, String rowkey) {
System.out.println("delete row. table=" + tableName + " rowkey=" + rowkey);
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
HTable table = new HTable(configuration, tableName);
Delete delete = new Delete(rowkey.getBytes());
table.delete(delete);
}
System.out.println(tableName + ", " + rowkey + " delete successfully!");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

9.删除指定column family

// 删除columnfamily
public static boolean deleteColumnFamily(String tableName, String columnFamily) {
System.out.println("delete column family. table=" + tableName + " cf=" + columnFamily);
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteColumn(tableName, columnFamily);
admin.enableTable(tableName);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

10.删除表

删除指定表名。

// 删除表
public static boolean delete(String tableName) {
System.out.println("delete table " + tableName);
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

11.一个测试案例

public class HBaseTest {
public static void main(String[] args) {
HBaseUtils.create("test1", "cf1,cf2,cf3");
HBaseUtils.put("test1", "row1", "cf1", "field1", "value1");
HBaseUtils.put("test1", "row1", "cf1", "field2", "value2");
HBaseUtils.put("test1", "row1", "cf2", "field3", "value3");
HBaseUtils.put("test1", "row2", "cf1", "field4", "value4");
HBaseUtils.list();
HBaseUtils.get("test1", "row1");
HBaseUtils.get("test1", "row2", "cf1");
HBaseUtils.deleteRow("test1", "row2");
HBaseUtils.scan("test1");
// HBaseUtils.delete("test1");
HBaseUtils.list();
}
}

[原创]HBase学习笔记(3)- Java程序访问HBase的更多相关文章

  1. [原创] hadoop学习笔记:wordcout程序实践

    看了官网上的示例:但是给的不是很清楚,这里依托官网给出的示例,加上自己的实践,解析worcount程序的操作 1.首先你的确定你的集群正确安装,并且启动你的集群,应为这个是hadoop2.6.0,所以 ...

  2. JAVA API访问Hbase org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=32

    Java使用API访问Hbase报错: 我的hbase主节点是spark1   java代码访问hbase的时候写的是ip 结果运行程序报错 不能够识别主机名 修改主机名     修改主机hosts文 ...

  3. 学习笔记之Java程序设计实用教程

    Java程序设计实用教程 by 朱战立 & 沈伟 学习笔记之JAVA多线程(http://www.cnblogs.com/pegasus923/p/3995855.html) 国庆休假前学习了 ...

  4. HBASE学习笔记(四)

    这两天把要前几天的知识点回顾一下,接下来我会用自己对知识点的理解来写一些东西 一.知识点回顾 1.hbase集群启动:$>start-hbase.sh ===>hbase-daemon.s ...

  5. HBase学习笔记之HBase的安装和配置

    HBase学习笔记之HBase的安装和配置 我是为了调研和验证hbase的bulkload功能,才安装hbase,学习hbase的.为了快速的验证bulkload功能,我安装了一个节点的hadoop集 ...

  6. HBase学习笔记1 - 如何编写高性能的客户端Java代码

    转载请标注原链接:http://www.cnblogs.com/xczyd/p/5577124.html 客户在使用HBase的时候,经常会抱怨说写入太慢,并发上不去等等.从前我遇到这种情况,一般都二 ...

  7. HBase学习笔记——Java API操作

    1.1.  配置 HBaseConfiguration 包:org.apache.hadoop.hbase.HBaseConfiguration 作用:通过此类可以对HBase进行配置 用法实例: C ...

  8. [原创]HBase学习笔记(1)-安装和部署

    HBase安装和部署 使用的HBase版本是1.2.4 1.安装步骤(默认hdfs已安装好) # 下载并解压安装包 cd tools/ tar -zxf hbase-1.2.4-bin.tar.gz ...

  9. [原创]HBase学习笔记(4)- 数据导入

    需要分别从Oracle和文本文件往HBase中导入数据,这里介绍几种数据导入方案. 1.使用importTSV导入HBase importTSV支持增量导入.新数据插入,已存在数据则修改. 1.1.首 ...

随机推荐

  1. 最简单的排序算法之一冒泡排序----js实现

    1. 算法步骤 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.这步做完后,最后的元素会是最大的数. 针对所有的元素重复以上的步骤, ...

  2. table中td的宽度不随文字变宽

    1.设置了table的宽度后,宽度仍然不固定,td的内容一多,很容易吧table撑变形.有些时候我们需要设置固定的宽度. 解决办法 table的css 加入样式  table-layout:fixed ...

  3. 一篇知乎的故事 - javascript技术贴

    前言 就像文章题目所示,本文的发表源于知乎的一篇文章.文章链接如下:如果你想靠前端技术还房贷,你不能连这个都不会.这篇文章是群里水群时别人发的,像我这样的菜鸟角色才不会逛知乎~~~.这篇文章主要是讲了 ...

  4. php小测试,难点与分享

    B/S(网页程序) 网页结构,依托游览器 C/S(客户端程序) 单引号和双引号包含字符串的区别: 双引号里面可以解析变量,比如: $a=555; echo "你好{$a}"; 输出 ...

  5. ASP.NET MVC5 实现分页查询

    对于大量数据的查询和展示使用分页是一种不错的选择,这篇文章简要介绍下自己实现分页查询的思路. 分页需要三个变量:数据总量.每页显示的数据条数.当前页码. //数据总量 int dataCount; / ...

  6. ZwQueryVirtualMemory枚举进程模块

    ZwQueryVirtualMemory算是枚举进程方法中的黑科技吧,主要是该方法可以检测出隐藏的模块(类似IceSword). 代码VS2015测试通过 再次奉上源码链接:https://githu ...

  7. javah的使用

    2.我们要开始写javah的命令,以便生成对应的C语言头文件 D:\我的文档\workspace\PrepareForExam\src>javah -classpath D:\我的文档\work ...

  8. 谈 jquery中.band() .live() .delegate() .on()的区别

    bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 $("a").bind("click",function(){alert(& ...

  9. Android网络编程要学的东西与Http协议学习

    本节引言: 本节开始我们来学习Android网络编程相关的一些东西:Android端网络编程是要干嘛?http协议的学习,使用自带扣脚Json解析类解析Json,XML解析常用的几种方式,HttpUr ...

  10. Git commit message和工作流规范

    目的 统一团队Git commit日志标准,便于后续代码review,版本发布以及日志自动化生成等等. 统一团队的Git工作流,包括分支使用.tag规范.issue等 Git commit日志参考案例 ...