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]throw exception

    CREATE PROCEDURE pro_throwException (errorCode char(5), errorMessage text) BEGIN SIGNAL SQLSTATE err ...

  2. Flags-Ural1225简单递推

    Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...

  3. iOS开发,URL编码和解码

    URL传递数据中,如果含有中文,需要进行编码: NSString *urlEncodeString = [urlStr stringByAddingPercentEncodingWithAllowed ...

  4. 第一个python实例--监控cpu

    #第一个python实例:监控cpu #/bin/bash/env Python from __future__ import print_function from collections impo ...

  5. tsne官方论文代码解读和使用

    MLGB,人生就是矫情,充满冲动,充满热恋. tsne的08年的论文看了几遍,发现原理还是蛮简单的,能想到还是不容易(人生的战场是星辰大海,但我们的贡献就是也就是宇宙尘埃) 怎么说呢,现在真的是一个好 ...

  6. tomcat部署web项目的3中方法

    1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法. 2.在tomcat安装目录中有一个conf文件夹,打开此文件夹,其 ...

  7. iOS_XCode7_Launch Image 的初使用

    之前一直没有做笔记的习惯,后来发现很多知识不常用,容易忘了,再去网上找的,很浪费时间,现在尝试着自己写写博客,可以是自己笔记,方便以后查询,如果有纰漏,欢迎指正. 1.Xcode7中 ,assets. ...

  8. kali Linux添加add-apt-repository

    Debian让用户可以通过一个名为add-apt-repository的应用程序,添加和使用PPA软件库,不过Kali Linux在其默认的程序包列表中并不含有该应用程序.就Kali而言,由于这是个特 ...

  9. SQL知识整理三:变量、全局变量、视图、事务、异常

           变量 1.局部变量的声明(一个@) declare @n int   --声明变量关键字为declare 然后@加变量名 后面是变量类型 declare @s varchar(36) 2 ...

  10. [Amazon] Amazon IAP for Unity

    1> 下载amazon IAP3.0 for unity plugin 2> 根据 https://developer.amazon.com/public/apis/earn/in-app ...