zoukankan      html  css  js  c++  java
  • HBase之四--(2):spring hadoop 访问hbase

    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= 
    10000092161011592W"7958687*209��n�
  • 相关阅读:
    jquery 实现 html5 placeholder 兼容password密码框
    php返回json的结果
    使用PHP读取远程文件
    Sharepoint 自定义字段
    Sharepoint 中新增 aspx页面,并在页面中新增web part
    【转】Sharepoint 2010 配置我的站点及BLOG
    JS 实现 Div 向上浮动
    UserProfile同步配置
    【转】Import User Profile Photos from Active Directory into SharePoint 2010
    Sharepoint 2010 SP1升级后 FIMSynchronizationService 服务无法开启
  • 原文地址:https://www.cnblogs.com/duanxz/p/4512864.html
Copyright © 2011-2022 走看看