java 获取 hbase数据 springdatahadoop -- hbasetemplate
2017-04-07 14:56
453 查看
原博客地址:http://blog.csdn.net/linlinv3/article/details/42737113
配置文件
1、建立 spring-hbase.xml 获取连接池
第一种:直接在配置文件注明 hdfs的端口和zk的地址以及端口进行连接
[html] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/hadoop
http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder location="hbase.properties" />
<!-- 配置HbaseTemplate -->
<bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration">
</property>
</bean>
<!-- 配置hadoop的基本信息 -->
<hdp:configuration>
fs.default.name="hdfs://192.168.0.173:8082"
</hdp:configuration>
<!-- 配置zookeeper地址和端口 -->
<hdp:hbase-configuration zk-quorum="192.168.0.173" zk-port="2181" />
</beans>
第二种:只需要配置zk的地址和端口,但是需要在classpath另外新增 hbase-site.xml的配置
spring-hbase.xml 配置:
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
<!-- 配置zookeeper的信息,远程连接hbase时使用 -->
<hdp:configuration resources="classpath:/hbase-site.xml" />
<hdp:hbase-configuration configuration-ref="hadoopConfiguration" />
<!-- 配置HbaseTemplate -->
<bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration">
</property>
<property name="encoding" value="UTF-8"></property>
</bean>
</beans>
hbase-site.xml 配置 放在classpath下(直接扔到src下即可)
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.zookeeper.quorum</name>
<value>192.168.0.173</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>
配置完成
程序代码
官网的api地址已经在上面发过了,这里只做了一个简单的demo,列举了简单的 get find execue 方法
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
package cn.fulong.hbase;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.RowMapper;
import org.springframework.data.hadoop.hbase.TableCallback;
import cn.fulong.view.HbaseModel;
public class HbaseTest {
//ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "application_hbase.xml" });
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring_hbase.xml" });
BeanFactory factory = (BeanFactory) context;
HbaseTemplate htemplate = (HbaseTemplate) factory.getBean("htemplate");
Map hMap = new HashMap<String, List<HbaseModel>>();
public String key;
public String familyName ;
public String qualifier;
public String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public String getQualifier() {
return qualifier;
}
public void setQualifier(String qualifier) {
this.qualifier = qualifier;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public static void main(String[] args) {
//PrefixFilter
HbaseTest h = new HbaseTest();
for(int i=0;i<=10000;i++){
h.setKey("linlin"+i);
h.setFamilyName("info");
h.setQualifier("service");
h.setValue(i+"技术创新和质量服务");
h.execute("linlintest", null);
}
List<Map<String,Object>> mapList1 = h.find("linlintest",null,null);
System.out.println("2");
}
/**
* 写数据
* @param tableName
* @param action
* @return
*/
public Boolean execute(String tableName, TableCallback<Boolean> action) {
return htemplate.execute(tableName, new TableCallback<Boolean>() {
public Boolean doInTable(HTableInterface table) throws Throwable {
boolean flag = false;
try{
byte[] rowkey = key.getBytes();
Put put = new Put(rowkey);
put.add(Bytes.toBytes(familyName),Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
flag = true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
});
}
/**
* 通过表名和key获取一行数据
* @param tableName
* @param rowName
* @return
*/
public Map<String, Object> get(String tableName, String rowName) {
return htemplate.get(tableName, rowName,new RowMapper<Map<String,Object>>(){
public Map<String,Object> mapRow(Result result, int rowNum) throws Exception {
List<Cell> ceList = result.listCells();
Map<String,Object> map = new HashMap<String, Object>();
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
map.put(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+
"_"+Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()),
Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
}
}
return map;
}
});
}
/**
* 通过表名 key 和 列族 和列 获取一个数据
* @param tableName
* @param rowName
* @param familyName
* @param qualifier
* @return
*/
public String get(String tableName ,String rowName, String familyName, String qualifier) {
return htemplate.get(tableName, rowName,familyName,qualifier ,new RowMapper<String>(){
public String mapRow(Result result, int rowNum) throws Exception {
List<Cell> ceList = result.listCells();
String res = "";
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
res = Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
}
}
return res;
}
});
}
/**
* 通过表名,开始行键和结束行键获取数据
* @param tableName
* @param startRow
* @param stopRow
* @return
*/
public List<Map<String,Object>> find(String tableName , String startRow,String stopRow) {
Scan scan = new Scan();
if(startRow==null){
startRow="";
}
if(stopRow==null){
stopRow="";
}
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
/* PageFilter filter = new PageFilter(5);
scan.setFilter(filter);*/
return htemplate.find(tableName, scan,new RowMapper<Map<String,Object>>(){
public Map<String,Object> mapRow(Result result, int rowNum) throws Exception {
List<Cell> ceList = result.listCells();
Map<String,Object> map = new HashMap<String,Object>();
Map<String,Map<String,Object>> returnMap = new HashMap<String,Map<String,Object>>();
String row = "";
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
row =Bytes.toString( cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String value =Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
String family = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
String quali = Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
map.put(family+"_"+quali, value);
}
map.put("row",row );
}
return map;
}
});
}
/* public void scanValueByFilter(String tableName,String row) throws IOException{
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
scan.setFilter(new PrefixFilter(row.getBytes()));
ResultScanner resultScanner = table.getScanner(scan);
for(Result rs:resultScanner){
}
}*/
}
下载
具体可运行代码下载地址 http://download.csdn.net/detail/linlinv3/8580185
项目目录结构和所需jar包如下图所示:
配置文件
1、建立 spring-hbase.xml 获取连接池
第一种:直接在配置文件注明 hdfs的端口和zk的地址以及端口进行连接
[html] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/hadoop
http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder location="hbase.properties" />
<!-- 配置HbaseTemplate -->
<bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration">
</property>
</bean>
<!-- 配置hadoop的基本信息 -->
<hdp:configuration>
fs.default.name="hdfs://192.168.0.173:8082"
</hdp:configuration>
<!-- 配置zookeeper地址和端口 -->
<hdp:hbase-configuration zk-quorum="192.168.0.173" zk-port="2181" />
</beans>
第二种:只需要配置zk的地址和端口,但是需要在classpath另外新增 hbase-site.xml的配置
spring-hbase.xml 配置:
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
<!-- 配置zookeeper的信息,远程连接hbase时使用 -->
<hdp:configuration resources="classpath:/hbase-site.xml" />
<hdp:hbase-configuration configuration-ref="hadoopConfiguration" />
<!-- 配置HbaseTemplate -->
<bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration">
</property>
<property name="encoding" value="UTF-8"></property>
</bean>
</beans>
hbase-site.xml 配置 放在classpath下(直接扔到src下即可)
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.zookeeper.quorum</name>
<value>192.168.0.173</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>
配置完成
程序代码
官网的api地址已经在上面发过了,这里只做了一个简单的demo,列举了简单的 get find execue 方法
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
package cn.fulong.hbase;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.RowMapper;
import org.springframework.data.hadoop.hbase.TableCallback;
import cn.fulong.view.HbaseModel;
public class HbaseTest {
//ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "application_hbase.xml" });
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring_hbase.xml" });
BeanFactory factory = (BeanFactory) context;
HbaseTemplate htemplate = (HbaseTemplate) factory.getBean("htemplate");
Map hMap = new HashMap<String, List<HbaseModel>>();
public String key;
public String familyName ;
public String qualifier;
public String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public String getQualifier() {
return qualifier;
}
public void setQualifier(String qualifier) {
this.qualifier = qualifier;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public static void main(String[] args) {
//PrefixFilter
HbaseTest h = new HbaseTest();
for(int i=0;i<=10000;i++){
h.setKey("linlin"+i);
h.setFamilyName("info");
h.setQualifier("service");
h.setValue(i+"技术创新和质量服务");
h.execute("linlintest", null);
}
List<Map<String,Object>> mapList1 = h.find("linlintest",null,null);
System.out.println("2");
}
/**
* 写数据
* @param tableName
* @param action
* @return
*/
public Boolean execute(String tableName, TableCallback<Boolean> action) {
return htemplate.execute(tableName, new TableCallback<Boolean>() {
public Boolean doInTable(HTableInterface table) throws Throwable {
boolean flag = false;
try{
byte[] rowkey = key.getBytes();
Put put = new Put(rowkey);
put.add(Bytes.toBytes(familyName),Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
flag = true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
});
}
/**
* 通过表名和key获取一行数据
* @param tableName
* @param rowName
* @return
*/
public Map<String, Object> get(String tableName, String rowName) {
return htemplate.get(tableName, rowName,new RowMapper<Map<String,Object>>(){
public Map<String,Object> mapRow(Result result, int rowNum) throws Exception {
List<Cell> ceList = result.listCells();
Map<String,Object> map = new HashMap<String, Object>();
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
map.put(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+
"_"+Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()),
Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
}
}
return map;
}
});
}
/**
* 通过表名 key 和 列族 和列 获取一个数据
* @param tableName
* @param rowName
* @param familyName
* @param qualifier
* @return
*/
public String get(String tableName ,String rowName, String familyName, String qualifier) {
return htemplate.get(tableName, rowName,familyName,qualifier ,new RowMapper<String>(){
public String mapRow(Result result, int rowNum) throws Exception {
List<Cell> ceList = result.listCells();
String res = "";
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
res = Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
}
}
return res;
}
});
}
/**
* 通过表名,开始行键和结束行键获取数据
* @param tableName
* @param startRow
* @param stopRow
* @return
*/
public List<Map<String,Object>> find(String tableName , String startRow,String stopRow) {
Scan scan = new Scan();
if(startRow==null){
startRow="";
}
if(stopRow==null){
stopRow="";
}
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
/* PageFilter filter = new PageFilter(5);
scan.setFilter(filter);*/
return htemplate.find(tableName, scan,new RowMapper<Map<String,Object>>(){
public Map<String,Object> mapRow(Result result, int rowNum) throws Exception {
List<Cell> ceList = result.listCells();
Map<String,Object> map = new HashMap<String,Object>();
Map<String,Map<String,Object>> returnMap = new HashMap<String,Map<String,Object>>();
String row = "";
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
row =Bytes.toString( cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String value =Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
String family = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
String quali = Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
map.put(family+"_"+quali, value);
}
map.put("row",row );
}
return map;
}
});
}
/* public void scanValueByFilter(String tableName,String row) throws IOException{
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
scan.setFilter(new PrefixFilter(row.getBytes()));
ResultScanner resultScanner = table.getScanner(scan);
for(Result rs:resultScanner){
}
}*/
}
下载
具体可运行代码下载地址 http://download.csdn.net/detail/linlinv3/8580185
项目目录结构和所需jar包如下图所示:
相关文章推荐
- java 获取 hbase数据 springdatahadoop -- hbasetemplate
- 大数据架构开发 挖掘分析 Hadoop HBase Hive Storm Spark Java Flume ZooKeeper Kafka Redis MongoDB 机器学习 云计算 视频教程
- spring-boot获取form-data数据及json数据
- 大数据相关书籍(包含Java, Scala, R, Linux, Spark, Hadoop, Hive, Hbase, Sqoop, Flume, Strom)
- springboot框架中使用java操作kafka获取数据
- 大数据之mongodb --> (2)java集成 MongoDB 3.2,使用Spring-data-mongodb进行集成
- windows环境下java开发连接linux环境的hbase数据获取CURD
- Java连接HBASE数据库,创建一个表,删除一张表,修改表,输出插入,修改,数据删除,数据获取,显示表信息,过滤查询,分页查询,地理hash
- 大数据开发利器:Hadoop(6)-HBase第三讲 Java 开发基础
- spring-data-hadoop与HBase结合示例
- Thinking in BigData(八)大数据Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解
- spark学习-53-Spark下Java版HBase下的根据权重获取最真实数据
- Spring MVC和Spring Data JPA之获取数据表数据放在List集合,显示在JSP页面
- HBase table SCan Data 获取数据
- Hbase总结(三)--使用spring-data-hadoop进行hbase的读写操作
- spring-data-hadoop 配置 hbase 问题汇总
- java客户端通过get方法获取Hbase的数据
- java SpringMVC + Mybatis 一套前台到后台完整获取数据的步骤
- 【Hadoop】HBase 数据模型(Data Model)
- Java---post获取选中路灯的数据时,result.message.dataOriginalSelect结果为undefined;