自定义spring-boot-starter-hbase
来源:互联网 发布:开源中国android源码 编辑:程序博客网 时间:2024/04/26 08:56
- 项目背景
- 代码
- github地址
- 打包
- 使用方式
- 依赖
- 集成
- 使用
- query
- update等
- 其他
项目背景
- 公司越来越多的项目采用spring-boot+hbase的查询,由于公司的hbase版本为1.x的CDH版本,本身没有spring-boot的starter,故写出一个spring-boot-starter-hbase提供给各方项目使用。
- 自定义的spring-boot的hbase starter,为hbase的query和更新等操作提供简易的api并集成spring-boot的auto configuration。
代码
代码较多,见github,参考了spring-data-hadoop中hbase的api设计
github地址
https://github.com/JThink/spring-boot-starter-hbase
打包
修改相关的maven私服地址
gradle clean install uploadArchives
使用方式
依赖
compile "jthink:spring-boot-starter-hbase:0.0.1"
集成
在spring-boot项目的application.properties文件中加入spring.data.hbase.quorum配置项,并赋予正确的值
使用
query
- 将上述配置项赋予正确的值
- dto定义
public class PeopleDto { private String name; private int age; public String getName() { return name; } public PeopleDto setName(String name) { this.name = name; return this; } public int getAge() { return age; } public PeopleDto setAge(int age) { this.age = age; return this; }}
- RowMapper定义
import com.jthink.skyeye.data.hbase.api.RowMapper;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.util.Bytes;public class PeopleRowMapper implements RowMapper<PeopleDto> { private static byte[] COLUMNFAMILY = "f".getBytes(); private static byte[] NAME = "name".getBytes(); private static byte[] AGE = "age".getBytes(); @Override public PeopleDto mapRow(Result result, int rowNum) throws Exception { PeopleDto dto = new PeopleDto(); // TODO: 设置相关的属性值 String name = Bytes.toString(result.getValue(COLUMNFAMILY, NAME)); int age = Bytes.toInt(result.getValue(COLUMNFAMILY, AGE)); return dto.setName(name).setAge(age); }}
- query操作
import com.jthink.skyeye.data.hbase.api.HbaseTemplate;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.util.Bytes;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;public class QueryService { @Autowired private HbaseTemplate hbaseTemplate; public List<PeopleDto> query(String startRow, String stopRow) { Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); scan.setCaching(5000); List<PeopleDto> dtos = this.hbaseTemplate.find("people_table", scan, new PeopleRowMapper()); return dtos; } public PeopleDto query(String row) { PeopleDto dto = this.hbaseTemplate.get("people_table", row, new PeopleRowMapper()); return dto; }}
update等
- 将上述配置项赋予正确的值
- update、delete、put操作
import com.jthink.skyeye.data.hbase.api.HbaseTemplate;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Mutation;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.util.Bytes;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.ArrayList;import java.util.List;@Servicepublic class QueryService { @Autowired private HbaseTemplate hbaseTemplate; public List<PeopleDto> query(String startRow, String stopRow) { Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); scan.setCaching(5000); List<PeopleDto> dtos = this.hbaseTemplate.find("people_table", scan, new PeopleRowMapper()); return dtos; } public PeopleDto query(String row) { PeopleDto dto = this.hbaseTemplate.get("people_table", row, new PeopleRowMapper()); return dto; } public void saveOrUpdates() { List<Mutation> puts = new ArrayList<>(); // 设值 this.hbaseTemplate.saveOrUpdates("people_table", puts); } public void saveOrUpdate() { Mutation delete = new Delete(Bytes.toBytes("")); this.hbaseTemplate.saveOrUpdate("people_table", delete); }}
其他
不可以满足需求的可以使用hbaseTemplate暴露出来的getConnection()方法
1 0
- 自定义spring-boot-starter-hbase
- spring boot自定义starter
- Spring Boot 自定义starter
- 自定义spring-boot-starter-dubbox
- Spring-boot实例学习之 自定义starter
- 自定义spring-boot-starter(IDEA开发)
- Spring Boot自定义starter【周三更新】【 Spring Boot视频】
- spring boot starter 详解
- spring-boot-starter-dubbo
- spring-boot-starter-security
- spring boot 启动器starter
- spring-boot-starter
- Spring Boot Starter 介绍
- Druid Spring Boot Starter
- mybatis-spring-boot-starter
- druid-spring-boot-starter
- Spring-boot-Mybatis-Starter
- Spring Boot 自动配置理解 以及实现自定义Starter
- 谷歌 Adobe Flash 无法正常使用
- JQuery is()与hasClass()方法的对比
- Android 仿微信底部渐变Tab(2)
- 进程间通信(四)------消息队列
- AOP概念与运行原理
- 自定义spring-boot-starter-hbase
- [RL] 3 Finite Markov Decision Processes (1)
- CSDN日报20170321——《给大学生的几条良心建议》
- Android使用fitsSystemWindows属性实现–状态栏【status_bar】各版本适配方案
- 如何利用C库检查一个路径下是否存在某文件
- Effective Java读书笔记——第十章 并发
- kettle的基本教程
- 红黑树并没有我们想象的那么难(下)
- RxJava学习笔记整理重点