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));
}




posted @ 2020-07-30 18:10  紫月java  阅读(10014)  评论(0编辑  收藏  举报