springboot整合Hbase


TOC

springboot整合Hbase

springboot項目需要整合SpringCloud

依賴

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-shaded-client</artifactId>
            <version>1.2.6</version>
        </dependency>
<!---->

yml配置:

自定義配置讀取zookeeper配置

hbase:
  zookeeper:
    quorum: hbase126-node2:2181

config配置:

import net.cc.commons.exception.CCRuntimeException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.io.IOException;
import java.util.function.Supplier;

/**
* @Author wangqiubao
* @Date 2019/9/24 15:28
* @Description
**/
@Configuration
public class UcareHbaseConfiguration {
    /**
     * 讀取HBase的zookeeper地址
     */
    @Value("${hbase.zookeeper.quorum}")
    private String quorum;

    /**
     * 配置HBase連接參數
     *
     * @return
     */
    @Bean
    public org.apache.hadoop.conf.Configuration hbaseConfig() {
        org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
        config.set(HConstants.ZOOKEEPER_QUORUM, quorum);
        return config;
    }
    //每次調用get方法就會創建一個Connection
    @Bean
    public Supplier<Connection> hbaseConnSupplier() {
        return () -> {
            try {
                return hbaseConnection();
            } catch (IOException e) {
                throw new CCRuntimeException(e);
            }
        };
    }

    @Bean
    //@Scope標明模式,默認單例模式.  prototype多例模式
    //若是在其他類中直接@Autowired引入的,多例就無效了,因為那個類在初始化的時候,已經創建了創建了這個bean了,之后調用的時候,不會重新創建,若是想要實現多例,就要每次調用的時候,手動獲取bean
    @Scope(value = "prototype")
    public Connection hbaseConnection() throws IOException {
        return ConnectionFactory.createConnection(hbaseConfig());
    }
}

使用

spring管理

    /**
     * 內部已實現線程安全的連接池
     */
    @Autowired
    private Connection hbaseConnection;

插入/更新數據

public void aaaa() throws IOException {
    try (Table table = hbaseConnection.getTable(TableName.valueOf("表名"))) {//獲取表連接
        //配置一條數據
        // 行鍵
        Put put = new Put(Bytes.toBytes("key主鍵"));
        put.addColumn(Bytes.toBytes("列族"), Bytes.toBytes("列"), Bytes.toBytes("值"));
        .....//每個有數據的列都要一個addColumn
        //put插入數據
        table.put(put);
    }
}

查詢

根據主鍵查詢內容
try (Table table = hbaseConnection.getTable(TableName.valueOf("表名"))) {
    Result result = table.get(new Get(asRowKey(date, acid)));
    if (result == null) return null;

    // 列名為starttime,最后一條就是該航班最新的航跡
    Cell latestCell = Iterables.getLast(result.listCells());
    return AdsbTrackProto.AdsbTrack.parseFrom(CellUtil.cloneValue(latestCell));
}





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM