SpringBoot整合HBase实现常用操作

导读:本篇文章讲解 SpringBoot整合HBase实现常用操作,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1.开发环境

SpringBoot:2.3.10
HBase:2.3.5
JDK:1.8
本文基于已经搭建好的HBase开发环境,若不清楚的可以阅读另一篇win10搭建Hadoop3.2.2和HBase2.3.5单机版

2.引入maven依赖

新建springboot-hbase模块,在pom.xml中引入如下依赖:

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.3.5</version>
</dependency>

3.application.yml配置

server:
  port: 8122
spring:
  application:
    name: springboot-hbase
hbase:
  config:
    zookeeper:
      quorum: 127.0.0.1
      port: 2181

4.HBase工具类

该类主要是用于连接HBase数据,具体代码如下:

@Component
public class HBaseUtils {

    private static String quorums;

    @Value("${hbase.config.zookeeper.quorum}")
    private String quorum;

    @PostConstruct
    public void getQuorum() {
        quorums = this.quorum;
    }

    /**
     * 获取hbase连接
     * @return
     * @throws Exception
     */
    public static Connection getHBaseConnection() {
        Connection connection = null;
        try {
            //hbase配置对象
            Configuration configuration = HBaseConfiguration.create();
            //设置zookeeper地址
            configuration.set("hbase.zookeeper.quorum", quorums);
            //获取hbase连接对象
            connection = ConnectionFactory.createConnection(configuration);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
}

根据表名获取表信息代码如下:

public static Table getTable(String tbName) throws Exception {
    TableName tableName = TableName.valueOf(tbName);
    Table table = getHBaseConnection().getTable(tableName);
    return table;
}

5.HBase常用操作业务接口和实现类

常用的接口如下代码:

/**
 * 判断表是否存在
 * @param tableName 表名
 * @return
 */
boolean isTableExist(String tableName) throws Exception;

/**
 * 新建表
 * @param tableName 表名
 * @param columnFamily 列族
 */
void createTable(String tableName, String... columnFamily) throws Exception;
/**
 * 添加数据
 * @param request
 * @throws Exception
 */
void addData(RequestDto request) throws Exception;

/**
 * 获取一条数据
 * @param tableName 表名
 * @param rowKey
 */
Result getOneData(String tableName, String rowKey) throws Exception;

/**
 * 获取多条数据
 * @param tableName 表名
 * @param startRowKey 开始rowkey
 * @param endRowKey 结束rowkey
 */
ResultScanner getMultiData(String tableName, String startRowKey, String endRowKey) throws Exception;

/**
 * 获取所有数据
 * @param tableName
 */
ResultScanner getAllData(String tableName) throws Exception;

/**
 * 根据row过滤器查询
 * @param tableName 表名
 * @param rowStr row过滤符
 * @return
 * @throws Exception
 */
ResultScanner getDataByRow(String tableName, String rowStr) throws Exception;

/**
 * 获取列名
 * @param tableName
 * @return
 * @throws IOException
 */
Result getRowQualifier(String tableName) throws Exception;

/**
 * 根据rowkey和rowkey前缀分页查询
 * @param tableName 表名
 * @param rowKey
 * @param rowStr 前缀
 * @param pageSize 每页条数
 * @return
 */
ResultScanner getPageDataByRow(String tableName, String rowKey, String rowStr, int pageSize) throws Exception;

/**
 * 根据行权限和列权限分页查询
 * @param tableName
 * @param rowKey
 * @param rowStr
 * @param qualifierStr
 * @param pageSize
 * @return
 */
ResultScanner getPageDataByRowAndQualifier(String tableName, String rowKey, String rowStr, String qualifierStr, int pageSize) throws Exception;

接口实现类代码如下:

@Override
public boolean isTableExist(String tableName) throws Exception {
    Admin admin = HBaseUtils.getAdmin();
    boolean exists = admin.tableExists(TableName.valueOf(tableName));
    return exists;
}

@Override
public void createTable(String tableName, String... columnFamily) throws Exception {
    if (this.isTableExist(tableName)) {
        log.info("该表{}已存在,不需要创建!", tableName);
    }
    Admin admin = HBaseUtils.getAdmin();
    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
    for (String family : columnFamily) {
        ColumnFamilyDescriptorBuilder familyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));
        ColumnFamilyDescriptor familyDescriptor = familyDescriptorBuilder.build();
        builder.setColumnFamily(familyDescriptor);
        TableDescriptor tableDescriptor = builder.build();
        admin.createTable(tableDescriptor);
    }
    admin.close();
}
@Override
public void addData(RequestDto request) throws Exception {
    Table table = HBaseUtils.getTable(request.getTableName());
    Put put = new Put(Bytes.toBytes(request.getRowKey()));
    put.addColumn(Bytes.toBytes(request.getColumnFamily()), Bytes.toBytes(request.getQualifier()), Bytes.toBytes(request.getValue()));
    table.put(put);
    table.close();
}
@Override
public Result getOneData(String tableName, String rowKey) throws Exception {
    Table table = HBaseUtils.getTable(tableName);
    Get get = new Get(Bytes.toBytes(rowKey));
    Result result = table.get(get);
    return result;
}

