spring+hbase集成

来源:互联网 发布:o 数据库管理软件 编辑:程序博客网 时间:2024/06/05 00:21

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是开启的。

 

参考文档:

http://docs.spring.io/springhadoop/docs/1.0.1.RELEASE/reference/html/hbase.html

http://abloz.com/hbase/book.html

1 0
原创粉丝点击