• Springboot集成HBase使用案例


    一、HBase部署安装

    1.HBase下载地址:Apache HBase – Apache HBase Downloads

    2.解压启动(进入压缩包bin目录),本次演示的是macOS系统启动演示(windows系统启动命令:./start-hbase.cmd)

    ./start-hbase.sh

     

    3. 浏览器访问http://IP:16010/master-status出现如下页面表示启动成功

     

     4.HBase控制台操作

    4.1进入控制台:

    1. 1. 进入HBase安装包的bin目录
    2. 2. ./hbase shell

    4.2.Hbase控制台常用命令总结如下

    1. 1.进入hbase shell命令行: ./hbase shell (bin目录下执行)
    2. 2.扫描全表全部数据: scan '表名'
    3. 3.查看连接的用户: whomi
    4. 4.创建表: create ‘表名’,’列族名1’,’列族名2’
    5. 5.查看数据库中全部的表: list
    6. 6.describe查看表结构: describe '表名'
    7. 7.删除表: drop '表名'
    8. 8.插入数据: put ‘表名’,’列族名1:列名1’,’值’
    9. 9.get获取数据: get '表名',行,列族,列
    10. 10.count计算表的行数: count '表名'
    11. 11.删除整行数据: deleteall '表名','行号'
    12. 12.删除全表数据: truncate '表名'

     4.3.HBase表结构以及参数说明

    1. /**
    2. * @param tableName 表名
    3. * @param rowKey 行主键
    4. * @param columnFamily 列族
    5. * @param column 列名
    6. * @param value 列值
    7. */

     5.代码如下

    5.1.maven依赖(本项目引用的Springboot版本是2.5.6)

    1. org.apache.hbase
    2. hbase-client
    3. 2.2.3
    4. io.springfox
    5. springfox-boot-starter
    6. 3.0.0
    7. com.github.xiaoymin
    8. knife4j-spring-boot-starter
    9. 3.0.3
    10. org.apache.hbase
    11. hbase-client
    12. 2.2.3
    13. com.alibaba
    14. fastjson
    15. 1.2.68
    16. cn.hutool
    17. hutool-all
    18. 5.3.2

    yml配置

    1. server:
    2. port: 8081
    3. #swagger文档开启/关闭
    4. springfox:
    5. documentation:
    6. auto-startup: true
    7. #knife4j
    8. knife4j:
    9. production: false # 开启/屏蔽文档资源
    10. #HBase
    11. hbase:
    12. config:
    13. hbase:
    14. zookeeper:
    15. property:
    16. clientPort: 2181
    17. quorum: 127.0.0.1

    5.2基础配置

    1. import org.apache.hadoop.conf.Configuration;
    2. import org.apache.hadoop.hbase.HBaseConfiguration;
    3. import org.apache.hadoop.hbase.client.Connection;
    4. import org.apache.hadoop.hbase.client.ConnectionFactory;
    5. import org.springframework.boot.context.properties.EnableConfigurationProperties;
    6. import org.springframework.context.annotation.Bean;
    7. import java.io.IOException;
    8. import java.util.Map;
    9. /**
    10. * @author :jerry
    11. * @date :Created in 2022/11/7 09:54
    12. * @description
    13. * @version: V1.1
    14. */
    15. @EnableConfigurationProperties(HbaseProperties.class)
    16. @org.springframework.context.annotation.Configuration
    17. public class HbaseConfig {
    18. private final HbaseProperties prop;
    19. public HbaseConfig(HbaseProperties properties) {
    20. this.prop = properties;
    21. }
    22. @Bean
    23. public Configuration configuration() {
    24. Configuration configuration = HBaseConfiguration.create();
    25. Map config = prop.getConfig();
    26. config.forEach(configuration::set);
    27. return configuration;
    28. }
    29. @Bean
    30. public Connection getConnection() throws IOException {
    31. return ConnectionFactory.createConnection(configuration());
    32. }
    33. }

    1. import org.springframework.boot.context.properties.ConfigurationProperties;
    2. import java.util.Map;
    3. /**
    4. * @author :jerry
    5. * @date :Created in 2022/11/7 09:52
    6. * @description
    7. * @version: V1.1
    8. */
    9. @ConfigurationProperties(prefix = "hbase")
    10. public class HbaseProperties {
    11. private Map config;
    12. public Map getConfig() {
    13. return config;
    14. }
    15. public void setConfig(Map config) {
    16. this.config = config;
    17. }
    18. }

    swagger配置

    1. import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import springfox.documentation.builders.ApiInfoBuilder;
    5. import springfox.documentation.builders.PathSelectors;
    6. import springfox.documentation.builders.RequestHandlerSelectors;
    7. import springfox.documentation.oas.annotations.EnableOpenApi;
    8. import springfox.documentation.service.ApiInfo;
    9. import springfox.documentation.service.Contact;
    10. import springfox.documentation.spi.DocumentationType;
    11. import springfox.documentation.spring.web.plugins.Docket;
    12. /**
    13. * @author :jerry
    14. * @date :Created in 2022/5/28 10:30
    15. * @description
    16. * @version: V1.1
    17. * *Swagger3API文档的配置
    18. * knife4j文档地址(端口号根据自己项目配置): http://localhost:8081/doc.html#
    19. * swagger文档地址(端口号根据自己项目配置):http://localhost:8081/swagger-ui/index.html#/
    20. */
    21. @Configuration
    22. @EnableOpenApi
    23. @EnableKnife4j
    24. public class Swagger3Config {
    25. @Bean
    26. public Docket api() {
    27. return new Docket(DocumentationType.OAS_30)
    28. // .groupName("webApi")
    29. .apiInfo(apiInfo())
    30. .select()
    31. .apis(RequestHandlerSelectors.basePackage("com.cetc.controller"))
    32. .paths(PathSelectors.any())
    33. .build();
    34. }
    35. @Bean
    36. public ApiInfo apiInfo() {
    37. return new ApiInfoBuilder()
    38. .title("SwaggerUI接口文档")
    39. .description("接口文档Swagger-Bootstrap版")
    40. .termsOfServiceUrl("http://localhost:8081/swagger-ui/index.html#/")
    41. .contact(new Contact("jerry","http://localhost:8081/doc.html#", "13258239832@163.com"))
    42. .version("1.0")
    43. .license("jerry")
    44. .build();
    45. }
    46. }

    5.3.控制层代码

    1. import com.cetc.dto.HBaseDto;
    2. import com.cetc.service.HbaseService;
    3. import com.cetc.util.CommonResultVo;
    4. import io.swagger.annotations.Api;
    5. import io.swagger.annotations.ApiOperation;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.web.bind.annotation.*;
    8. import java.io.IOException;
    9. import java.util.List;
    10. import java.util.Map;
    11. /**
    12. * @author :jerry
    13. * @date :Created in 2022/11/7 10:39
    14. * @description
    15. * @version: V1.1
    16. */
    17. @Api(tags = "habse接口测试")
    18. @RestController
    19. @RequestMapping("/hbase")
    20. public class HbaseController {
    21. @Autowired
    22. private HbaseService hbaseService;
    23. /**
    24. * 创建表
    25. * tableName:表名
    26. * columnFamilies:列族
    27. */
    28. @ApiOperation(value = "创建表")
    29. @GetMapping("/createTable")
    30. public CommonResultVo createTable(String tableName, String[] columnFamilies) throws IOException {
    31. return hbaseService.createTable(tableName, columnFamilies);
    32. }
    33. @ApiOperation(value = "新增/修改")
    34. @PostMapping("/saveOrUpdate")
    35. public CommonResultVo saveOrUpdate(@RequestBody List list) throws IOException {
    36. for (HBaseDto hBaseDto : list) {
    37. hbaseService.saveOrUpdate(hBaseDto);
    38. }
    39. return CommonResultVo.success();
    40. }
    41. @ApiOperation(value = "删除表/行/列族/列")
    42. @PostMapping("/deleteTable")
    43. public CommonResultVo deleteTable(@RequestBody HBaseDto hBaseDto) throws IOException {
    44. return hbaseService.deleteTable(hBaseDto);
    45. }
    46. // @ApiOperation(value = "判断表是否已经存在")
    47. // @GetMapping("/tableExists")
    48. // public CommonResultVo tableExists(String tableName) throws IOException {
    49. // boolean flag = hbaseService.tableExists(tableName);
    50. // return CommonResultVo.success(flag);
    51. // }
    52. @ApiOperation(value = "高级条件查询")
    53. @PostMapping("/scanRowData")
    54. public CommonResultVo scanRowData(@RequestBody HBaseDto hBaseDto) throws IOException {
    55. return hbaseService.scanRowData(hBaseDto);
    56. }
    57. @ApiOperation(value = "分页查询")
    58. @PostMapping("/scanPageRow")
    59. public CommonResultVo scanPageRow(@RequestBody HBaseDto hBaseDto) throws IOException {
    60. return hbaseService.scanPageRow(hBaseDto);
    61. }
    62. //
    63. // @ApiOperation(value = "总行数")
    64. // @PostMapping("/countRow")
    65. // public CommonResultVo countRow(@RequestBody HBaseDto hBaseDto) throws IOException {
    66. // return CommonResultVo.success(hbaseService.countRow(hBaseDto));
    67. // }
    68. }

    5.4数据逻辑层代码

    1. import com.cetc.dto.HBaseDto;
    2. import com.cetc.util.CommonResultVo;
    3. import java.io.IOException;
    4. import java.util.List;
    5. import java.util.Map;
    6. /**
    7. * @author :jerry
    8. * @date :Created in 2022/11/7 10:01
    9. * @description
    10. * @version: V1.1
    11. */
    12. public interface HbaseService {
    13. //创建表
    14. CommonResultVo createTable(String tableName, String... columnFamilies) throws IOException;
    15. void saveOrUpdate(HBaseDto hBaseDto) throws IOException;
    16. CommonResultVo deleteTable(HBaseDto hBaseDto) throws IOException;
    17. //判断表是否已经存在,这里使用间接的方式来实现
    18. boolean tableExists(String tableName) throws IOException;
    19. CommonResultVo scanRowData(HBaseDto hBaseDto) throws IOException;
    20. CommonResultVo scanPageRow(HBaseDto hBaseDto) throws IOException;
    21. long countRow(HBaseDto hBaseDto) throws IOException;
    22. }
    1. import com.cetc.config.HbaseConfig;
    2. import com.cetc.dto.HBaseDto;
    3. import com.cetc.service.HbaseService;
    4. import com.cetc.util.CommonResultVo;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.apache.commons.lang3.StringUtils;
    7. import org.apache.hadoop.hbase.*;
    8. import org.apache.hadoop.hbase.client.*;
    9. import org.apache.hadoop.hbase.filter.RowFilter;
    10. import org.apache.hadoop.hbase.filter.SubstringComparator;
    11. import org.apache.hadoop.hbase.util.Bytes;
    12. import org.springframework.beans.factory.annotation.Autowired;
    13. import org.springframework.stereotype.Service;
    14. import javax.annotation.PostConstruct;
    15. import java.io.IOException;
    16. import java.util.*;
    17. /**
    18. * @author :jerry
    19. * @date :Created in 2022/11/7 09:58
    20. * @description
    21. * @version: V1.1
    22. */
    23. @Service
    24. @Slf4j
    25. public class HbaseServiceImpl implements HbaseService {
    26. @Autowired
    27. private HbaseConfig config;
    28. private static Connection connection = null;
    29. private static Admin admin = null;
    30. @PostConstruct
    31. private void init() {
    32. if (connection != null) {
    33. return;
    34. }
    35. try {
    36. connection = ConnectionFactory.createConnection(config.configuration());
    37. admin = connection.getAdmin();
    38. } catch (IOException e) {
    39. log.error("HBase create connection failed:", e);
    40. }
    41. }
    42. /**
    43. * 根据表名/列族创建表
    44. *
    45. * @param tableName 表名
    46. * @param columnFamilies 列族名
    47. * @throws IOException 异常
    48. */
    49. @Override
    50. public CommonResultVo createTable(String tableName, String... columnFamilies) throws IOException {
    51. try {
    52. TableName name = TableName.valueOf(tableName);
    53. boolean isExists = this.tableExists(tableName);
    54. if (isExists) {
    55. throw new TableExistsException(tableName + "is exists!");
    56. }
    57. TableDescriptorBuilder descriptorBuilder = TableDescriptorBuilder.newBuilder(name);
    58. List columnFamilyList = new ArrayList<>();
    59. for (String columnFamily : columnFamilies) {
    60. ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder
    61. .newBuilder(columnFamily.getBytes()).build();
    62. columnFamilyList.add(columnFamilyDescriptor);
    63. }
    64. descriptorBuilder.setColumnFamilies(columnFamilyList);
    65. TableDescriptor tableDescriptor = descriptorBuilder.build();
    66. admin.createTable(tableDescriptor);
    67. } catch (IOException e) {
    68. throw new RuntimeException(e);
    69. }
    70. return CommonResultVo.success();
    71. }
    72. /**
    73. * 保存修改
    74. */
    75. @Override
    76. public void saveOrUpdate(HBaseDto dto) throws IOException {
    77. String tableName = dto.getTableName(); //表名
    78. String rowKey = dto.getRow(); //行主键
    79. String columnFamily = dto.getColumnFamily(); //列族
    80. String columns = dto.getColumn(); //列
    81. String value = dto.getValue();
    82. Long msgtimeUtime = dto.getMsgtimeUtime();
    83. Table table = connection.getTable(TableName.valueOf(tableName));
    84. Put put = new Put(Bytes.toBytes(rowKey));
    85. put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columns), Bytes.toBytes(value));
    86. //设置时间戳
    87. put.setTimestamp(msgtimeUtime != null ? msgtimeUtime : System.currentTimeMillis());
    88. table.put(put);
    89. }
    90. /**
    91. * 删除
    92. * tableName 表名
    93. * rowKey 行主键
    94. * columnFamily 列族
    95. * column 列
    96. */
    97. @Override
    98. public CommonResultVo deleteTable(HBaseDto hBaseDto) throws IOException {
    99. boolean isExists = this.tableExists(hBaseDto.getTableName());
    100. if (!isExists) {
    101. return CommonResultVo.failed("表" + hBaseDto.getTableName() + "不存在");
    102. }
    103. String tableName = hBaseDto.getTableName();
    104. Table table = connection.getTable(TableName.valueOf(tableName));
    105. //删除列
    106. String columnFamily = hBaseDto.getColumnFamily(); //列族
    107. String row = hBaseDto.getRow(); //行主键
    108. String column = hBaseDto.getColumn(); //列
    109. if (StringUtils.isNotBlank(column) && StringUtils.isNotBlank(row)
    110. && StringUtils.isNotBlank(columnFamily) && StringUtils.isNotBlank(column)) {
    111. Delete delete = new Delete(row.getBytes());
    112. delete.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
    113. table.delete(delete);
    114. return CommonResultVo.success("列:" + column + "删除成功");
    115. }
    116. //删除列族
    117. if (StringUtils.isNotBlank(columnFamily) && StringUtils.isNotBlank(row)) {
    118. Delete delete = new Delete(row.getBytes());
    119. delete.addFamily(Bytes.toBytes(columnFamily));
    120. table.delete(delete);
    121. return CommonResultVo.success("列族:" + columnFamily + "删除成功");
    122. }
    123. //删除行主键
    124. if (StringUtils.isNotBlank(row)) {
    125. Delete delete = new Delete(row.getBytes());
    126. table.delete(delete);
    127. }
    128. //删除表
    129. if (StringUtils.isNotBlank(hBaseDto.getTableName())) {
    130. TableName name = TableName.valueOf(hBaseDto.getTableName());
    131. admin.disableTable(name);
    132. admin.deleteTable(name);
    133. return CommonResultVo.success("表:" + tableName + "删除成功");
    134. }
    135. return CommonResultVo.success();
    136. }
    137. /**
    138. * 判断表是否已经存在,这里使用间接的方式来实现
    139. *
    140. * @param tableName 表名
    141. * @return 真or假
    142. * @throws IOException 异常
    143. */
    144. @Override
    145. public boolean tableExists(String tableName) throws IOException {
    146. TableName[] tableNames = admin.listTableNames();
    147. if (tableNames != null && tableNames.length > 0) {
    148. for (int i = 0; i < tableNames.length; i++) {
    149. if (tableName.equals(tableNames[i].getNameAsString())) {
    150. return true;
    151. }
    152. }
    153. }
    154. return false;
    155. }
    156. /**
    157. * 扫描指定列在指定行键范围的值
    158. */
    159. public CommonResultVo scanRowData(HBaseDto hBaseDto) throws IOException {
    160. List> lis = new ArrayList<>();
    161. if (StringUtils.isBlank(hBaseDto.getTableName())) {
    162. CommonResultVo.failed("表名不能为null");
    163. }
    164. boolean flagStu = this.tableExists(hBaseDto.getTableName());
    165. if (!flagStu) {
    166. CommonResultVo.failed("表" + hBaseDto.getTableName() + "不存在");
    167. }
    168. Table table = connection.getTable(TableName.valueOf(hBaseDto.getTableName()));
    169. //指定起始行键和结束行键
    170. Scan scan = new Scan();
    171. //根据列族查询
    172. if (StringUtils.isNotBlank(hBaseDto.getColumnFamily())) {
    173. scan.addFamily(Bytes.toBytes(hBaseDto.getColumnFamily()));
    174. }
    175. //起始or结束行
    176. if (StringUtils.isNotBlank(hBaseDto.getStartRow()) || StringUtils.isNotBlank(hBaseDto.getStopRow())) {
    177. new Scan(Bytes.toBytes(hBaseDto.getStartRow()), Bytes.toBytes(hBaseDto.getStopRow()));
    178. }
    179. //指定行
    180. if (StringUtils.isNotBlank(hBaseDto.getRow())) {
    181. RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, new SubstringComparator(hBaseDto.getRow()));
    182. scan.setFilter(rowFilter);
    183. }
    184. //扫描指定的列
    185. if (StringUtils.isNotBlank(hBaseDto.getColumn())) {
    186. scan.addColumn(Bytes.toBytes(hBaseDto.getColumnFamily()), Bytes.toBytes(hBaseDto.getColumn()));
    187. }
    188. //时间戳精确查询
    189. if (!Objects.isNull(hBaseDto.getMsgtimeUtime())) {
    190. scan.setTimestamp(hBaseDto.getMsgtimeUtime());
    191. }
    192. //时间戳区间查询
    193. if ((!Objects.isNull(hBaseDto.getStartTime())) && (!Objects.isNull(hBaseDto.getEndTime()))) {
    194. scan.setTimeRange(hBaseDto.getStartTime(), hBaseDto.getEndTime());
    195. }
    196. ResultScanner resultScanner = table.getScanner(scan);
    197. for (Result result : resultScanner) {
    198. Map map = new HashMap<>();
    199. String flag = "0";
    200. for (Cell cell : result.rawCells()) {
    201. String rowVal = Bytes.toString(CellUtil.cloneRow(cell));
    202. if (!flag.equals(rowVal)) {
    203. map = new HashMap<>();
    204. }
    205. flag = rowVal;
    206. String columns = Bytes.toString(CellUtil.cloneQualifier(cell));
    207. String value = Bytes.toString(CellUtil.cloneValue(cell));
    208. map.put(columns, value);
    209. map.put("row", rowVal);
    210. if (flag.equals(rowVal)) {
    211. lis.remove(map);
    212. }
    213. lis.add(map);
    214. // System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) +
    215. // ", 列簇:" + Bytes.toString(CellUtil.cloneFamily(cell)) +
    216. // ", 列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
    217. // ", 值:" + Bytes.toString(CellUtil.cloneValue(cell)));
    218. }
    219. }
    220. return CommonResultVo.success(lis);
    221. }
    222. /**
    223. * 分页查询
    224. */
    225. @Override
    226. public CommonResultVo scanPageRow(HBaseDto hBaseDto) throws IOException {
    227. List> lis = new ArrayList<>();
    228. if (StringUtils.isBlank(hBaseDto.getTableName())) {
    229. CommonResultVo.failed("表名不能为null");
    230. }
    231. boolean flagStu = this.tableExists(hBaseDto.getTableName());
    232. if (!flagStu) {
    233. CommonResultVo.failed("表" + hBaseDto.getTableName() + "不存在");
    234. }
    235. Table table = connection.getTable(TableName.valueOf(hBaseDto.getTableName()));
    236. //指定起始行键和结束行键
    237. Scan scan = new Scan();
    238. //根据列族查询
    239. if (StringUtils.isNotBlank(hBaseDto.getColumnFamily())) {
    240. scan.addFamily(Bytes.toBytes(hBaseDto.getColumnFamily()));
    241. }
    242. //起始or结束行
    243. if (StringUtils.isNotBlank(hBaseDto.getStartRow()) || StringUtils.isNotBlank(hBaseDto.getStopRow())) {
    244. new Scan(Bytes.toBytes(hBaseDto.getStartRow()), Bytes.toBytes(hBaseDto.getStopRow()));
    245. }
    246. //指定行
    247. if (StringUtils.isNotBlank(hBaseDto.getRow())) {
    248. RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, new SubstringComparator(hBaseDto.getRow()));
    249. scan.setFilter(rowFilter);
    250. }
    251. //扫描指定的列
    252. if (StringUtils.isNotBlank(hBaseDto.getColumn())) {
    253. scan.addColumn(Bytes.toBytes(hBaseDto.getColumnFamily()), Bytes.toBytes(hBaseDto.getColumn()));
    254. }
    255. //时间戳精确查询
    256. if (!Objects.isNull(hBaseDto.getMsgtimeUtime())) {
    257. scan.setTimestamp(hBaseDto.getMsgtimeUtime());
    258. }
    259. //时间戳区间查询
    260. if ((!Objects.isNull(hBaseDto.getStartTime())) && (!Objects.isNull(hBaseDto.getEndTime()))) {
    261. scan.setTimeRange(hBaseDto.getStartTime(), hBaseDto.getEndTime());
    262. }
    263. //current 当前页 pageSize:条数
    264. scan.setCaching(hBaseDto.getPageSize() * hBaseDto.getCurrent() > 6000 ? 6000 : hBaseDto.getPageSize() * hBaseDto.getCurrent());
    265. ResultScanner resultScanner = table.getScanner(scan);
    266. Result[] results;
    267. int pageCount = 0;
    268. while ((results = resultScanner.next(hBaseDto.getPageSize())).length != 0) {
    269. pageCount++;
    270. if (pageCount < hBaseDto.getCurrent()) {
    271. continue;
    272. }
    273. for (Result rs : results) {
    274. //在此处解析获取数据
    275. // alls.add(rs);
    276. Map map = new HashMap<>();
    277. String flag = "0";
    278. for (Cell cell : rs.rawCells()) {
    279. String rowVal = Bytes.toString(CellUtil.cloneRow(cell));
    280. if (!flag.equals(rowVal)) {
    281. map = new HashMap<>();
    282. }
    283. flag = rowVal;
    284. String columns = Bytes.toString(CellUtil.cloneQualifier(cell));
    285. String value = Bytes.toString(CellUtil.cloneValue(cell));
    286. map.put(columns, value);
    287. map.put("row", rowVal);
    288. if (flag.equals(rowVal)) {
    289. lis.remove(map);
    290. }
    291. lis.add(map);
    292. // System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) +
    293. // ", 列簇:" + Bytes.toString(CellUtil.cloneFamily(cell)) +
    294. // ", 列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
    295. // ", 值:" + Bytes.toString(CellUtil.cloneValue(cell)));
    296. }
    297. }
    298. break;
    299. }
    300. return CommonResultVo.success(lis);
    301. }
    302. /**
    303. * 获取HBase的总行数
    304. */
    305. @Override
    306. public long countRow(HBaseDto hBaseDto) throws IOException {
    307. Table table = connection.getTable(TableName.valueOf(hBaseDto.getTableName()));
    308. Scan scan = new Scan();
    309. scan.setCaching(20);
    310. scan.addFamily(Bytes.toBytes(hBaseDto.getColumnFamily()));
    311. ResultScanner scanner = table.getScanner(scan);
    312. long rowCount = 0;
    313. Result[] results;
    314. while ((results = scanner.next(hBaseDto.getPageSize())).length != 0) {
    315. rowCount+=results.length;
    316. }
    317. return rowCount;
    318. }
    319. }

    5.5.封装的实体对象

    1. import io.swagger.annotations.ApiModelProperty;
    2. import lombok.Data;
    3. /**
    4. * @author :jerry
    5. * @date :Created in 2022/11/7 15:50
    6. * @description:结果集封装
    7. * @version: V1.1
    8. */
    9. @Data
    10. public class HBaseDto {
    11. /**
    12. * @param tableName 表名
    13. * @param rowKey 行主键
    14. * @param columnFamily 列族
    15. * @param column 列
    16. * @param value 值
    17. * @throws
    18. */
    19. @ApiModelProperty("行")
    20. private String row;
    21. @ApiModelProperty("列族")
    22. private String columnFamily;
    23. @ApiModelProperty("列")
    24. private String column;
    25. @ApiModelProperty("列值")
    26. private String value;
    27. @ApiModelProperty("表名")
    28. private String tableName;
    29. @ApiModelProperty("开始行")
    30. private String startRow;
    31. @ApiModelProperty("结束行")
    32. private String stopRow;
    33. @ApiModelProperty("报文时间")
    34. private Long msgtimeUtime;
    35. @ApiModelProperty("开始时间")
    36. private Long startTime;
    37. @ApiModelProperty("结束时间")
    38. private Long endTime;
    39. @ApiModelProperty("页码")
    40. private Integer current;
    41. @ApiModelProperty("条数")
    42. private Integer pageSize;
    43. }

    5.6.工具类:

    1. import lombok.Builder;
    2. import lombok.Getter;
    3. import lombok.Setter;
    4. /**
    5. * @author dudycoco
    6. * @version 1.0.0
    7. * @ClassName CommonResultVo.java
    8. * @Description 结果返回公共类
    9. * @createTime 2022年09月19日 00:16
    10. */
    11. @Getter
    12. @Setter
    13. @Builder
    14. public class CommonResultVo {
    15. private int code;
    16. private String message;
    17. private T data;
    18. protected CommonResultVo() {
    19. }
    20. protected CommonResultVo(int code, String message, T data) {
    21. this.code = code;
    22. this.message = message;
    23. this.data = data;
    24. }
    25. /**
    26. * 成功返回结果
    27. *
    28. */
    29. public static CommonResultVo success() {
    30. return new CommonResultVo(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    31. }
    32. /**
    33. * 成功返回结果
    34. *
    35. * @param data 获取的数据
    36. */
    37. public static CommonResultVo success(T data) {
    38. return new CommonResultVo(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    39. }
    40. /**
    41. * 成功返回结果
    42. *
    43. * @param data 获取的数据
    44. * @param message 提示信息
    45. */
    46. public static CommonResultVo success(T data, String message) {
    47. return new CommonResultVo(ResultCode.SUCCESS.getCode(), message, data);
    48. }
    49. /**
    50. * 失败返回结果
    51. * @param resultCode 错误码
    52. */
    53. public static CommonResultVo failed(ResultCode resultCode) {
    54. return new CommonResultVo(resultCode.getCode(), resultCode.getMessage(), null);
    55. }
    56. /**
    57. * 失败返回结果
    58. * @param resultCode 错误码
    59. * @param message 错误信息
    60. */
    61. public static CommonResultVo failed(ResultCode resultCode, String message) {
    62. return new CommonResultVo(resultCode.getCode(), message, null);
    63. }
    64. /**
    65. * 失败返回结果
    66. * @param message 提示信息
    67. */
    68. public static CommonResultVo failed(String message) {
    69. return new CommonResultVo(ResultCode.FAILED.getCode(), message, null);
    70. }
    71. /**
    72. * 失败返回结果
    73. */
    74. public static CommonResultVo failed() {
    75. return failed(ResultCode.FAILED);
    76. }
    77. }
    1. import lombok.Getter;
    2. import lombok.Setter;
    3. /**
    4. * @author dudycoco
    5. * @version 1.0.0
    6. * @ClassName ResultCode.java
    7. * @Description 返回值code
    8. * @createTime 2022年09月19日 00:22
    9. */
    10. public enum ResultCode {
    11. SUCCESS(0, "操作成功"),
    12. FAILED(-1, "操作失败"),
    13. VALIDATE_FAILED(404, "参数检验失败"),
    14. UNAUTHORIZED(401, "暂未登录或token已经过期"),
    15. FORBIDDEN(403, "没有相关权限");
    16. @Setter
    17. @Getter
    18. private int code;
    19. @Setter
    20. @Getter
    21. private String message;
    22. private ResultCode(int code, String message) {
    23. this.code = code;
    24. this.message = message;
    25. }
    26. }

    更多HBase知识点可以查看:Hbase 入门详解_Se7en_InfoQ写作社区

  • 相关阅读:
    第五章 Linux常用应用软件
    Gem5模拟器之scon和模拟脚本时具体在做啥?
    单例模式之DCL(Double-Checked Locking)
    【元宇宙】数字孪生,为智能社会赋能
    spring5.0源码解析 从源码角度分析 advice 的执行顺序 aop 05
    Spring boot和Vue.js实现基于oauth2授权码模式的认证 (二)
    springboot+vue企业销售人员培训报名系统java+ssm
    怎么把pdf转换成高清图片?
    eclipse创建springboot项目的三种方法
    扩展卡尔曼滤波器
  • 原文地址:https://blog.csdn.net/qq_40068304/article/details/127942658