1、  环境准备:

Maven

Eclipse

Java

Spring

2、 Maven  pom.xml配置

<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.0</version>
<exclusions>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
<exclusions>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</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:context="http://www.springframework.org/schema/context"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop-1.0.xsd">

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

Hadoop hbase相关配置文件如下:

对应的properties如下:

spring hbasetemplate配置如下:

<?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:context="http://www.springframework.org/schema/context"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop-1.0.xsd"> <hdp:configuration>
fs.default.name=hdfs://10.202.34.200:8020
</hdp:configuration> <hdp:hbase-configuration delete-connection="true" stop-proxy="false">
<!-- hbase.rootdir=${hbase.rootdir} -->
<!-- hbase.zookeeper.quorum=${hbase.zookeeper.quorum} --> hbase.rootdir=hdfs://10.202.34.200:8020/hbase
hbase.zookeeper.quorum=10.202.34.200
hbase.zookeeper.property.clientPort=2181
hbase.zookeeper.property.dataDir=/hbase hbase.cluster.distributed=true
zookeeper.session.timeout=180000
hbase.zookeeper.property.tickTime=4000
dfs.replication=3
hbase.regionserver.handler.count=100
hbase.hregion.max.filesize=10737418240
hbase.regionserver.global.memstore.upperLimit=0.4
hbase.regionserver.global.memstore.lowerLimit=0.35
hfile.block.cache.size=0.2
hbase.hstore.blockingStoreFiles=20
hbase.hregion.memstore.block.multiplier=2
hbase.hregion.memstore.mslab.enabled=true
hbase.client.scanner.timeout.period=6000000
hbase.client.write.buffer=20971520
hbase.hregion.memstore.flush.size=268435456
hbase.client.pause=20
hbase.client.retries.number=11
hbase.client.max.perserver.tasks=50
hbase.client.max.perregion.tasks=10
</hdp:hbase-configuration> <!--
<bean id="tablePool" class="org.apache.hadoop.hbase.client.HTablePool">
<constructor-arg ref="hbaseConfiguration" />
<constructor-arg value="100" />
</bean>
--> <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration" />
</bean> </beans>

Hbasetemplate使用代码示例:

1
2
3
4
5
6
7
8
9
10
11
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  常用于查询,使用示例如下所示:

1
2
3
4
5
6
7
8
9
10
11
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 常用于更新操作,使用示例如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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连接池等,它的创建和释放通过配置来自动管理。

示例:

package com.sf.study.hbase;

import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.RowMapper; public class SpringHbaseTest { public static void main(String[] agrs) {
// 在xml配置文件中找到htemplate
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:/com/sf/study/META-INF/config/biz-hbase.xml" });
BeanFactory factory = (BeanFactory) context;
HbaseTemplate htemplate = (HbaseTemplate) factory.getBean("hbaseTemplate");
// 使用find方法查找 fvp_dev_duan为表名 ,info为列族名称及family
htemplate.find("fvp_dev_duan", "info", new RowMapper<String>() {
// result为得到的结果集
public String mapRow(Result result, int rowNum) throws Exception {
// 循环行
for (KeyValue kv : result.raw()) {
// 得到列族组成列qualifier
String key = new String(kv.getQualifier());
// 得到值
String value = new String(kv.getValue());
System.out.println(key + "= " + Bytes.toString(value.getBytes()));
}
return null;
}
});
} }

结果:

D=
592W"7958687*209��n�

HBase之四--(2):spring hadoop 访问hbase的更多相关文章

  1. spring hadoop 访问hbase入门

    1.  环境准备: Maven Eclipse Java Spring 版本 3..2.9 2. Maven  pom.xml配置 <!-- Spring hadoop --> <d ...

  2. HBase之四--(1):Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  3. 【HBase基础教程】1、HBase之单机模式与伪分布式模式安装(转)

    在这篇blog中,我们将介绍Hbase的单机模式安装与伪分布式的安装方式,以及通过浏览器查看Hbase的用户界面.搭建hbase伪分布式环境的前提是我们已经搭建好了hadoop完全分布式环境,搭建ha ...

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

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

  5. 使用ganglia监控hadoop及hbase集群

    一.Ganglia简介 Ganglia 是 UC Berkeley 发起的一个开源监视项目,设计用于测量数以千计的节点.每台计算机都运行一个收集和发送度量数据(如处理器速度.内存使用量等)的名为 gm ...

  6. Hadoop 之Hbase命令

    一.常用命令:(hbase shell 进入终端) 1.创建表: create 'users','user_id','address','info' 表users,有三个列族user_id,addre ...

  7. 使用Ganglia监控hadoop、hbase

    Ganglia是一个监控服务器,集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等指标. Ganglia的强大在于:g ...

  8. HBase(一): c#访问hbase组件开发

    HDP2.4安装系列介绍了通过ambari创建hbase集群的过程,但工作中一直采用.net的技术路线,如何去访问基于Java搞的Hbase呢? Hbase提供基于Java的本地API访问,同时扩展了 ...

  9. 使用C#通过Thrift访问HBase

    前言 因为项目需要要为客户程序提供C#.Net的HBase访问接口,而HBase并没有提供原生的.Net客户端接口,可以通过启动HBase的Thrift服务来提供多语言支持. Thrift介绍 环境 ...

随机推荐

  1. jquery 实现鼠标点击div盒子移动功能

    // Start 窗口的拖动 var _move=false; //移动标记 var _x,_y; //鼠标离控件左上角的相对位置 $(document).ready(function(){ $(&q ...

  2. LVM创建

    LVM介绍 PV(Physical Volume) - 物理卷 物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘,也可以是raid设备 VG(Volume Group ...

  3. hdu 1679 The Unique MST (克鲁斯卡尔)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24152   Accepted: 8587 D ...

  4. Solidworks如何创建投影曲线

    画好草图之后(草图是在上视基准面上画的)然后点击曲线,投影曲线   面选择要投影的曲面,然后就得到了平面曲线在曲面上的投影得到的空间曲线   注意这种方法对于开环轮廓也是可以用的,比如下面,我定义一个 ...

  5. ZOJ ACM 1314(JAVA)

    昨天做了几个题目.过于简单,就不在博客里面写了. 1314这道题也比較简单,写出来是由于我认为在这里有一个小技巧,对于时间复杂度和空间复杂度都比較节省. 这个题目类似哈希表的求解.可是更简单.刚拿到题 ...

  6. 【转载】C# 理解泛型

    术语表 generics:泛型type-safe:类型安全collection: 集合compiler:编译器run time:程序运行时object: 对象.NET library:.Net类库va ...

  7. UWP 新手教程2——怎样实现自适应用户界面

    系列文章 UWP新手教程1--UWP的前世今生 如上文所说的,布局面板依据可用的屏幕空间.指定界面元素的大小和位置. 比如StackPanel 会水平或垂直排列界面元素.Grid 布局与CSS 中的表 ...

  8. EF中 Code-First 方式的数据库迁移

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...

  9. EF(Entity Framework)通用DBHelper通用类,增删改查以及列表

    其中 通用类名:DBhelper 实体类:UserInfo 1 //新增 2 DBHelper<UserInfo> dbhelper = new DBHelper<UserInfo& ...

  10. 回溯法——求解N皇后问题

    问题描写叙述 八皇后问题是十九世纪著名数学家高斯于1850年提出的.问题是:在8*8的棋盘上摆放8个皇后.使其不能互相攻击,即随意的两个皇后不能处在允许行.同一列,或允许斜线上. 能够把八皇后问题拓展 ...