Spring Boot HBase入门

一、HBase 简介和应用场景

1.1 HBase 是什么?

HBase 是什么?HBase 是在 Hadoop 分布式文件系统(简称:HDFS)之上的分布式面向列的数据库。而且是 2007 最初原型,历史悠久。

那追根究底,Hadoop 是什么?Hadoop是一个分布式环境存储并处理大数据。Hadoop 使用 MapReduce 算法统计分析大数据。这时候不得不说下 Google 的著名的三篇大数据的论文,分别讲述 GFS、MapReduce、BigTable,详见 https://www.bysocket.com/archives/2051

那回到 HBase,HBase 在 Hadoop 之上提供了类似 BigTable 的能力,它不同于一般的关系数据库,是一个适合非结构化数据存储的数据库。它也不同于行式数据库,是基于列的模式。

HBase 一个面向列的数据库,排序由行决定。简而言之:

  • 表是行的集合。
  • 行是列族的集合。列族,就是键值对。每个列族以 key 为列命名,可以有无数的列。
  • 列族就是列的集合。列连续存储,并且每个单元会有对应的时间戳
  • 列的存储也是键值对。

image.png

与行式数据库最大的区别就是,可以面向列设计巨大表,适用于在线分析处理 OLAP。
与关系型数据库 RDBMS 也有些区别如下:

  • HBase 宽表,横向扩展。RDBMS 小表,难成规模
  • HBase 没有事务
  • HBase 无规范化数据,都是键值对 key value

1.2 HBase 应用场景

官网上 hbase.apache.org,特性这么多:

Features:
Linear and modular scalability.
Strictly consistent reads and writes.
Automatic and configurable sharding of tables
Automatic failover support between RegionServers.
Convenient base classes for backing Hadoop MapReduce jobs with Apache HBase tables.
Easy to use Java API for client access.
Block cache and Bloom Filters for real-time queries.
Query predicate push down via server side Filters
Thrift gateway and a REST-ful Web service that supports XML, Protobuf, and binary data encoding options
Extensible jruby-based (JIRB) shell
Support for exporting metrics via the Hadoop metrics subsystem to files or Ganglia; or via JMX

最主要的还是特性能有什么应用场景?大致搜集了下业界的:

  • 监控数据的日志详情
  • 交易订单的详情数据(淘宝、有赞)
  • facebook 的消息详情

二、spring-boot-starter-hbase 开源简介

spring-boot-starter-hbase 是自定义的spring-boot 的 hbase starter,为 hbase 的 query 和更新等操作提供简易的 api 并集成spring-boot 的 auto configuration。

具体地址:

https://github.com/SpringForAll/spring-boot-starter-hbase

三、集成 HBase 实战

具体代码地址:https://github.com/JeffLi1993/springboot-learning-example

工程名:springboot-hbase

3.1 安装 spring-boot-starter-hbase 组件依赖

官网给的sample也是xml方式,具体可以参考
https://github.com/spring-projects/spring-hadoop-samples/tree/master/hbase

在 pom.xml 文件中,引入相关依赖。 

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-hadoop-hbase</artifactId>
            <version>2.5.0.RELEASE</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-hadoop</artifactId>
            <version>2.5.0.RELEASE</version>
        </dependency>

增加配置

官方提供的方式是通过xml方式,简单改写后如下:

@Configuration
public class HBaseConfiguration {

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

    @Value("${hbase.zookeeper.property.clientPort}")
    private String clientPort;

    @Value("${zookeeper.znode.parent}")
    private String znodeParent;

    @Bean
    public HbaseTemplate hbaseTemplate() {
        org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
        conf.set("hbase.zookeeper.quorum", zookeeperQuorum);
        conf.set("hbase.zookeeper.property.clientPort", clientPort);
        conf.set("zookeeper.znode.parent", znodeParent);
        return new HbaseTemplate(conf);
    }
}

application.yml 

hbase:
  zookeeper:
    quorum: hbase1.xxx.org,hbase2.xxx.org,hbase3.xxx.org
    property:
      clientPort: 2181

zookeeper:
  znode:
    parent: /hbase

 在service类注入HBaseTemplate

@Service
@Slf4j
public class HBaseService {


    @Autowired
    private HbaseTemplate hbaseTemplate;


    public List<Result> getRowKeyAndColumn(String tableName, String startRowkey, String stopRowkey, String column, String qualifier) {
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        if (StringUtils.isNotBlank(column)) {
            log.debug("{}", column);
            filterList.addFilter(new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(column))));
        }
        if (StringUtils.isNotBlank(qualifier)) {
            log.debug("{}", qualifier);
            filterList.addFilter(new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(qualifier))));
        }
        Scan scan = new Scan();
        if (filterList.getFilters().size() > 0) {
            scan.setFilter(filterList);
        }
        scan.setStartRow(Bytes.toBytes(startRowkey));
        scan.setStopRow(Bytes.toBytes(stopRowkey));

        return hbaseTemplate.find(tableName, scan, (rowMapper, rowNum) -> rowMapper);
    }

    public List<Result> getListRowkeyData(String tableName, List<String> rowKeys, String familyColumn, String column) {
        return rowKeys.stream().map(rk -> {
            if (StringUtils.isNotBlank(familyColumn)) {
                if (StringUtils.isNotBlank(column)) {
                    return hbaseTemplate.get(tableName, rk, familyColumn, column, (rowMapper, rowNum) -> rowMapper);
                } else {
                    return hbaseTemplate.get(tableName, rk, familyColumn, (rowMapper, rowNum) -> rowMapper);
                }
            }
            return hbaseTemplate.get(tableName, rk, (rowMapper, rowNum) -> rowMapper);
        }).collect(Collectors.toList());
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42073629/article/details/105804522