Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase

简介: HBase 是在 Hadoop 分布式文件系统(简称:HDFS)之上的分布式面向列的数据库。而且是 2007 最初原型,历史悠久。 那追根究底,Hadoop 是什么?Hadoop是一个分布式环境存储并处理大数据。本文介绍通过 spring-boot-starter-hbase 集成 HBase。

本文内容

  • HBase 简介和应用场景
  • spring-boot-starter-hbase 开源简介
  • 集成 HBase 实战
  • 小结

一、HBase 简介和应用场景

1.1 HBase 是什么?

HBase 是在 Hadoop 分布式文件系统(简称:HDFS)之上的分布式面向列的数据库。而且是 2007 最初原型,历史悠久。
那追根究底,Hadoop 是什么?Hadoop是一个分布式环境存储并处理大数据。Hadoop 使用 MapReduce 算法统计分析大数据。这时候不得不说下 Google 的著名的三篇大数据的论文,分别讲述 GFS、MapReduce、BigTable。
那回到 HBase,HBase 在 Hadoop 之上提供了类似 BigTable 的能力,它不同于一般的关系数据库,是一个适合非结构化数据存储的数据库。它也不同于行式数据库,是基于列的模式。

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

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

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

  • 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 组件依赖

因为不在公共仓库,只能自行安装。如果有 maven 私库,可以考虑安装到私库。
下载项目到本地:

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

安装依赖:

cd spring-boot-starter-hbase
mvn clean install

等待安装完毕即可。

3.2 工程集成依赖

目录结构如下:

springboot-hbase git:(master)
├── pom.xml
└── src
    └── main
        ├── java
        │   └── org
        │       └── spring
        │           └── springboot
        │               ├── Application.java
        │               ├── controller
        │               │   └── CityRestController.java
        │               ├── dao
        │               │   └── CityRowMapper.java
        │               ├── domain
        │               │   └── City.java
        │               └── service
        │                   ├── CityService.java
        │                   └── impl
        │                       └── CityServiceImpl.java
        └── resources
            └── application.properties

先在 pom.xml 加入 spring-boot-starter-hbase 组件依赖,也就是上面安装的依赖,核心加入代码如下:

<properties>
        <hbase-spring-boot>1.0.0.RELEASE</hbase-spring-boot>
    </properties>

    <!-- Spring Boot HBase 依赖 -->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>spring-boot-starter-hbase</artifactId>
            <version>${hbase-spring-boot}</version>
        </dependency>

然后配置相关 HBase 连接信息,具体 HBase 安装,网上文章一大堆。在 spring-boot 项目的 application.properties 文件中加入对应的配置项目,并检查配置是否正确:

## HBase 配置
spring.data.hbase.quorum=xxx
spring.data.hbase.rootDir=xxx
spring.data.hbase.nodeParent=xxx

具体配置项信息如下:

  • spring.data.hbase.quorum 指定 HBase 的 zk 地址
  • spring.data.hbase.rootDir 指定 HBase 在 HDFS 上存储的路径
  • spring.data.hbase.nodeParent 指定 ZK 中 HBase 的根 ZNode

3.3 HBase 保存查询操作

定义 DTO ,即 domain 包下的 City 对象:

public class City {

    /**
     * 城市编号
     */
    private Long id;

    /**
     * 省份年龄
     */
    private Integer age;

    /**
     * 城市名称
     */
    private String cityName;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }
}

然后定义该对象的 RowMapper,是用来和 HBase 存储作为映射:

public class CityRowMapper implements RowMapper<City> {

    private static byte[] COLUMN_FAMILY = "f".getBytes();
    private static byte[] NAME = "name".getBytes();
    private static byte[] AGE = "age".getBytes();

    @Override
    public City mapRow(Result result, int rowNum) throws Exception {
        String name = Bytes.toString(result.getValue(COLUMN_FAMILY, NAME));
        int age = Bytes.toInt(result.getValue(COLUMN_FAMILY, AGE));

        City dto = new City();
        dto.setCityName(name);
        dto.setAge(age);
        return dto;
    }
}

