spring boot 2.0.2 集成hbase

maven依赖

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.2.RELEASE</version>
</parent>

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-hadoop-boot</artifactId>
  <version>2.5.0.RELEASE</version>
  <exclusions>
     <exclusion>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
     </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-hadoop</artifactId>
   <version>2.5.0.RELEASE</version>
   <exclusions>
      <exclusion>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
       </exclusion>
    <exclusion>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       </exclusion>
     <exclusion>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        </exclusion>
     </exclusions>
</dependency>

<dependency>
   <groupId>org.apache.hbase</groupId>
   <artifactId>hbase-client</artifactId>
   <version>1.4.4</version>
   <exclusions>
      <exclusion>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
      </exclusion>
      <exclusion>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
      </exclusion>
   </exclusions>
</dependency>

configuration 配置

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.CanReadFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.security.UserGroupInformation;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.hadoop.hbase.HbaseTemplate;

import com.hginfo.ete.common.exception.EteException;
import com.hginfo.ete.framework.data.HBaseProperties;

@Configuration
@EnableConfigurationProperties(HBaseProperties.class)
public class HBaseConfig {

    private final HBaseProperties properties;

    public HBaseConfig(HBaseProperties properties) {
        this.properties = properties;
    }

    @Bean
    public HbaseTemplate hbaseTemplate() {
        HbaseTemplate hbaseTemplate = new HbaseTemplate();
        hbaseTemplate.setConfiguration(configuration());
        hbaseTemplate.setAutoFlush(true);
        return hbaseTemplate;
    }

    public org.apache.hadoop.conf.Configuration configuration() {

        org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();

        Map<String, String> config = properties.getConfig();
        Set<String> keySet = config.keySet();
        for (String key : keySet) {
            configuration.set(key, config.get(key));
        }

        return configuration;
    }
    
}

HBaseProperties

@ConfigurationProperties(prefix = "hbase")
public class HBaseProperties {

    private Map<String, String> config;
    
    public Map<String, String> getConfig() {
        return config;
    }

    public void setConfig(Map<String, String> config) {
        this.config = config;
    }
    

}

yml文件配置

hbase:
  config:
    hbase.zookeeper.quorum: 127.0.0.1,127.0.0.2,127.0.0.3
    hbase.zookeeper.property.clientPort: 2181
    fs.defaultFS: hdfs://cc-11:8020
    fs.trash.interval: 1

列出的yml配置只是最基本的,其他配置一次列入yml文件即可!

相关推荐