Java API 实现HBase的数据添加与过滤查询
包依赖比较麻烦,找了好久,我用的CDH5.0 现将所依赖的包的列表清单如下:

public class EmployeeDao {
/**
* @param args
*/
public static Configuration configuration;
static {
configuration = new Configuration();
String filePath = "hbase-site.xml";
Path path = new Path(filePath);
configuration.addResource(path);
configuration = HBaseConfiguration.create(configuration);
}
//使用过滤器实现查询
public List<Employee> QueryEmployeeByName(String name) throws Exception {
String rowKey = name;
HTable table = new HTable(configuration, "employee".getBytes());
Scan scan = new Scan();
// select ..... from .. where filter
scan.addColumn("info".getBytes(), "age".getBytes());
scan.addColumn("info".getBytes(), "sex".getBytes());
RowFilter filter = new RowFilter(CompareOp.EQUAL,
new RegexStringComparator(rowKey));
scan.setFilter(filter);
List<Employee> list = new ArrayList<Employee>();
ResultScanner rScanner = table.getScanner(scan);
for (Result rs : rScanner) {
Employee e = new Employee();
for (KeyValue kValue : rs.list()) {
if ("sex".equalsIgnoreCase(new String(kValue.getQualifier()))) {
e.setSex(new String(kValue.getValue()));
}
if ("age".equalsIgnoreCase(new String(kValue.getQualifier()))) {
e.setAge(Integer.parseInt((new String(kValue.getValue()))));
}
}
list.add(e);
}
return list;
}
// 插入一个单元格数据
public static void insertOneRow(String tableName, String rowkey,
String columnFamily, String column, String value) throws Exception {
HTable table = new HTable(configuration, tableName);
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(value));
table.put(put);// 放入表
table.close();// 释放资源
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
EmployeeDao dao = new EmployeeDao();
List<Employee> l = dao.QueryEmployeeByName("oftenlin");
for (Employee e : l) {
System.out.println("Name:oftenlin," + "Sex:" + e.getSex() + ",Age:"+ e.getAge());
}
//insertOneRow("employee","allen","info","scholl","shi yan mid school");
insertOneRow("employee","gold fly","info","scholl","shi yan mid school");
System.out.println("写入成功!");
}
}
hbase-site.xml 的清单
<?xml version="1.0" encoding="UTF-8"?>
<!--Autogenerated by Cloudera Manager-->
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://CentOS-cm:8020/hbase</value>
</property>
<property>
<name>hbase.client.write.buffer</name>
<value>2097152</value>
</property>
<property>
<name>hbase.client.pause</name>
<value>100</value>
</property>
<property>
<name>hbase.client.retries.number</name>
<value>35</value>
</property>
<property>
<name>hbase.client.scanner.caching</name>
<value>100</value>
</property>
<property>
<name>hbase.client.keyvalue.maxsize</name>
<value>10485760</value>
</property>
<property>
<name>hbase.rpc.timeout</name>
<value>60000</value>
</property>
<property>
<name>hbase.snapshot.enabled</name>
<value>true</value>
</property>
<property>
<name>hbase.security.authentication</name>
<value>simple</value>
</property>
<property>
<name>zookeeper.session.timeout</name>
<value>60000</value>
</property>
<property>
<name>zookeeper.znode.parent</name>
<value>/hbase</value>
</property>
<property>
<name>zookeeper.znode.rootserver</name>
<value>root-region-server</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>CentOS-server3,CentOS-server2,CentOS-server1</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>
Java API 实现HBase的数据添加与过滤查询的更多相关文章
- HBase 6、用Phoenix Java api操作HBase
开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...
- 通过Java Api与HBase交互(转)
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- 通过Java Api与HBase交互
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...
- HBase总结(十二)Java API 与HBase交互实例
HBase提供了Java Api的訪问接口,掌握这个就跟Java应用使用RDBMS时须要JDBC一样重要 import java.io.IOException; import org.apache.h ...
- JAVA API访问Hbase org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=32
Java使用API访问Hbase报错: 我的hbase主节点是spark1 java代码访问hbase的时候写的是ip 结果运行程序报错 不能够识别主机名 修改主机名 修改主机hosts文 ...
- Java API 操作HBase Shell
HBase Shell API 操作 创建工程 本实验的环境实在ubuntu18.04下完成,首先在改虚拟机中安装开发工具eclipse. 然后创建Java项目名字叫hbase-test 配置运行环境 ...
- Java Api与HBase交互实例
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hb ...
- linux 下通过过 hbase 的Java api 操作hbase
hbase版本:0.98.5 hadoop版本:1.2.1 使用自带的zk 本文的内容是在集群中创建java项目调用api来操作hbase,主要涉及对hbase的创建表格,删除表格,插入数据,删除数据 ...
随机推荐
- 416. Partition Equal Subset Sum
题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...
- supersr--图形上下文的注意点
- (void)test { // 不要自己调用drawRect:方法的原因: // 当系统调用drawRect:方法之前, 会创建一个与当前UIView的layer相关的图形上下文, 这样就可以保证 ...
- python基础——返回函数
python基础——返回函数 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: def calc_ ...
- iOS - 开发类库
开发类库 UI 项目名称 项目信息 1.MJRefresh 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明. ...
- db2 bind on luw
https://www.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.embed.doc/doc/c000556 ...
- 1.2 容器-container
1) * 容器是用于存放数据的类模板,实例化后就是容器类.用容器定义的对象称为容器对象. **类型相同的容器可以进行比较运算 2)分类 容器可分为顺序容器和关联容器两大类. *:顺序容器 元素的位置 ...
- PHP之MVC项目实战(三)
本文主要包括以下内容 标准错误错误处理 http操作 PDO 文件操作 标准错误错误处理 PHP在语法层面上发生的错误 两个过程: 触发阶段(发生一个错误) 处理阶段(如何处理该错误) 触发阶段 系统 ...
- Stuts2的"struts.devMode"设置成true后,不起作用,仍需要重启tomcat
在项目的struts.xml加入了常量配置:<constant name="struts.devMode" value="true" />后,重启服 ...
- linux cpuInfo
转自:http://blog.csdn.net/lgstudyvc/article/details/7889364 /proc/cpuinfo文件分析 在Linux系统中,提供了proc文件系统显 ...
- 【JAVA集合框架之Set】
一.Set概述. Set集合的特点是元素不允许重复,而且是无序的(添加和取出的顺序不一致). Set接口中的方法和Collection接口中的方法几乎相同,略. Set接口下常用的两个类:HashSe ...