springBoot 整合Hbase及自带聚和协处理器的使用

本文介绍了如何在SpringBoot中整合Hbase,避免使用spring-boot-starter-hbase的方式,而是采用org.apache.hbase提供的工具包。详细讲述了引入依赖、编写工具类的步骤,并特别提到Hbase的AggregateImplementation聚合协处理器在1.x版本的平均值计算问题,以及在2.1.0版本的改进。同时强调了Table和Admin对象的线程安全问题以及ColumnInterpreter的使用注意事项。
摘要由CSDN通过智能技术生成

1、前言

springBoot整合hbase有两种方式:

  • 一种是使用spring-boot-starter-hbase,但是这种方式,使用时需要先创建hbase表的实体类和转换类,有点类似jpa,但是对于非关系型数据库,我不是很喜欢这种用法。而且spring-boot-starter-hbase只有一个1.0.0.RELEASE的版本,对于新版hbase的兼容性尚待测试。
  • 所以本文介绍的是第二种方式,使用org.apache.hbase提供的工具包,使用的版本是2.1.0


2、引入依赖

		<!-- hbase 客户端 -->
		<dependency>
			<groupId>org.apache.hbase</groupId>
			<artifactId>hbase-client</artifactId>
			<version>2.1.0</version>
		</dependency>

		<!-- hbase协处理器 -->
		<dependency>
			<groupId>org.apache.hbase</groupId>
			<artifactId>hbase-endpoint</artifactId>
			<version>2.1.0</version>
		</dependency>

3、编写工具类

@Component
public class HbaseUtil {
    
    /**
     * The Logger.
     */
    Logger logger = LoggerFactory.getLogger(HbaseUtil.class);
    
    /**
     * hbase连接对象
     */
    private Connection conn;
    
    /**
     * hbase zookeeper地址
     */
    @Value("${zookeeper.ip}")
    private String zookeeper;
    
    /**
     * hbase自带聚和协处理器
     */
    private String gatherCoprocessor = AggregateImplementation.class.getName();
    
    /**
     * 初始化连接
     */
    @PostConstruct
    private void initConnection() {
        Configuration config = getConfig();
        try {
            //获取连接
            conn = ConnectionFactory.createConnection(config);
            logger.info("初始化hbase连接");
        } catch (Exception e) {
            logger.error("初始化失败", e);
        }
    }
    
    /**
     * 获取配置对象
     * 
     * @return
     */
    private Configuration getConfig() {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", zookeeper);
        return config;
    }
    
    /**
     * 获取连接
     */
    private Connection getConnection() {
        if (conn.isClosed()) {
            synchronized (this) {
                if (conn.isClosed()) {
                    initConnection();
                }
            }
        }
        return conn;
    }
    
    /**
     * 获取表连接
     * 
     * @param tableName
     * @return
     * @throws IOException
     */
    private Table getTable(String tableName)
        throws IOException {
        return getConnection().getTable(TableName.valueOf(tableName));
    }
    
    /**
     * 获取admin连接
     * 
     * @return
     * @throws IOException
     */
    private Admin getAdmin()
        throws IOException {
        return getConnection().getAdmin();
    }
    
    /**
     * Creat table boolean.创建表
     *
     * @param tableName the table name表名
     * @param columnFamily the column family列族名的集合
     * @return the boolean
     */
    public boolean creatTable(String tableName, List<String> columnFamily) {
        TableName table = TableName.valueOf(tableName);
        try (Admin admin = getAdmin();) {
            if (!admin.tableExists(table)) {
                TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(table);
                for (String s : columnFamily) {
                    tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(s)).build());
                }
                admin.createTable(tableDescriptor.build());
            }
        } catch (Exception e) {
            logger.error("创建表失败", e);
        }
        return true;
    }
    
    /**
     * Gets all table names.获取所有表名
     *
     * @return the all table names
     */
    public List<String> getAllTableNames() {
        List<String> result = new ArrayList<>();
        try (Admin admin = getAdmin();) {
            TableName[] tableNames = admin.listTableNames();
            for (TableName tableName : tableNames) {
                result.add(tableName.getNameAsString());
            }
        } catch (Exception e) {
            logger.error("获取所有表的表名失败", e);
        }
        return result;
    }
    
    /**
     * Delete table boolean.删除表
     *
     * @param tableName the table name要删除的表名
     * @return the boolean
     */
    public boolean deleteTable(String tableName) {
        try (Admin admin = getAdmin();) {
            if (admin.tableExists(TableName
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值