Spring Boot 2.x :經過 spring-boot-starter-hbase 集成 HBase

摘要: 原創出處 https://www.bysocket.com 「公衆號:泥瓦匠BYSocket 」歡迎關注和轉載,保留摘要,謝謝!

本文內容java

  • HBase 簡介和應用場景
  • spring-boot-starter-hbase 開源簡介
  • 集成 HBase 實戰
  • 小結

摘錄:Many a Man thinks he is buying Pleasure,when he is really sellinghimself a Slave to it.
許多人認爲本身花錢買了快樂,實際上是花錢作了快樂的奴隸。node

1、HBase 簡介和應用場景

1.1 HBase 是什麼?

HBase 是什麼?HBase 是在 Hadoop 分佈式文件系統(簡稱:HDFS)之上的分佈式面向列的數據庫。並且是 2007 最初原型,歷史悠久。git

那追根究底,Hadoop 是什麼?Hadoop是一個分佈式環境存儲並處理大數據。Hadoop 使用 MapReduce 算法統計分析大數據。這時候不得不說下 Google 的著名的三篇大數據的論文,分別講述 GFS、MapReduce、BigTable,詳見 https://www.bysocket.com/archives/2051。github

那回到 HBase,HBase 在 Hadoop 之上提供了相似 BigTable 的能力,它不一樣於通常的關係數據庫,是一個適合非結構化數據存儲的數據庫。它也不一樣於行式數據庫,是基於列的模式。算法

HBase 一個面向列的數據庫,排序由行決定。簡而言之:spring

  • 表是行的集合。
  • 行是列族的集合。列族,就是鍵值對。每一個列族以 key 爲列命名,能夠有無數的列。
  • 列族就是列的集合。列連續存儲,而且每一個單元會有對應的時間戳
  • 列的存儲也是鍵值對。

file

與行式數據庫最大的區別就是,能夠面向列設計巨大表,適用於在線分析處理 OLAP。
與關係型數據庫 RDBMS 也有些區別以下:shell

  • 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 JMXapache

最主要的仍是特性能有什麼應用場景?大體蒐集了下業界的:api

  • 監控數據的日誌詳情
  • 交易訂單的詳情數據(淘寶、有贊)
  • facebook 的消息詳情

2、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

3、集成 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

4、小結

其實 starter 這種好處,你們也都知道。低耦合高內聚,相似 JDBCTemplate,將操做 HBase、ES 也好的 Client 封裝下。而後每一個業務工程拿來即用,否則確定會有重複代碼出現。

另外仍是強調一點,合適的業務場景選擇 HBase,常見以下:

  • 監控數據的日誌詳情
  • 交易訂單的詳情數據(淘寶、有贊)
  • facebook 的消息詳情
  (關注微信公衆號,領取 Java 精選乾貨學習資料)
相關文章
相關標籤/搜索