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的创建表格,删除表格,插入数据,删除数据 ...
随机推荐
- stm32——NFC芯片--PN532的使用
stm32——NFC芯片--PN532的使用 一.NFC简介 NFC(Near Field Communication)近场通信,是一种短距高频的无线电技术,在13.56MHz频率运行于20厘米距离内 ...
- XMPP框架下微信项目总结(4)重新连接服务器
xmpp 很多功能是面向模块开发的 例如电子名片 无须自己去写请求的代码XMPP(文件)->Extension(存放的是各个模块)->Reconnect(自动连接模块) ...
- 413. Arithmetic Slices
/**************************Sorry. We do not have enough accepted submissions.*********************** ...
- js函数动态传参
js函数体内可以通过arguments对象来接收传递进来的参数,利用这一对象属性可以动态传参. function box() { return arguments[0]+' | '+arguments ...
- iOS,Objective-C,相册功能的实现。
#import "ViewController.h" #define kuan [UIScreen mainScreen].bounds.size.width #define ga ...
- nginx 配一个简单的静态文件服务器 和一个虚似机
下面是个图片服务器: server { listen ; server_name img.xxx.xxx.com; root /data/site/img.xxx.xxx.com; access_lo ...
- linux中解决SSH连接慢问题 关键点GSSAPIAuthentication
[root@ok 6FE5-D831]# ssh -v xxx.xxx.xxx.64 OpenSSH_5.3p1, OpenSSL Feb debug1: Reading configuration ...
- Linux系统入门学习:在curl中设置自定义的HTTP头
http://www.linuxidc.com/Linux/2015-02/114220.htm
- ARPPING
http://www.tuicool.com/articles/M7B3umj http://lixcto.blog.51cto.com/4834175/1571838/
- C语言中do...while(0)的妙用
在linux内核代码中,经常看到do...while(0)的宏,do...while(0)有很多作用,下面举出几个: 1.避免goto语句: 通常,如果一个函数开始要分配一些资源,然后如果在中途遇到错 ...