spring集成hbase,简单的demo

1.spring集成hbase,它是由spring-data系列中的一种,可去https://spring.io/projects/spring-hadoop#overview查看他的介绍,以及简单的demo

然后下载下来,进行启动。

2.更新maven 依赖,启动他的app测试类,即可启动成功,可以测试一下,提供了查询所有和保存的方法。

3.集成到自己的spring-mybatis项目中,引入他需要依赖的jar包,主要,需要将jasper-compiler,jasper-runtime去除,否则和tomcat的冲突,无法启动,spring版本若需要修改,则在自己的项目中重新引入spring的jar包。

<!--Spring start -->
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<!--Spring end-->

<!-- spring 集成hbase版本配置 -->
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <spring.hadoop.version>2.2.0.RELEASE</spring.hadoop.version>
  <hadoop.version>2.6.0</hadoop.version>
  <hbase.version>0.98.5-hadoop2</hbase.version>
</properties>

<!-- spring 集成hbase 所需要的jar包 -->
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-hadoop</artifactId>
  <version>${spring.hadoop.version}</version>
  <exclusions>
    <exclusion>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-common</artifactId>
  <version>${hadoop.version}</version>
  <scope>compile</scope>
  <exclusions>
    <exclusion>
      <groupId>tomcat</groupId>
      <artifactId>jasper-runtime</artifactId>
    </exclusion>
    <exclusion>
      <groupId>tomcat</groupId>
      <artifactId>jasper-compiler</artifactId>
    </exclusion>
  </exclusions>

</dependency>

然后新建spring-hbase.xml,然后引入到springmvc启动配置文件applicationContext.xml中

 

 

 

4、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: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.xsd">

<!--<context:property-placeholder location="hbase.properties"/> -->

<!--<context:component-scan base-package="org.springframework.samples.hadoop.hbase"/>-->

<hdp:configuration/>

<hdp:hbase-configuration/>

<bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration"/>
</bean>

</beans>

 注意路径写对,否则会报BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

启动tomcat或者jetty,此时,启动正常,

控制层代码

service层代码

UserRepository save方法

 

 

public Boolean save(final User entity) {
  return hbaseTemplate.execute(tableName, new TableCallback<Boolean>() {
  @SuppressWarnings("deprecation")
  public Boolean doInTable(HTableInterface table) throws Throwable {
    boolean flag = false;
    try{
      byte[] rowkey = entity.getRowKey().getBytes();
      Put put = new Put(rowkey);
      //通过属性名称,分解出 列族和行建
      Field[] fields = entity.getClass().getDeclaredFields();
      //设置反射允许访问私有变量
      Field.setAccessible(fields, true);
      for(int i = 0 , len = fields.length; i < len; i++){
        Field field = fields[i];
        String varName = field.getName();
        if(!"rowKey".equals(varName)){
        String[] familyAndQual = varName.split("[_]");
          if(2==familyAndQual.length){
            if(field.get(entity) instanceof byte[]){
              //数组类型
              put.add(Bytes.toBytes(familyAndQual[0]),Bytes.toBytes(familyAndQual[1]), (byte[]) field.get(entity));
            }else{
              //string类型
              put.add(Bytes.toBytes(familyAndQual[0]),Bytes.toBytes(familyAndQual[1]), Bytes.toBytes(field.get(entity).toString()) );
            }

          }
        }
      }
      table.put(put);
      flag = true;
    }catch(Exception e){
      e.printStackTrace();
    }
    return flag;
    }
  });
}

User.java为需要保存到hbase的对象,其中列族为u

 

然后测试工具用fidder或者postman测试,类型content-type为application/json

User-Agent: Fiddler
Content-Type: application/json
Accept: application/json

 

 

转载于:https://www.cnblogs.com/hejj-bk/p/11304487.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值