springBoot集成Hbase(spring-data-hadoop-boot版)


最近開始一步一步走進大數據,當然是作爲程序員。今天就來說說Hbase的集成,首先今天先說下通過spring-data-hadoop-boot的集成吧。老規矩,廢話不多說,加班快下班了,就抽20分鐘分享。
一、maven引包

<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.5.0</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>

裏面會排除log、servlet-api,原因是與springboot裏衝突了。
二、配置文件

hbase:
  config:
    hbase.zookeeper.quorum: 192.168.200.71,192.168.200.73
    hbase.zookeeper.property.clientPort: 2181
zookeeper:
  znode:
    parent: /hbase

三、配置類


import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.beans.factory.annotation.Value;
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 java.util.Map;
import java.util.Set;

/**
 * @author zhengwen
 **/
@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;
    }

}

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Map;

/**
 * @author zhengwen
 **/
@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;
    }

}

注意引入lombok。
四、使用

mport org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.stereotype.Service;

import org.apache.hadoop.conf.Configuration;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author zhengwen
 **/
@Service
public class HBaseServiceImpl implements HBaseService {

    @Autowired
    private HbaseTemplate hbaseTemplate;


    @Override
    public Result<?> createTable(HbaseTable hbaseTable) throws Exception {

        //從hbase模板獲取配置
        Configuration conf = hbaseTemplate.getConfiguration();

        //連接
        Connection connection = null;
        Table table = null;
        try {
            //獲取連接
            connection = ConnectionFactory.createConnection(conf);

            //從連接獲取一個客戶端
            Admin admin = connection.getAdmin();

            //組織建表
            String tableName = hbaseTable.getTableName();
            List<Map<String, String>> columnMpList = hbaseTable.getColumnMapList();
            if (!admin.tableExists(TableName.valueOf(tableName))) {

                HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

                for (Map<String, String> mp:columnMpList){
                    Set<String> keySet = mp.keySet();
                    Iterator<String> it = keySet.iterator();
                    while (it.hasNext()){
                        String key = it.next();
                        if ("colEn".equals(key)){
                            String colEn = mp.get(key);
                            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(Bytes.toBytes(colEn));
                            tableDescriptor.addFamily(hColumnDescriptor);
                        }
                    }
                }

                //客戶端創建表
                admin.createTable(tableDescriptor);
            } else {
                return ResultGenerator.genFailResult(tableName+"表已經存在");
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult(e.getCause().getMessage());
        }


        return ResultGenerator.genSuccessResult();
    }
}

Result是自定義的一個參數統一返回對象,裏面就是message、code、data。
ResultGenerator是封裝的生成Result基本信息的工具類。
HbaseTable是我定義的一個建表的入參對象,裏面是表名、List<Map<String, String>>接收family的。
hbase的建表與其他關係型數據庫不太一樣,建表時不需要建列。基本上集成就完成了,這裏說下這樣集成的弊端吧。
首先spring-data-hadoop-boot在2019-04-09已經停止維護了,原因沒有說。停更時支持的最高版本:hadoop:2.7.3,hbase:2.1。具體可以去spring官網看,有博友說使用到2.7.5 + 2.1,我沒有親測。因爲我們後期時微服務,我測試springBoot與SpringCloud的版本最高就是我pom裏的版本,再高就報錯了額,想改來着,看到要改的地方太多,不是一人之力可以做的,放棄了。如果是自己玩大數據平臺,圖方便就可以用這套版本。
我們這次是從kafak消費,存Hbase庫。詳細邏輯我也不便說太多,見諒。下次分享spring高版本對接高版本Hbase。

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