@Override
public ResultScanner getMultiData(String tableName, String startRowKey, String endRowKey) throws Exception {
    Table table = HBaseUtils.getTable(tableName);
    Scan scan = new Scan();
    scan.withStartRow(Bytes.toBytes(startRowKey));
    scan.withStopRow(Bytes.toBytes(endRowKey));
    ResultScanner results = table.getScanner(scan);
    table.close();
    return results;
}

@Override
public ResultScanner getAllData(String tableName) throws Exception {
    Table table = HBaseUtils.getTable(tableName);
    Scan scan = new Scan();
    ResultScanner results = table.getScanner(scan);
    table.close();
    return results;
}

@Override
public ResultScanner getDataByRow(String tableName, String rowStr) throws Exception {
    Table table = HBaseUtils.getTable(tableName);
    Scan scan = new Scan();
    Filter rowFilter = new RowFilter(CompareOperator.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes(rowStr)));
    scan.setFilter(rowFilter);
    ResultScanner results = table.getScanner(scan);
    return results;
}

@Override
public Result getRowQualifier(String tableName) throws Exception {
    Table table = HBaseUtils.getTable(tableName);
    Scan scan = new Scan();
    scan.setLimit(1);
    ResultScanner results = table.getScanner(scan);
    Result result = null;
    if (null != results) {
        result = results.next();
    }
    return result;
}

@Override
public ResultScanner getPageDataByRow(String tableName, String rowKey, String rowStr, int pageSize) throws Exception {
    Table table = HBaseUtils.getTable(tableName);
    Scan scan = new Scan();
    PageFilter pageFilter = new PageFilter(pageSize);
    RowFilter rowFilter = new RowFilter(CompareOperator.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes(rowStr)));
    FilterList filterList = new FilterList();
    filterList.addFilter(pageFilter);
    filterList.addFilter(rowFilter);
    scan.setFilter(filterList);
    if (!" ".equals(rowKey)) {
        scan.withStartRow(Bytes.toBytes(rowKey));
    }
    ResultScanner results = table.getScanner(scan);
    return results;
}

@Override
public ResultScanner getPageDataByRowAndQualifier(String tableName, String rowKey, String rowStr, String qualifierStr, int pageSize) throws Exception {
    Table table = HBaseUtils.getTable(tableName);
    Scan scan = new Scan();
    PageFilter pageFilter = new PageFilter(pageSize);
    RowFilter rowFilter = new RowFilter(CompareOperator.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes(rowStr)));
    SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), CompareOperator.GREATER_OR_EQUAL, Bytes.toBytes(qualifierStr));
    FilterList filterList = new FilterList();
    filterList.addFilter(pageFilter);
    filterList.addFilter(rowFilter);
    filterList.addFilter(singleColumnValueFilter);
    scan.setFilter(filterList);
    if (!" ".equals(rowKey)) {
        scan.withStartRow(Bytes.toBytes(rowKey));
    }
    ResultScanner results = table.getScanner(scan);
    return results;
}

以上内容为SpringBoot整合HBase常用操作的核心代码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/76813.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!