spring-data-hadoop与HBase结合示例

原创
2016/09/09 23:51
阅读数 7.6K

##测试数据准备

关于测试数据的准备可以参考之前写的一篇文章 HBase单机模式和伪分布式模式安装和配置HBase简单操作 这节。

data-prepare

##示例程序

###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 也易于使用, 可自行动手实践。

展开阅读全文
加载中

作者的其它热门文章

打赏
0
9 收藏
分享
打赏
0 评论
9 收藏
0
分享
返回顶部
顶部