1、  环境准备:

Maven

Eclipse

Java

Spring 版本 3..2.9

2、 Maven  pom.xml配置

<!-- Spring hadoop  -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.96.1.1-hadoop2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>

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

  <?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
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使用代码示例:

Tile t = hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper<Tile>() {

			@Override
public Tile mapRow(Result result, int rowNum) throws Exception {
// TODO Auto-generated method stub Tile t = new Tile();
t.setData(result.getValue("T".getBytes(), "key".getBytes()));
return t;
}
});

  

Hbasetemplate 常用方法简介:

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

Tile t = hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper<Tile>() {

			@Override
public Tile mapRow(Result result, int rowNum) throws Exception {
// TODO Auto-generated method stub Tile t = new Tile();
t.setData(result.getValue("T".getBytes(), "key".getBytes()));
return t;
}
});

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

return hbaseTemplate.execute(dataIdentifier, new TableCallback<Boolean>() {

			@Override
public Boolean doInTable(HTableInterface table) throws Throwable {
// TODO Auto-generated method stub
boolean flag = false;
try{
Delete delete = new Delete(key.getBytes());
table.delete(delete);
flag = true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
});

  

备注: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. 如何安装nodejs

    1.进入官网https://nodejs.org/en/download/ 2.安装过程基本直接“NEXT”就可以了.(windows的安装msi文件在过程中会直接添加path的系统变量,变量值是你的 ...

  2. FL2440驱动添加(3)LCD驱动添加学习笔记

    FL2440 LCD内置控制器,320*240 TFT型LCD. 自我理解总结的两种添加驱动模式: 非platform方式添加驱动: 加载驱动: 1,硬件初始化,申请内存,并作地址映射 2,分配设备号 ...

  3. fibonacci数列的和取余(1)

    As we know , the Fibonacci numbers are defined as follows:  """" Given two numbe ...

  4. oracle client ORA-12541: TNS: 无监听程序

    1. Question description: if you are setting the oracle client to add a local network service,  you m ...

  5. 解决C#导出excel异常来自 HRESULT:0x800A03EC的方法 .

    解决C#导出excel异常来自 HRESULT:0x800A03EC的方法 .   xlBook.SaveAs(FilePath,Microsoft.Office.Interop.Excel.XlFi ...

  6. andriod 读取网络图片

    来自:http://blog.csdn.net/jianghuiquan/article/details/8641283 Android手机上,我们常用ImageView显示图片,我们本章获取网络图片 ...

  7. JAVA基础学习day18--常用工具类

    一.System 1.1.概述 System 类包含一些有用的类字段和方法.它不能被实例化. 在 System 类提供的设施中,有标准输入.标准输出和错误输出流:对外部定义的属性和环境变量的访问:加载 ...

  8. IOS 应用跳转 (IOS9白名单)

    跳转到指定app的实现 IOS中应用的跳转是通过URL实现的,因此在实现应用跳转之前我们要设置一下对应的URL. 图一(寻找配置软件的URL) 图二(具体配置选项) 注意: 如果IOS版本为IOS9 ...

  9. ARC-数据类型需要释放的情况

    // Foundation :  OC// Core Foundation : C语言// Foundation和Core Foundation框架的数据类型可以互相转换的 //NSString *s ...

  10. Android源码分析之Looper

    先来说说summary,Looper就是用来在某个线程中跑一个message loop.一个线程默认是没有message loop与之相关联的, 为了创建一个你必须在这个线程中调用Looper.pre ...