Spring整合HBase

Spring
HBase
SHDP

§ 系统环境

  • 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作为存储系统。

首先需要安装必须的软件:

  1. $ apt-get install ssh 

  2. $ apt-get install rsync 

并确保ssh可以不使用口令就能够连接localhost。设置方式如下:

  1. $ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa 

  2. $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys 

  3. $ chmod 0600 ~/.ssh/authorized_keys 

查看Ubuntu的IP地址,然后编辑/etc/hosts,注释掉其他主机映射内容,将<ip><domain>替换为本机IP地址和自定义域:

  1. #127.0.0.1 localhost 

  2. #127.0.1.1 <domain> 

  3. <ip> <domain> localhost 

§ 配置Hadoop

<HADOOP_DIR>表示Hadoop根目录,编辑<HADOOP_DIR>/etc/hadoop/hadoop-env.sh,修改JAVA_HOME为JDK安装目录。

编辑配置文件<HADOOP_DIR>/etc/hadoop/core-site.xml

  1. <configuration> 

  2. <property> 

  3. <name>fs.defaultFS</name> 

  4. <value>hdfs://<ip>:9000</value> 

  5. </property> 

  6. <property> 

  7. <name>hadoop.tmp.dir</name> 

  8. <value>/home/<user>/data</value> 

  9. </property> 

  10. </configuration> 

编辑配置文件<HADOOP_DIR>/etc/hadoop/hdfs-site.xml

  1. <configuration> 

  2. <property> 

  3. <name>dfs.replication</name> 

  4. <value>1</value> 

  5. </property> 

  6. </configuration> 

§ 配置HBase

<HBASE_DIR>表示HBase根目录,修改<HBASE_DIR>/conf/hbase-env.sh,添加JAVA_HOME

编辑配置文件<HBASE_DIR>/conf/hbase-site.xml,替换ip和domain为对应内容:

  1. <configuration> 

  2. <property> 

  3. <name>hbase.cluster.distributed</name> 

  4. <value>true</value> 

  5. </property> 

  6. <property> 

  7. <name>hbase.rootdir</name> 

  8. <value>hdfs://<ip>:9000/hbase</value> 

  9. </property> 

  10. <property> 

  11. <name>hbase.zookeeper.quorum</name> 

  12. <value><domain></value> 

  13. </property> 

  14. <property> 

  15. <name>hbase.zookeeper.property.clientPort</name> 

  16. <value>2181</value> 

  17. </property> 

  18. </configuration> 

§ 启动Hadoop和HBase

启动Hadoop前先格式化文件系统:

  1. $ ./<HADOOP_DIR>/bin/hdfs namenode -format 

然后启动Hadoop和HBase:

  1. $ ./<HADOOP_DIR>/sbin/start-dfs.sh 

  2. $ ./<HBASE_DIR>/bin/start-hbase.sh 

可以使用jps命令查看所有启动的进程:

  1. $ jps 

  2. 2786 NameNode 

  3. 2914 DataNode 

  4. 6259 HQuorumPeer 

  5. 6324 HMaster 

  6. 3083 SecondaryNameNode 

  7. 6411 HRegionServer 

§ 创建Maven项目

新建Maven项目,所需依赖如下:

  1. <properties> 

  2. <slf4j.version>1.7.21</slf4j.version> 

  3. <spring.version>4.3.2.RELEASE</spring.version> 

  4. </properties> 


  5. <dependencies> 

  6. <dependency> 

  7. <groupId>junit</groupId> 

  8. <artifactId>junit</artifactId> 

  9. <scope>test</scope> 

  10. </dependency> 

  11. <dependency> 

  12. <groupId>org.springframework</groupId> 

  13. <artifactId>spring-core</artifactId> 

  14. </dependency> 

  15. <dependency> 

  16. <groupId>org.springframework</groupId> 

  17. <artifactId>spring-context</artifactId> 

  18. </dependency> 

  19. <dependency> 

  20. <groupId>org.springframework</groupId> 

  21. <artifactId>spring-tx</artifactId> 

  22. </dependency> 

  23. <dependency> 

  24. <groupId>org.springframework.data</groupId> 

  25. <artifactId>spring-data-hadoop</artifactId> 

  26. <exclusions> 

  27. <exclusion> 

  28. <groupId>org.springframework</groupId> 

  29. <artifactId>spring-context-support</artifactId> 

  30. </exclusion> 

  31. <exclusion> 

  32. <groupId>org.slf4j</groupId> 

  33. <artifactId>slf4j-log4j12</artifactId> 

  34. </exclusion> 

  35. </exclusions> 

  36. </dependency> 

  37. <dependency> 

  38. <groupId>org.springframework</groupId> 

  39. <artifactId>spring-test</artifactId> 

  40. </dependency> 

  41. <dependency> 

  42. <groupId>org.apache.hadoop</groupId> 

  43. <artifactId>hadoop-auth</artifactId> 

  44. </dependency> 

  45. <dependency> 

  46. <groupId>org.apache.hbase</groupId> 

  47. <artifactId>hbase-client</artifactId> 

  48. <version>1.2.3</version> 

  49. <scope>compile</scope> 

  50. <exclusions> 

  51. <exclusion> 

  52. <groupId>log4j</groupId> 

  53. <artifactId>log4j</artifactId> 

  54. </exclusion> 

  55. <exclusion> 

  56. <groupId>org.slf4j</groupId> 

  57. <artifactId>slf4j-log4j12</artifactId> 

  58. </exclusion> 

  59. </exclusions> 

  60. </dependency> 


  61. <dependency> 

  62. <groupId>org.slf4j</groupId> 

  63. <artifactId>jcl-over-slf4j</artifactId> 

  64. </dependency> 

  65. <dependency> 

  66. <groupId>org.slf4j</groupId> 

  67. <artifactId>slf4j-api</artifactId> 

  68. </dependency> 

  69. <dependency> 

  70. <groupId>org.slf4j</groupId> 

  71. <artifactId>slf4j-log4j12</artifactId> 

  72. </dependency> 

  73. <dependency> 

  74. <groupId>log4j</groupId> 

  75. <artifactId>log4j</artifactId> 

  76. </dependency> 

  77. </dependencies> 

