spring-data-hadoop与HBase结合示例
2016-09-09 23:51
585 查看
##测试数据准备
关于测试数据的准备可以参考之前写的一篇文章 HBase单机模式和伪分布式模式安装和配置 中 HBase简单操作 这节。
![](https://static.oschina.net/uploads/img/201609/09225728_RKBO.png)
##示例程序
###pom.xml
要想使用 Spring for Apache Hadoop 与 HBase 进行交互,pom.xml 文件需要配置如下:
###Spring配置文件
创建一个 Spring 配置文件 spring-hbase.xml,在该文件中配置与 HBase 连接相关的信息。spring-hbase.xml 有两种配置方式:一种是直接指定 HDFS 地址以及 ZooKeeper 的地址和端口号,另外一种就是引入 HBase 配置文件 hbase-site.xml。下面就这两种方式分开进行描述。
####直接指定
####引入hbase-site.xml
hbase-site.xml配置文件内容如下:
spring-hbase.xml配置如下:
###测试代码
输出结果是:value1。
##总结
从上面的示例可以看到 Spring for Apache Hadoop 与 HBase 结合比较简单,API 也易于使用, 可自行动手实践。
关于测试数据的准备可以参考之前写的一篇文章 HBase单机模式和伪分布式模式安装和配置 中 HBase简单操作 这节。
![](https://static.oschina.net/uploads/img/201609/09225728_RKBO.png)
##示例程序
###pom.xml
要想使用 Spring for Apache Hadoop 与 HBase 进行交互,pom.xml 文件需要配置如下:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <hbase.version>1.2.2</hbase.version> <spring-data-hadoop.version>2.4.0.RELEASE</spring-data-hadoop.version> </properties> <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-hadoop</artifactId> <version>${spring-data-hadoop.version}</version> </dependency> </dependencies>
###Spring配置文件
创建一个 Spring 配置文件 spring-hbase.xml,在该文件中配置与 HBase 连接相关的信息。spring-hbase.xml 有两种配置方式:一种是直接指定 HDFS 地址以及 ZooKeeper 的地址和端口号,另外一种就是引入 HBase 配置文件 hbase-site.xml。下面就这两种方式分开进行描述。
####直接指定
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop-2.3.xsd"> <!-- HDFS配置 --> <hdp:configuration id="hadoopConfiguration"> fs.defaultFS="hdfs://localhost:9000" </hdp:configuration> <!-- HBase连接配置 --> <hdp:hbase-configuration id="hbaseConfiguration" zk-quorum="127.0.0.1" zk-port="2181"/> <!-- HbaseTemplate Bean配置--> <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate" p:configuration-ref="hbaseConfiguration"/> </beans>
####引入hbase-site.xml
hbase-site.xml配置文件内容如下:
<configuration> <!-- 使用集群模式 --> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <!-- HDFS 地址 --> <property> <name>hbase.rootdir</name> <value>hdfs://localhost:9000/hbase</value> </property> <!-- ZooKeeper 地址 --> <property> <name>hbase.zookeeper.quorum</name> <value>localhost</value> </property> <!-- ZooKeeper 端口号 --> <property> <name>hbase.zookeeper.property.clientPort</name> <value>2181</value> </property> <!-- ZooKeeper 数据文件路径 --> <property> <name>hbase.zookeeper.property.dataDir</name> <value>/opt/zookeeper/data</value> </property> </configuration>
spring-hbase.xml配置如下:
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop-2.3.xsd"> <!-- 直接引入类路径上的hbase-site.xml,也可以引入文件系统路径下hbase-site.xml,如:file:/opt/hbase/conf/hbase-site.xml --> <hdp:configuration id="hadoopConfiguration" resources="classpath:hbase-site.xml"/> <hdp:hbase-configuration id="hbaseConfiguration" configuration-ref="hadoopConfiguration"/> <!-- HbaseTemplate Bean配置--> <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate" p:configuration-ref="hbaseConfiguration"/> </beans>
###测试代码
import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; 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; public class HBaseTest { private static final String TABLE_NAME = "test"; private static final String ROW_KEY = "row1"; private static final String COLUMN_FAMILY = "cf"; private static final String QUALIFIER = "a"; public static void main(String[] args) { // 加载Spring配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-hbase.xml"); // 获取HbaseTemplate HbaseTemplate hbaseTemplate = (HbaseTemplate) applicationContext.getBean("hbaseTemplate"); // 通过表名和rowKey获取最近一行数据 String result = hbaseTemplate.get(TABLE_NAME, ROW_KEY, new RowMapper<String>() { public String mapRow(Result result, int rowNum) throws Exception { return Bytes.toString(result.getValue(COLUMN_FAMILY.getBytes(), QUALIFIER.getBytes())); } }); System.out.println(result); } }
输出结果是:value1。
##总结
从上面的示例可以看到 Spring for Apache Hadoop 与 HBase 结合比较简单,API 也易于使用, 可自行动手实践。
相关文章推荐
- spring-data-hadoop 配置 hbase 问题汇总
- java 获取 hbase数据 springdatahadoop -- hbasetemplate
- Hbase总结(三)--使用spring-data-hadoop进行hbase的读写操作
- java 获取 hbase数据 springdatahadoop -- hbasetemplate
- spring data redis使用示例
- WEB项目整合spring-data-hbase出现NoSuchMethodError: PageContextImpl.evaluateExpression
- Spring Data Jpa示例(IntelliJ maven项目)
- Spring.net与Asp.net Mvc结合入门示例
- crawler with data analysis (Hadoop, MapReduce, HBase) - Phase I - Data Modeling
- Thinking in BigData(八)大数据Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解
- hadoop 使用 -- spring-data-hadoop
- Hbase与Hadoop结合出现的jar包冲突问题
- hadoop datanode 重新启动和hbase regionserver重新启动
- Thinking in BigData(八)大数据Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解
- spring-data-hadoop使用--maven配置详解
- 单独启动Hadoop datanode和hbase regionserver
- 单独启动hadoop datanode和hbase regionserver
- 【转载】Spring.net与Asp.net Mvc结合入门示例
- Creating Hadoop MapReduce Job with Spring Data Apache Hadoop
- hadoop2.4.1分布式安装结合hbase0.94.23