然后可以用 spring-boot-starter-hbase 组件的 HbaseTemplate 操作 HBase API 。具体操作逻辑写在 CityServiceImpl 业务逻辑实现:

@Service
public class CityServiceImpl implements CityService {

    @Autowired private HbaseTemplate hbaseTemplate;

    public List<City> query(String startRow, String stopRow) {
        Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow));
        scan.setCaching(5000);
        List<City> dtos = this.hbaseTemplate.find("people_table", scan, new CityRowMapper());
        return dtos;
    }

    public City query(String row) {
        City dto = this.hbaseTemplate.get("people_table", row, new CityRowMapper());
        return dto;
    }

    public void saveOrUpdate() {
        List<Mutation> saveOrUpdates = new ArrayList<Mutation>();
        Put            put           = new Put(Bytes.toBytes("135xxxxxx"));
        put.addColumn(Bytes.toBytes("people"), Bytes.toBytes("name"), Bytes.toBytes("test"));
        saveOrUpdates.add(put);

        this.hbaseTemplate.saveOrUpdates("people_table", saveOrUpdates);
    }
}

HbaseTemplate 提供常见的操作接口如下:

  • HbaseTemplate.find 返回 HBase 映射的 City 列表
  • HbaseTemplate.get 返回 row 对应的 City 信息
  • HbaseTemplate.saveOrUpdates 保存或者更新

如果 HbaseTemplate 操作不满足需求,完全可以使用 hbaseTemplate 的getConnection() 方法,获取连接。进而类似 HbaseTemplate 实现的逻辑,实现更复杂的需求查询等功能

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

工程名:springboot-hbase

四、小结

其实 starter 这种好处,大家也都知道。低耦合高内聚,类似 JDBCTemplate,将操作 HBase、ES 也好的 Client 封装下。然后每个业务工程拿来即用,不然肯定会有重复代码出现。
另外还是强调一点,合适的业务场景选择 HBase,常见如下:

  • 监控数据的日志详情
  • 交易订单的详情数据(淘宝、有赞)
  • facebook 的消息详情
目录
相关文章
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
968 2
|
人工智能 Java 机器人
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
Spring AI Alibaba集成Ollama,基于Java构建本地大模型应用,支持流式对话、knife4j接口可视化,实现高隐私、免API密钥的离线AI服务。
7866 2
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
存储 JSON Java
1126 0
|
数据可视化 Java BI
将 Spring 微服务与 BI 工具集成:最佳实践
本文探讨了 Spring 微服务与商业智能(BI)工具集成的潜力与实践。随着微服务架构和数据分析需求的增长,Spring Boot 和 Spring Cloud 提供了构建可扩展、弹性服务的框架,而 BI 工具则增强了数据可视化与实时分析能力。文章介绍了 Spring 微服务的核心概念、BI 工具在企业中的作用,并深入分析了两者集成带来的优势,如实时数据处理、个性化报告、数据聚合与安全保障。同时,文中还总结了集成过程中的最佳实践,包括事件驱动架构、集中配置管理、数据安全控制、模块化设计与持续优化策略,旨在帮助企业构建高效、智能的数据驱动系统。
590 1
将 Spring 微服务与 BI 工具集成:最佳实践
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
3154 0
|
监控 Cloud Native Java
Spring Integration 企业集成模式技术详解与实践指南
本文档全面介绍 Spring Integration 框架的核心概念、架构设计和实际应用。作为 Spring 生态系统中的企业集成解决方案,Spring Integration 基于著名的 Enterprise Integration Patterns(EIP)提供了轻量级的消息驱动架构。本文将深入探讨其消息通道、端点、过滤器、转换器等核心组件,以及如何构建可靠的企业集成解决方案。
1029 0