1、  环境准备:

Maven

Eclipse

Java

Spring 版本 3..2.9

2、 Maven  pom.xml配置

  1. <!-- Spring hadoop -->
  2. <dependency>
  3. <groupId>org.apache.hbase</groupId>
  4. <artifactId>hbase-client</artifactId>
  5. <version>0.96.1.1-hadoop2</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.data</groupId>
  9. <artifactId>spring-data-jpa</artifactId>
  10. <version>1.6.0.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.data</groupId>
  14. <artifactId>spring-data-hadoop</artifactId>
  15. <version>2.0.2.RELEASE</version>
  16. </dependency>

3、 Spring和hadoop、hbase相关配置文件

  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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  7. xmlns:hdp="http://www.springframework.org/schema/hadoop"
  8. xmlns:cache="http://www.springframework.org/schema/cache" xmlns:c="http://www.springframework.org/schema/c"
  9. xmlns:p="http://www.springframework.org/schema/p"
  10. xsi:schemaLocation="
  11. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
  12. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  13. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
  14. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  15. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  16. http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
  17. http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
  18. http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
  19. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  其中标红的是spring  hadoop xml命名空间配置。

Hadoop hbase相关配置文件如下:

<!--  默认properties-->

<hdp:configuration>fs.default.name=hdfs://192.98.8.224:8010</hdp:configuration>

<hdp:hbase-configuration delete-connection="${delete-connection}" zk-quorum="${hbase.zookeeper.quorum}" zk-          port="${hbase.zookeeper.property.clientPort}"/>

对应的properties如下:

hbase.zookeeper.property.clientPort=2181

hbase.zookeeper.quorum=192.98.8.224

hbase.master=192.98.8.224:600000

fs.default.name=hdfs://192.98.8.224:8010

delete-connection=true

#hive jdbc url

hive.url=jdbc:hive://192.98.8.224:10000/default

spring hbasetemplate配置如下:

<bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate"> <property name="configuration" ref="hbaseConfiguration" />

</bean>

Hbasetemplate使用代码示例:

  1. Tile t = hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper<Tile>() {
  2.  
  3. @Override
  4. public Tile mapRow(Result result, int rowNum) throws Exception {
  5. // TODO Auto-generated method stub
  6.  
  7. Tile t = new Tile();
  8. t.setData(result.getValue("T".getBytes(), "key".getBytes()));
  9. return t;
  10. }
  11. });

  

Hbasetemplate 常用方法简介:

hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper  常用于查询,使用示例如下所示:

  1. Tile t = hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper<Tile>() {
  2.  
  3. @Override
  4. public Tile mapRow(Result result, int rowNum) throws Exception {
  5. // TODO Auto-generated method stub
  6.  
  7. Tile t = new Tile();
  8. t.setData(result.getValue("T".getBytes(), "key".getBytes()));
  9. return t;
  10. }
  11. });

  hbaseTemplate.execute(dataIdentifier, new TableCallback 常用于更新操作,使用示例如下所示:

  1. return hbaseTemplate.execute(dataIdentifier, new TableCallback<Boolean>() {
  2.  
  3. @Override
  4. public Boolean doInTable(HTableInterface table) throws Throwable {
  5. // TODO Auto-generated method stub
  6. boolean flag = false;
  7. try{
  8. Delete delete = new Delete(key.getBytes());
  9. table.delete(delete);
  10. flag = true;
  11. }catch(Exception e){
  12. e.printStackTrace();
  13. }
  14. return flag;
  15. }
  16. });

  

备注:spring hbasetemplate针对hbase接口做了强大的封装,普通功能可以使用它强大的接口,同时复杂的功能,还可以使用hbase原生的接口,如:HTableInterface、Result等。其类方法如下图:

同时hbasetemplate封装了hbase连接池等,它的创建和释放通过配置来自动管理。

文章转载请注明出处:http://www.cnblogs.com/likehua/p/4016257.html

spring hadoop 访问hbase入门的更多相关文章

  1. HBase之四--(2):spring hadoop 访问hbase

    1.  环境准备: Maven Eclipse Java Spring 2. Maven  pom.xml配置 <dependency> <groupId>org.apache ...

  2. Spring Boot(一):入门篇+前端访问后端

    转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...

  3. JAVA API访问Hbase org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=32

    Java使用API访问Hbase报错: 我的hbase主节点是spark1   java代码访问hbase的时候写的是ip 结果运行程序报错 不能够识别主机名 修改主机名     修改主机hosts文 ...

  4. Hbase入门教程--单节点伪分布式模式的安装与使用

    Hbase入门简介 HBase是一个分布式的.面向列的开源数据库,该技术来源于 FayChang 所撰写的Google论文"Bigtable:一个结构化数据的分布式存储系统".就像 ...

  5. 基于Hadoop技术实现的离线电商分析平台(Flume、Hadoop、Hbase、SpringMVC、highcharts)

    离线数据分析平台是一种利用hadoop集群开发工具的一种方式,主要作用是帮助公司对网站的应用有一个比较好的了解.尤其是在电商.旅游.银行.证券.游戏等领域有非常广泛,因为这些领域对数据和用户的特性把握 ...

  6. HBase入门基础教程之单机模式与伪分布式模式安装(转)

    原文链接:HBase入门基础教程 在本篇文章中,我们将介绍Hbase的单机模式安装与伪分布式的安装方式,以及通过浏览器查看Hbase的用户界面.搭建HBase伪分布式环境的前提是我们已经搭建好了Had ...

  7. HBase入门篇

    目录: 1-HBase的安装 2-Java操作HBase例子 3-HBase简单的优化技巧 4–存储 5(集群) -压力分载与失效转发 6 -白话MySQL(RDBMS)与HBase之间 7 -安全& ...

  8. 1.1-1.3 HBase入门

    一.HBASE入门 部分参考链接:https://www.cnblogs.com/steven-note/p/7209398.html 1.简介 HBase – Hadoop Database,是一个 ...

  9. spring boot(一):入门篇

    构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

随机推荐

  1. Asp.Net中动态页面转静态页面

    关于在Asp.Net中动态页面转静态页面的方法网上比较多.结合实际的需求,我在网上找了一些源代码,并作修改.现在把修改后的代码以及说明写一下. 一个是一个页面转换的类,该类通过静态函数Changfil ...

  2. sql 执行时间

    SET STATISTICS PROFILE ON SET STATISTICS IO ON SET STATISTICS TIME ON --GO /*--你的SQL脚本开始*/SELECT * F ...

  3. isEmpty与null、""的区别

    前一段时间我阅读别人的代码,发现有的时候用isEmpty,有的时候用null,有的时候用"".我很困惑三者之间的区别,于是我就自己写了一个程序来验证一下 public class ...

  4. SFTP和FTS协议的区别

    都是为FTP连接加密,协议非常相似.一个是借助SSL协议加密,一个时借助SSH协议加密.SSL是为HTTP/SMTP等加密设计的:SSH是为TELNET/FTP等加密.建立传输通道而设计的.其实SSH ...

  5. Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法

    Eclipse 出现Some sites could not be found.  See the error log for more detail.错误 解决方法 Some sites could ...

  6. JSON数据解析(转)

    上篇随笔详细介绍了三种解析服务器端传过来的xml数据格式,而对于服务器端来说,返回给客户端的数据格式一般分为html.xml和json这三种格式,那么本篇随笔将讲解一下json这个知识点,包括如何通过 ...

  7. docker 使用

    https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-docker-application http ...

  8. js得到屏幕宽高、页面宽高 (window.screen.availHeight)等--笔记

    window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间) window.screen.width ...

  9. React对话框组件实现

    当下前端届最火的技术之一莫过于React + Redux + webpack的技术结合.最近公司内部也正在转react,这周主要做了个React的modal组件,接下来谈下具体实现过程. 基本的HTM ...

  10. Atitit. Xss 漏洞的原理and应用xss木马

    Atitit. Xss 漏洞的原理and应用xss木马 1. XSS漏洞1 2. XSS的用途2 2.1. 盗取cookie2 2.2. 刷新流量 刷分3 2.3. DOS 窃取隐私”.“假冒身份”. ...