项目作者: Al-assad

项目描述 :
spring-data-hadoop 中的 hbase 代码部分提取,支持 hbase 2.x API
高级语言: Java
项目地址: git://github.com/Al-assad/spring-data-hbase.git


Spring-Data-Hbase

spring-data-hadoop 项目 HBaseTemplate API 提取,支持 HBase 2.x API


项目说明

由于 spring-data-hadoop 目前已经停止更新(2019-04-05),最后一个版本不支持 Hbase-Client-2.x 的 API,此项目基于 spring-hadoop 代码,提取 spring-data-hbase 部分的代码,更新相关代码以支持 HBase 2.x。

该项目引入的主要组件依赖版本如下:

  • spring-boot:2.1.8.RELEASE

  • hbase-client:2.0.0

实际使用可以根据实际版本修改 pom 并重新打包;


HBase 连接配置

spring-data-hbase 已经集成了 spring-boot 的 auto configuration 功能,自动读取 yml 或者 properties 中以 spring.data.hbase.config 为前缀的配置项,作为 hbase 的连接配置项,以下是一个示例的 yml 配置文件:

  1. spring.data.hbase.config:
  2. hbase.zookeeper.quorum: hbase
  3. hbase.zookeeper.property.clientPort: 2181
  4. hbase.zookeeper.parent: /hbase
  5. hbase.rootdir: hdfs://namenode:9000/hbase


HBaseTemplate 使用说明

HBaseTemplate 的使用和原生 HBaseTemplate API 完全一致,所有的测试代码位于 /src/test 目录下,以下作简要说明:

示例用的 HBase 初始化语句

  1. create 'city', 'f0'

映射用的 DTO 实体

  1. public class CityDTO {
  2. private String rowkey;
  3. private String name;
  4. private String province;
  5. private String code;
  6. .......
  7. }

HBase Put 操作

  1. /**
  2. * 新增/更新数据
  3. */
  4. @Test
  5. public void testPut() {
  6. hbaseTemplate.execute("city", (TableCallback<Object>) table -> {
  7. String rowkey = "key122";
  8. Put put = new Put(Bytes.toBytes(rowkey));
  9. put.addColumn(Bytes.toBytes("f0"), Bytes.toBytes("name"), Bytes.toBytes("guangzhou"));
  10. put.addColumn(Bytes.toBytes("f0"), Bytes.toBytes("province"), Bytes.toBytes("guangdong"));
  11. put.addColumn(Bytes.toBytes("f0"), Bytes.toBytes("code"), Bytes.toBytes("G-11"));
  12. table.put(put);
  13. return rowkey;
  14. });
  15. }
  16. /**
  17. * 删除数据
  18. */
  19. @Test
  20. public void testDelete() {
  21. hbaseTemplate.execute("city", (TableCallback<Object>) table -> {
  22. String rowkey = "key122";
  23. Delete delete = new Delete(Bytes.toBytes(rowkey));
  24. table.delete(delete);
  25. return rowkey;
  26. });
  27. }

HBase Get/Scan 操作

  1. /**
  2. * get 操作
  3. */
  4. @Test
  5. public void testGet() {
  6. String rowkey = "key122";
  7. CityDTO results = hbaseTemplate.get("city", rowkey, (result, rowNum) -> {
  8. CityDTO dto = new CityDTO.CityDTOBuilder()
  9. .rowkey(new String(result.getRow()))
  10. .name(new String(result.getValue(Bytes.toBytes("f0"), Bytes.toBytes("name"))))
  11. .province(new String(result.getValue(Bytes.toBytes("f0"), Bytes.toBytes("province"))))
  12. .code(new String(result.getValue(Bytes.toBytes("f0"), Bytes.toBytes("code"))))
  13. .build();
  14. return dto;
  15. });
  16. System.out.println(results);
  17. }
  18. /**
  19. * scan 操作
  20. */
  21. @Test
  22. public void testScanFilter() {
  23. Scan scan = new Scan().withStartRow(Bytes.toBytes("key100")).withStopRow(Bytes.toBytes("key300"));
  24. scan.addFamily(Bytes.toBytes("f0"));
  25. scan.setFilter(new SingleColumnValueFilter(
  26. Bytes.toBytes("f0"),
  27. Bytes.toBytes("code"),
  28. CompareOperator.EQUAL,
  29. Bytes.toBytes("G-11")));
  30. List<CityDTO> results = hbaseTemplate.find("city", scan, (result, rowNum) -> {
  31. CityDTO dto = new CityDTO.CityDTOBuilder()
  32. .rowkey(new String(result.getRow()))
  33. .name(new String(result.getValue(Bytes.toBytes("f0"), Bytes.toBytes("name"))))
  34. .province(new String(result.getValue(Bytes.toBytes("f0"), Bytes.toBytes("province"))))
  35. .code(new String(result.getValue(Bytes.toBytes("f0"), Bytes.toBytes("code"))))
  36. .build();
  37. return dto;
  38. });
  39. results.forEach(System.out::println);
  40. }