将HBase的配置文件hbase-site.xml复制到resources下,新建Spring配置文件applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 

  2. <beans xmlns="http://www.springframework.org/schema/beans" 

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

  4. xmlns:context="http://www.springframework.org/schema/context" 

  5. xmlns:hdp="http://www.springframework.org/schema/hadoop" 

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 

  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 

  8. http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd"> 


  9. <context:annotation-config/> 

  10. <context:component-scan base-package="com.sample.hbase"/> 

  11. <hdp:configuration resources="hbase-site.xml"/> 

  12. <hdp:hbase-configuration configuration-ref="hadoopConfiguration"/> 

  13. <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate"> 

  14. <property name="configuration" ref="hbaseConfiguration"/> 

  15. </bean> 

  16. </beans> 

新建测试用例:

  1. @RunWith(SpringJUnit4ClassRunner.class) 

  2. @ContextConfiguration(locations = {"classpath*:applicationContext.xml"}) 

  3. public class BaseTest { 


  4. @Autowired 

  5. private HbaseTemplate template; 


  6. @Test 

  7. public void testFind() { 

  8. List<String> rows = template.find("user", "cf", "name", new RowMapper<String>() { 

  9. public String mapRow(Result result, int i) throws Exception { 

  10. return result.toString(); 



  11. }); 

  12. Assert.assertNotNull(rows); 




  13. @Test 

  14. public void testPut() { 

  15. template.put("user", "1", "cf", "name", Bytes.toBytes("Alice")); 





整合完成。

Spring整合HBase的更多相关文章

  1. NoSql存储日志数据之Spring+Logback+Hbase深度集成

    NoSql存储日志数据之Spring+Logback+Hbase深度集成 关键词:nosql, spring logback, logback hbase appender 技术框架:spring-d ...

  2. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  3. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  4. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  5. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  6. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  7. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  8. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  9. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

随机推荐

  1. MySQL字段类型详解

    MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型.本节首先给出可用类型的一个概述,并且总结每个列类型的存储需求,然后提供每个类中的类型性质的更详细的描述. ...

  2. vpsmate安装

    安装需求 操作系统:CentOS/Redhat 5.4 或 5.4 以上版本,32位或64位均可,推荐使用 CentOS 6.2 64位. 内存大小:运行时占用约 20MB 左右的服务器内存. 请使用 ...

  3. SQL的多表连接查询

    SQL的多表连接查询 多表连接查询具有两种规范,SQL92和SQL99规范. SQL92规范支持下列多表连接查询: (1)等值连接: (2)非等值连接: (3)外连接: (4)广义笛卡尔积: SQL9 ...

  4. iOS UIButton 设置图片不变型setImage

    [btn.imageView setContentMode:UIViewContentModeScaleAspectFill];

  5. AFN设置请求超时时间

    进入AFURLRequestSerialization.m 找到 - (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLSt ...

  6. [issue] [iOS 10] 升级后无法真机测试 Could not find Developer Disk Image

    说明:更新了手机的到了iOS 10.0.2.真机调试时候提示"Could not find Developer Disk Image" 并且之前就下载了Xcode8,但是没有安装X ...

  7. 微信小程序-视图条件渲染

    条件渲染 wx:if 在框架中,我们用 wx:if="{{condition}}" 来判断是否需要渲染该代码块: <view wx:if="{{condition} ...

  8. RabbitMQ 开启WEB管理

    rabbitmq-plugins 插件管理器 1.开启rabbitmq management  -  WEB管理插件 # rabbitmq-plugins enable rabbitmq_manage ...

  9. OpenCV2+入门系列(二):图像的打开、创建与显示(命令行)

    前置知识:数字图像的简略知识 这里只是最基础的知识,上课如果稍微听了课的同学可以直接略过不不看. 彩色图像: 对于一副数字图像,对于一副RGB色彩空间的彩色数字图像,它一共有宽X高个像素格子,每个格子 ...

  10. Google-GLog编译以及使用

    心血来潮要去看开源代码,看到公司的日志库是在google-glog开源库上二次封装的,那就先撸glog吧. 1. 下载源码  一条命令取源码:git clone github.com/google/g ...