spring hadoop 访问hbase入门
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入门的更多相关文章
- HBase之四--(2):spring hadoop 访问hbase
1. 环境准备: Maven Eclipse Java Spring 2. Maven pom.xml配置 <dependency> <groupId>org.apache ...
- Spring Boot(一):入门篇+前端访问后端
转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...
- JAVA API访问Hbase org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=32
Java使用API访问Hbase报错: 我的hbase主节点是spark1 java代码访问hbase的时候写的是ip 结果运行程序报错 不能够识别主机名 修改主机名 修改主机hosts文 ...
- Hbase入门教程--单节点伪分布式模式的安装与使用
Hbase入门简介 HBase是一个分布式的.面向列的开源数据库,该技术来源于 FayChang 所撰写的Google论文"Bigtable:一个结构化数据的分布式存储系统".就像 ...
- 基于Hadoop技术实现的离线电商分析平台(Flume、Hadoop、Hbase、SpringMVC、highcharts)
离线数据分析平台是一种利用hadoop集群开发工具的一种方式,主要作用是帮助公司对网站的应用有一个比较好的了解.尤其是在电商.旅游.银行.证券.游戏等领域有非常广泛,因为这些领域对数据和用户的特性把握 ...
- HBase入门基础教程之单机模式与伪分布式模式安装(转)
原文链接:HBase入门基础教程 在本篇文章中,我们将介绍Hbase的单机模式安装与伪分布式的安装方式,以及通过浏览器查看Hbase的用户界面.搭建HBase伪分布式环境的前提是我们已经搭建好了Had ...
- HBase入门篇
目录: 1-HBase的安装 2-Java操作HBase例子 3-HBase简单的优化技巧 4–存储 5(集群) -压力分载与失效转发 6 -白话MySQL(RDBMS)与HBase之间 7 -安全& ...
- 1.1-1.3 HBase入门
一.HBASE入门 部分参考链接:https://www.cnblogs.com/steven-note/p/7209398.html 1.简介 HBase – Hadoop Database,是一个 ...
- spring boot(一):入门篇
构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
随机推荐
- 重新想象 Windows 8 Store Apps (59) - 锁屏
[源码下载] 重新想象 Windows 8 Store Apps (59) - 锁屏 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 锁屏 登录锁屏,获取当前程序的锁 ...
- FL2440驱动添加(1):hello world 驱动模块添加
试试第一个hello world模块添加: 1,在添加drivers/char/hello.c /*************************************************** ...
- [Redis] redis-cli 命令总结
Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如使用Redis 的Java语言包,这些命令都有对应的方法.下面将R ...
- (六)play之yabe项目【验证码】
(六)play之yabe项目[验证码] 博客分类: 框架@play framework 添加验证码功能 在Application.java中添加一个action:captcha() /** * 添 ...
- JavaScript中的null与nudefined
null和undefined 概述 null与undefined都可以表示"没有",含义非常相似.将一个变量赋值为undefined或null,老实说,语法效果几乎没区别. var ...
- JavaScript的作用域和闭包
首发于:https://mingjiezhang.github.io/ 闭包和作用域有着千丝万缕的联系. js的作用域 具体的作用域我就不展开叙述了.其中很重要的两点就是:js的作用域链机制和函数词法 ...
- SQL如何取得一个面的中心点
) .sdo_point.x x, sdo_geom.sdo_centroid(t.shape, ) .sdo_point.y y from gd_zy_region t SQL如何取得一个面的中心点 ...
- 利用UIScrollView实现几个页面的切换
此实例可以了解一下UIScrollView的运用,以及表格跟页面跳转的内容: 原作者地址:http://www.cocoachina.com/bbs/read.php?tid=323514 效果图如下 ...
- File存储 - 文件存储
博客地址 http://www.cnblogs.com/mmyblogs/p/6107472.html(转载请保留) 文件存储 文件存储是 Android 中最基本的一种数据存储方式,它不对存储的内容 ...
- MAC中安卓开发环境的下载(转)
今天终于为我的Macbook Pro Retina搭建好了Android开发环境,几经折磨,差点放弃了: 总结如下:1.最好选择ADT Bundle,这里面已经集成好了Eclipse.ADT.Andr ...