HbaseTemplate配置进阶:利用Spring自动装配加载HbaseTemplate

来源:互联网 发布:jersey 返回对象json 编辑:程序博客网 时间:2024/05/29 13:39

1、spring-hbase.xml配置文件:

<?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:hdp="http://www.springframework.org/schema/hadoop"  
   xmlns:beans="http://www.springframework.org/schema/beans"  
   xmlns:context="http://www.springframework.org/schema/context"
   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-3.0.xsd
    http://www.springframework.org/schema/hadoop 
    http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
    <!-- 加载外部的properties配置文件  -->
    <context:property-placeholder location="file:/opt/search/resourse/hbase.properties"/>
    <!-- 配置zookeeper的信息,远程连接hbase时使用 -->  
    <hdp:configuration resources="classpath:/hbase-site.xml" />   
    <hdp:hbase-configuration zk-quorum="${quorum}"  
zk-port="${clientPort}" configuration-ref="hadoopConfiguration" /> 
    <!-- 配置HbaseTemplate   -->
    <bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">  
        <property name="configuration" ref="hbaseConfiguration">  
        </property>  
        <property name="encoding" value="UTF-8"></property>  
    </bean>   
 </beans>  

2、hbase-site.xml配置文件

<?xml version="1.0"?>  
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>  
<configuration> 
    <!-- zookeeper集群的URL配置 
    <property>  
        <name>hbase.zookeeper.quorum</name>  
        <value>namenode1-sit.cnsuning.com,namenode2-sit.cnsuning.com,slave01-sit.cnsuning.com</value>  
    </property>  -->
    <!-- 客户端连接端口 
    <property>  
        <name>hbase.zookeeper.property.clientPort</name>
        <value>2015</value>  
    </property> -->
    <!-- 重试等待时间 -->
    <property>  
        <name>hbase.client.pause</name>  
        <value>50</value>  
    </property>
    <!-- 失败时重试次数 -->
    <property>  
        <name>hbase.client.retries.number</name>  
        <value>3</value>  
    </property>
    <!-- RPC请求的超时时间。如果某次RPC时间超过该值,客户端就会主动关闭socket -->
    <property>  
        <name>hbase.rpc.timeout</name>  
        <value>2000</value>  
    </property>
    <!-- HBase客户端发起一次数据操作直至得到响应之间总的超时时间 -->
    <property>  
        <name>hbase.client.operation.timeout</name>  
        <value>3000</value>  
    </property>
    <!-- 该参数是表示HBase客户端发起一次scan操作的rpc调用至得到响应之间总的超时时间 -->
    <property>  
        <name>hbase.client.scanner.timeout.period</name>  
        <value>10000</value>  
    </property>
</configuration>  


3、hbase.properties属性配置文件

quorum=10.16.35.232
clientPort=2015


4、applicationContext.xml配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:hdp="http://www.springframework.org/schema/hadoop"  
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

<context:component-scan base-package="com.suning" />
<context:component-scan base-package="com.suning.app" />
<context:component-scan base-package="com.suning.commonlib" />
<context:component-scan base-package="com.suning.snRedis" />

      <import resource="classpath:/spring-hbase.xml" /> <!--关键这里,直接引用外部的xml文件-->
</beans>


4、java中调用只需

@Autowired
    HbaseTemplate htemplate;

阅读全文
0 0
原创粉丝点击