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 ...
随机推荐
- [mysql]throw exception
CREATE PROCEDURE pro_throwException (errorCode char(5), errorMessage text) BEGIN SIGNAL SQLSTATE err ...
- Flags-Ural1225简单递推
Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...
- iOS开发,URL编码和解码
URL传递数据中,如果含有中文,需要进行编码: NSString *urlEncodeString = [urlStr stringByAddingPercentEncodingWithAllowed ...
- 第一个python实例--监控cpu
#第一个python实例:监控cpu #/bin/bash/env Python from __future__ import print_function from collections impo ...
- tsne官方论文代码解读和使用
MLGB,人生就是矫情,充满冲动,充满热恋. tsne的08年的论文看了几遍,发现原理还是蛮简单的,能想到还是不容易(人生的战场是星辰大海,但我们的贡献就是也就是宇宙尘埃) 怎么说呢,现在真的是一个好 ...
- tomcat部署web项目的3中方法
1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法. 2.在tomcat安装目录中有一个conf文件夹,打开此文件夹,其 ...
- iOS_XCode7_Launch Image 的初使用
之前一直没有做笔记的习惯,后来发现很多知识不常用,容易忘了,再去网上找的,很浪费时间,现在尝试着自己写写博客,可以是自己笔记,方便以后查询,如果有纰漏,欢迎指正. 1.Xcode7中 ,assets. ...
- kali Linux添加add-apt-repository
Debian让用户可以通过一个名为add-apt-repository的应用程序,添加和使用PPA软件库,不过Kali Linux在其默认的程序包列表中并不含有该应用程序.就Kali而言,由于这是个特 ...
- SQL知识整理三:变量、全局变量、视图、事务、异常
变量 1.局部变量的声明(一个@) declare @n int --声明变量关键字为declare 然后@加变量名 后面是变量类型 declare @s varchar(36) 2 ...
- [Amazon] Amazon IAP for Unity
1> 下载amazon IAP3.0 for unity plugin 2> 根据 https://developer.amazon.com/public/apis/earn/in-app ...