關於spring集成hbase

關於spring繼承hbase

1.      spring繼承hbase首先下載spring-data-hadoop jar包官方版本已更新至2.1.0,此處引用1.0.1版本作爲繼承對象(官方未找到下載端口download點擊無反應)。

2.      項目中引入spring-data-hadoop jar包。

3.      新建一個xml文件命名爲applicationContext_hbase.xml在applicationContext.xml中引入此xml文件。

4.      配置xml文件 配置內容如下所示:
<?xml version="1.0" encoding="UTF-8"?>

<beansxmlns="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">

<!-- 配置hadoop的基本信息 -->

<hdp:configuration>

              fs.default.name=hdfs://192.168.1.230:9000

</hdp:configuration>

<!-- 配置zookeeper地址和端口 -->

       <hdp:hbase-configurationzk-quorum="192.168.1.230"

              zk-port="2181"/>

<!-- 配置HbaseTemplate -->

       <beanid="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">

              <propertyname="configuration" ref="hbaseConfiguration">

              </property>

       </bean>

</beans>
以上就是xml文件配置內容:

5.寫方法測試
       public static void main(String[]agrs) {

              //在xml配置文件中找到htemplate

              ApplicationContextcontext = new ClassPathXmlApplicationContext(

                            newString[] { "applicationContext.xml" });

              BeanFactoryfactory = (BeanFactory) context;

              HbaseTemplatehtemplate = (HbaseTemplate) factory.getBean("htemplate");

              //使用find方法查找  test2爲表名 ,zb爲列族名稱及family

              htemplate.find("test2","zb", new RowMapper<String>() {

                     //result爲得到的結果集

                     publicString mapRow(Result result, int rowNum) throws Exception {

                            //循環行

                            for(KeyValue kv : result.raw()) {

                                   //得到列族組成列qualifier

                                   Stringkey = new String(kv.getQualifier());

                                   //得到值

                                   Stringvalue = new String(kv.getValue());

                                   System.out.println(key+ "= "

                                                 +Bytes.toString(value.getBytes()));

                            }

                            returnnull;

                     }

              });

       }

 

得到結果:

jd= 13

wd= 13

jd= 73

wd= 73

jd= 80

wd= 80

jd= 54

wd= 34

jd= 42

wd= 42

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章