分享

Spring hadoop 访问hbase 入门教程

pig2 发表于 2014-10-12 23:09:16 [显示全部楼层] 只看大图 回帖奖励 阅读模式 关闭右栏 6 72451
本帖最后由 pig2 于 2014-10-12 23:48 编辑
问题导读:
1.你认为该如何访问hbase?
2.Hadoop hbase需要做哪些配置?
3.pom文件需要哪些修改?






环境准备:
Maven
Eclipse
Java
Spring 版本 3..2.9
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>
  17.                     <!-- Spring hadoop  -->
复制代码




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">
复制代码





其中
xmlns:hdp="http://www.springframework.org/schema/hadoop"

http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd  


是spring  hadoop配置  

Hadoop hbase相关配置文件如下:
  1.    <!--  默认properties-->
  2.         <hdp:configuration>fs.default.name=hdfs://192.98.8.224:8010</hdp:configuration>
  3.         <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配置如下:
  1.         <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
  2.        <property name="configuration" ref="hbaseConfiguration" />
  3. </bean>
复制代码




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




Hbasetemplate 常用方法简介:
  1. hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper  常用于查询,使用示例如下所示:
  2. Tile t = hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper<Tile>() {
  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.                         @Override
  3.                         public Boolean doInTable(HTableInterface table) throws Throwable {
  4.                                 // TODO Auto-generated method stub
  5.                                 boolean flag = false;
  6.                                 try{
  7.                                 Delete delete = new Delete(key.getBytes());
  8.                                 table.delete(delete);
  9.                                 flag = true;
  10.                                 }catch(Exception e){
  11.                                         e.printStackTrace();
  12.                                 }
  13.                                 return flag;
  14.                         }
  15.                 });
复制代码




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

1.png

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

来源李克华:




已有(6)人评论

跳转到指定楼层
howtodown 发表于 2014-11-15 12:57:49
补充一些内容:
spring+hbase集成

1.    引入maven依赖
<dependency>
         <groupId>org.springframework.data</groupId>
         <artifactId>spring-data-hadoop</artifactId>
         <version>1.0.1.RELEASE</version>
</dependency>
<dependency>
         <groupId>org.apache.hbase</groupId>
         <artifactId>hbase</artifactId>
         <version>0.94.12</version>
</dependency>

2.    Spring-data头部命名空间和标签
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/hadoophttp://www.springframework.org/schema/hadoop/spring-hadoop.xsd">


3.    配置hbase基础信息
//配置hadoop的基本信息
<hdp:configuration>
fs.default.name=hdfs://172.20.59.47:9000    </hdp:configuration>
//配置zookeeper的信息,远程连接hbase时使用
<hdp:hbase-configuration  zk-quorum="xxx.xxx.xxx.xxx"zk-port="2181"/>
   <beanid="htemplate"class="org.springframework.data.hadoop.hbase.HbaseTemplate" >
       <property name="configuration"ref="hbaseConfiguration">
</property>
</bean>
hbaseConfiguration其实就是指的<hdp:hbase-configuration/>配置的信息


4.     实例演示
public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext(newString[] { "spring-beans-hbase.xml" });
       BeanFactory factory = (BeanFactory) context;
       HbaseTemplate htemplate = (HbaseTemplate) factory.getBean("htemplate");
       String custom = "custom";
       htemplate.get("wcm", "10461", newRowMapper<String>(){
           @Override
           public String mapRow(Result result, int rowNum) throws Exception {
                // TODO Auto-generated methodstub
                for(KeyValue kv :result.raw()){
                    String key = newString(kv.getQualifier());
                    String value = newString(kv.getValue());
                    System.out.println(key +"= "+Bytes.toString(value.getBytes()));
                }
                return null;
           }
       });
}
查看数据 get “wcm“, ”rowkey“ 得到一条数据



5.    基本命令
查看有哪些表list
查看所有数据 scan “表名”
查看数据 get “wcm“,”lrowkey“ 得到一条数据
删除一条数据delete “表名”,”主键”,”列族”,”列”
删除整条数据deleteAll “表名”,”主键”
Hbase的特点:
>>在命令窗口只能一次更新一个单元格;
>>在程序中通过调用HTable.setAutoFlush(false)方法可以将HTable写客户端的自动flush关闭,这样可以批量写入数据到 HBase,而不是有一条put就执行一次更新,只有当put填满客户端写缓存时,才实际向HBase服务端发起写请求。默认情况下auto flush是开启的。


回复

使用道具 举报

xuezhiji 发表于 2016-5-4 16:58:33
不错,学习了
回复

使用道具 举报

wangjie142 发表于 2016-5-5 17:42:37
正在学习中,想做一个网盘
回复

使用道具 举报

wangjie142 发表于 2016-5-5 17:43:13
对了,你们都是看的官方的 api还是书籍啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条