Spring整合HBase
Spring整合HBase
§ 系统环境
Ubuntu
Hadoop 2.7.3
HBase 1.2.3
JDK 1.8
Windows
IDEA 16
Spring 4.3.2.RELEASE
Spring Data Hadoop 2.4.0.RELEASE
§ 配置HBase运行环境
Hadoop和HBase都配置运行在Ubuntu虚拟机中,HBase以伪分布式模式运行,使用HDFS作为存储系统。
首先需要安装必须的软件:
- $ apt-get install ssh
- $ apt-get install rsync
并确保ssh可以不使用口令就能够连接localhost。设置方式如下:
- $ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
- $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- $ chmod 0600 ~/.ssh/authorized_keys
查看Ubuntu的IP地址,然后编辑/etc/hosts,注释掉其他主机映射内容,将<ip>和<domain>替换为本机IP地址和自定义域:
- #127.0.0.1 localhost
- #127.0.1.1 <domain>
- <ip> <domain> localhost
§ 配置Hadoop
以<HADOOP_DIR>表示Hadoop根目录,编辑<HADOOP_DIR>/etc/hadoop/hadoop-env.sh,修改JAVA_HOME为JDK安装目录。
编辑配置文件<HADOOP_DIR>/etc/hadoop/core-site.xml:
- <configuration>
- <property>
- <name>fs.defaultFS</name>
- <value>hdfs://<ip>:9000</value>
- </property>
- <property>
- <name>hadoop.tmp.dir</name>
- <value>/home/<user>/data</value>
- </property>
- </configuration>
编辑配置文件<HADOOP_DIR>/etc/hadoop/hdfs-site.xml:
- <configuration>
- <property>
- <name>dfs.replication</name>
- <value>1</value>
- </property>
- </configuration>
§ 配置HBase
以<HBASE_DIR>表示HBase根目录,修改<HBASE_DIR>/conf/hbase-env.sh,添加JAVA_HOME。
编辑配置文件<HBASE_DIR>/conf/hbase-site.xml,替换ip和domain为对应内容:
- <configuration>
- <property>
- <name>hbase.cluster.distributed</name>
- <value>true</value>
- </property>
- <property>
- <name>hbase.rootdir</name>
- <value>hdfs://<ip>:9000/hbase</value>
- </property>
- <property>
- <name>hbase.zookeeper.quorum</name>
- <value><domain></value>
- </property>
- <property>
- <name>hbase.zookeeper.property.clientPort</name>
- <value>2181</value>
- </property>
- </configuration>
§ 启动Hadoop和HBase
启动Hadoop前先格式化文件系统:
- $ ./<HADOOP_DIR>/bin/hdfs namenode -format
然后启动Hadoop和HBase:
- $ ./<HADOOP_DIR>/sbin/start-dfs.sh
- $ ./<HBASE_DIR>/bin/start-hbase.sh
可以使用jps命令查看所有启动的进程:
- $ jps
- 2786 NameNode
- 2914 DataNode
- 6259 HQuorumPeer
- 6324 HMaster
- 3083 SecondaryNameNode
- 6411 HRegionServer
§ 创建Maven项目
新建Maven项目,所需依赖如下:
- <properties>
- <slf4j.version>1.7.21</slf4j.version>
- <spring.version>4.3.2.RELEASE</spring.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-hadoop</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- </exclusion>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-auth</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.hbase</groupId>
- <artifactId>hbase-client</artifactId>
- <version>1.2.3</version>
- <scope>compile</scope>
- <exclusions>
- <exclusion>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- </exclusion>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl-over-slf4j</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- </dependency>
- </dependencies>
将HBase的配置文件hbase-site.xml复制到resources下,新建Spring配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
- xmlns:hdp="http://www.springframework.org/schema/hadoop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
- <context:annotation-config/>
- <context:component-scan base-package="com.sample.hbase"/>
- <hdp:configuration resources="hbase-site.xml"/>
- <hdp:hbase-configuration configuration-ref="hadoopConfiguration"/>
- <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
- <property name="configuration" ref="hbaseConfiguration"/>
- </bean>
- </beans>
新建测试用例:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
- public class BaseTest {
- @Autowired
- private HbaseTemplate template;
- @Test
- public void testFind() {
- List<String> rows = template.find("user", "cf", "name", new RowMapper<String>() {
- public String mapRow(Result result, int i) throws Exception {
- return result.toString();
- }
- });
- Assert.assertNotNull(rows);
- }
- @Test
- public void testPut() {
- template.put("user", "1", "cf", "name", Bytes.toBytes("Alice"));
- }
- }
整合完成。
Spring整合HBase的更多相关文章
- NoSql存储日志数据之Spring+Logback+Hbase深度集成
NoSql存储日志数据之Spring+Logback+Hbase深度集成 关键词:nosql, spring logback, logback hbase appender 技术框架:spring-d ...
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- Spring整合Ehcache管理缓存
前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- MyBatis学习(四)MyBatis和Spring整合
MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
- Spring整合Ehcache管理缓存(转)
目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...
随机推荐
- CryptoJS DES加密
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equ ...
- Uva 10891 经典博弈区间DP
经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...
- c++多线程の死锁与防止死锁
如果有两把锁 lock1(mutex_gard 方式)和lock: 两者的调用顺序不同,会出现相互等待的情况,从而造成死锁: 为了避免死锁,我们可以: 1.每个线程中锁的调用顺序都相同: 2.使用st ...
- 。linux桌面与命令行
1.输入用户名和密码登录到系统2.vi /etc/inittab3.修改id:后对应的值为5(桌面模式),id:后对应的值改成3(命令行模式)先用命令#startx启动到桌面模式,然后 Ctrl + ...
- Spark学习(一) -- Spark安装及简介
标签(空格分隔): Spark 学习中的知识点:函数式编程.泛型编程.面向对象.并行编程. 任何工具的产生都会涉及这几个问题: 现实问题是什么? 理论模型的提出. 工程实现. 思考: 数据规模达到一台 ...
- Analyzer报表里显示的 * 星号、红叉、#井号的意义
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Hibernate的性能优化问题
本文是根据Hibernate帮助文档,以及一些书籍及项目经验整理而成,只提供要点和思路,具体做法可以留言探讨,或是找一些更详细更有针对性的资料. 初用Hibernate的人也许都遇到过性能问题,实现同 ...
- Oracle创建数据库
Oracle创建数据库有三种方式:一.使用DBCA(Database Configuration Assistant 数据库配置助手):二.使用 create database指令:三.在安装数据库软 ...
- TensorFlow支持windows了
(留坑)找个时间测试一下. 终于来了,TensorFlow 新增官方 Windows 支持
- Fair Scheduler 队列设置经验总结
Fair Scheduler 队列设置经验总结 由于公司的hadoop集群的计算资源不是很充足,需要开启yarn资源队列的资源抢占.在使用过程中,才明白资源抢占的一些特点.在这里总结一下. 只有一个队 ...