摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢!

本文内容

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

摘录:Many a Man thinks he is buying Pleasure,when he is really sellinghimself a Slave to it.
许多人认为自己花钱买了快乐,其实是花钱做了快乐的奴隶。

一、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 为列命名,可以有无数的列。
  • 列族就是列的集合。列连续存储,并且每个单元会有对应的时间戳
  • 列的存储也是键值对。

与行式数据库最大的区别就是,可以面向列设计巨大表,适用于在线分析处理 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 组件依赖

因为不在公共仓库,只能自行安装。如果有 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 Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase的更多相关文章

  1. Spring Boot实战之定制自己的starter

    本文首发于个人网站,原文地址:http://www.javaadu.online/?p=535,如需转载,请注明出处 在学习Spring Boot的过程中,接触最多的就是starter.可以认为sta ...

  2. spring cloud教程之使用spring boot创建一个应用

    <7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...

  3. Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...

  4. [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...

  5. Spring Cloud Hoxton正式发布,Spring Boot 2.2 不再孤单

    距离Spring Boot 2.2.0的发布已经有一个半月左右时间,由于与之匹配的Spring Cloud版本一直没有Release,所以在这期间碰到不少读者咨询的问题都是由于Spring Boot和 ...

  6. 【自学Spring Boot】什么是Spring Boot

    为啥要有Spring Boot? 以前大学刚开始学java web的时候,需要搭建起web框架,当时使用的是SSH(struts+spring+hibernate),那就开始搭建吧,初学者哪里知道整套 ...

  7. 【spring】1.2、Spring Boot创建项目

    Spring Boot创建项目 在1.1中,我们通过"Spring Starter Project"来创建了一个项目,实际上是使用了Pivotal团队提供的全新框架Spring B ...

  8. Spring Boot 学习1-创建Spring Boot应用

    如果使用Maven, 确保先安装好Maven再继续. 创建POM文件 在这里有两种方式: 继承Spring Boot parent的pom. 不继承. 继承Spring Boot pom 1 2 3 ...

  9. Springboot(一):使用Intellij中的Spring Initializr来快速构建Spring Boot工程

    使用Intellij中的Spring Initializr来快速构建Spring Boot工程 New---Project 可以看到图所示的创建功能窗口.其中Initial Service Url指向 ...

随机推荐

  1. OpenGL执行渲染图片的主要操作步骤

    一个用来执行图形渲染的OpenGL程序的主要步骤包括: 1.从OpenGL的几何图元中设置数据,用于构建形状: 2.使用不用的着色器(shader)对输入的图元数据进行进行计算,判断它们的位置.颜色以 ...

  2. 「WC 2018」州区划分

    题目大意: 给一个无向图$G(V,E)$满足$|V|<=21$,对于某一种将$G(V,E)$划分为k个的有序集合方案,若每一个子集$G_i(V_i,E_i)$,$E_i=\{(x,y)|x\in ...

  3. 【BZOJ 4652】【NOI 2016】循环之美

    题目连接: 传送 题解: 真是一道好题…… 一: 一个分数$\frac{x}{y}$完全循环当其第一次出现时,当且仅当y与k互质,x与y互质,且y不等于1. 整数情况下y一定为1,即也满足以上判断. ...

  4. codeforces 671D

    首先O(n2)dp很好想 f[i][j]表示i子树内的所有边都被覆盖且i~j的路径也都被覆盖的最小花费. 考虑去掉无用的状态,其实真正用到的就是每一条链. 去掉第二维,f[i]表示i子树内的边都被覆盖 ...

  5. HrbustOJ 1564 螺旋矩阵

    Description 对于给定的一个数n,要你打印n*n的螺旋矩阵. 比如n=3时,输出: 1 2 3 8 9 4 7 6 5 Input 多组测试数据,每个测试数据包含一个整数n(1<=n& ...

  6. uni-app——想说爱你不容易之踩坑系列

    1.uni-app不支持动态组件,目前在用i-if判断,或者用scroll-view切换,没有想到什么其他的办法 2.uni-app不支持具名插槽,会导致页面塌陷 3.uni-app在做动态组件渲染的 ...

  7. wGenerator代码生成工具

    由来 以前一直用window系列的操作系统,有不少可以用的代码生成工具,如:动软的代码生成器(.net),可以自定义模板,然后按需生成代码.后来用mac系统,发现好像没有什么太好用的生成工具,所以自己 ...

  8. 关于Data URLs svg图片显示出错和浏览器URL hash #

    在使用生成的svg图作为<img>标签是src值时,发现有部分浏览器显示异常,所以这里记录下 参考链接 Data URLs http://www.faqs.org/rfcs/rfc2397 ...

  9. 解决Dynamics 365使用JS调用Web API时报no property value was found in the payload 错误。

    摘要: 微软动态CRM专家罗勇 ,回复323或者20190421可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! 碰到如下报错: message: "An er ...

  10. Android底部导航栏(可滑动)----TabLayout+viewPager

    [TabLayout] ①TabLayout是选项卡,在屏幕空间有限的情况下,对不同的空间进行分组.属于android support design,更多的用于新闻上,如果放在底部也可做底部导航栏 ② ...