使用最新的 hbase-client 2.1 操作 hbase

快来打我* 2021-12-03 19:57 352阅读 0赞

文章目录

  • 概述
  • 直接使用hbase-client
    • 引入依赖
    • 配置文件
    • 编写测试代码

概述

springboot 2.1 集成 hbase2.1

环境说明:
hbase:2.1.5
springboot:2.1.1.RELEASE
hadoop :2.8.5
java: 8+

hadoop环境:Hadoop 2.8.5 完全分布式HA高可用安装(二)–环境搭建
hbase环境:hbase 2.1 环境搭建–完全分布式模式 Advanced - Fully Distributed

直接使用hbase-client

引入依赖

  1. <dependency>
  2. <groupId>org.apache.hbase</groupId>
  3. <artifactId>hbase-client</artifactId>
  4. <version>2.1.5</version>
  5. </dependency>

配置文件

将 hbase-site.xml和core-site.xml两个文件拷贝到resource文件夹下:
在这里插入图片描述

由于上述两个文件中配置了虚拟机中名称,所以要在maven工程所在的机器中配置hosts,我的是Windows,修改C:\Windows\System32\drivers\etc中的hosts文件:

  1. # localhost name resolution is handled within DNS itself.
  2. # 127.0.0.1 localhost
  3. # ::1 localhost
  4. 192.168.229.128 node1
  5. 192.168.229.129 node2
  6. 192.168.229.130 node3
  7. 192.168.229.131 node4

编写测试代码

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.Path;
  3. import org.apache.hadoop.hbase.*;
  4. import org.apache.hadoop.hbase.client.*;
  5. import org.apache.hadoop.hbase.io.compress.Compression;
  6. import org.apache.hadoop.hbase.util.Bytes;
  7. import java.io.IOException;
  8. import java.net.URISyntaxException;
  9. public class HelloHBase {
  10. public static void main(String[] args) throws URISyntaxException {
  11. // 加载HBase的配置
  12. Configuration configuration = HBaseConfiguration.create();
  13. // 读取配置文件
  14. configuration.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
  15. configuration.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
  16. try (// 创建一个HBase连接
  17. Connection connection = ConnectionFactory.createConnection(configuration);
  18. // 获得执行操作的管理接口
  19. Admin admin = connection.getAdmin();) {
  20. // 新建一个表名为mytable的表
  21. TableName tableName = TableName.valueOf("mytable");
  22. HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
  23. // 新建一个列族名为mycf的列族
  24. HColumnDescriptor mycf = new HColumnDescriptor("mycf");
  25. // 将列族添加到表中
  26. tableDescriptor.addFamily(mycf);
  27. // 执行建表操作
  28. createOrOverwrite(admin, tableDescriptor);
  29. // 设置列族的压缩方式为GZ
  30. mycf.setCompactionCompressionType(Compression.Algorithm.GZ);
  31. // 设置最大版本数量(ALL_VERSIONS实际上就是Integer.MAX_VALUE)
  32. mycf.setMaxVersions(HConstants.ALL_VERSIONS);
  33. // 列族更新到表中
  34. tableDescriptor.modifyFamily(mycf);
  35. // 执行更新操作
  36. admin.modifyTable(tableName, tableDescriptor);
  37. // 新增一个列族
  38. HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("newcf");
  39. hColumnDescriptor.setCompactionCompressionType(Compression.Algorithm.GZ);
  40. hColumnDescriptor.setMaxVersions(HConstants.ALL_VERSIONS);
  41. // 执行新增操作
  42. admin.addColumnFamily(tableName, hColumnDescriptor);
  43. // 获取表对象
  44. Table table = connection.getTable(tableName);
  45. // 创建一个put请求,用于添加数据或者更新数据
  46. Put put = new Put(Bytes.toBytes("row1"));
  47. put.addColumn(Bytes.toBytes("mycf"), Bytes.toBytes("name"), Bytes.toBytes("jack"));
  48. table.put(put);
  49. // 创建一个append请求,用于在数据后面添加内容
  50. Append append = new Append(Bytes.toBytes("row1"));
  51. append.add(Bytes.toBytes("mycf"), Bytes.toBytes("name"), Bytes.toBytes("son"));
  52. table.append(append);
  53. // 创建一个long类型的列
  54. Put put1 = new Put(Bytes.toBytes("row2"));
  55. put1.addColumn(Bytes.toBytes("mycf"), Bytes.toBytes("age"), Bytes.toBytes(6L));
  56. table.put(put1);
  57. // 创建一个增值请求,将值增加10L
  58. Increment increment = new Increment(Bytes.toBytes("row2"));
  59. increment.addColumn(Bytes.toBytes("mycf"), Bytes.toBytes("age"), 10L);
  60. table.increment(increment);
  61. // 创建一个查询请求,查询一行数据
  62. Get get = new Get(Bytes.toBytes("row1"));
  63. // 由于HBase的一行可能非常大,所以限定要取出的列族
  64. get.addFamily(Bytes.toBytes("mycf"));
  65. // 创建一个结果请求
  66. Result result = table.get(get);
  67. // 从查询结果中取出name列,然后打印(这里默认取最新版本的值,如果要取其他版本要使用Cell对象)
  68. byte[] name = result.getValue(Bytes.toBytes("mycf"), Bytes.toBytes("name"));
  69. System.out.println(Bytes.toString(name));
  70. // 创建一个查询请求,查询一行数据
  71. get = new Get(Bytes.toBytes("row2"));
  72. // 由于HBase的一行可能非常大,所以限定要取出的列族
  73. get.addFamily(Bytes.toBytes("mycf"));
  74. // 创建一个结果请求
  75. result = table.get(get);
  76. // 从查询结果中取出name列,然后打印(这里默认取最新版本的值,如果要取其他版本要使用Cell对象)
  77. byte[] age = result.getValue(Bytes.toBytes("mycf"), Bytes.toBytes("age"));
  78. System.out.println(Bytes.toLong(age));//注意格式
  79. // 创建一个扫描请求,查询多行数据
  80. Scan scan = new Scan(Bytes.toBytes("row1"));
  81. // 设置扫描器的缓存数量,遍历数据时不用发多次请求,默认100,适当的缓存可以提高性能
  82. scan.setCaching(150);
  83. // 创建扫描结果,这个时候不会真正从HBase查询数据,下面的遍历才是去查询
  84. ResultScanner resultScanner = table.getScanner(scan);
  85. for (Result r : resultScanner) {
  86. String data = Bytes.toString(r.getValue(Bytes.toBytes("mycf"), Bytes.toBytes("name")));
  87. System.out.println(data);
  88. }
  89. // 使用完毕关闭
  90. resultScanner.close();
  91. // 创建一个删除请求
  92. Delete delete = new Delete(Bytes.toBytes("row2"));
  93. // 可以自定义一些筛选条件
  94. delete.addFamily(Bytes.toBytes("mycf"));
  95. table.delete(delete);
  96. // 停用表
  97. admin.disableTable(tableName);
  98. // 删除列族
  99. admin.deleteColumnFamily(tableName, "mycf".getBytes());
  100. // 删除表
  101. admin.deleteTable(tableName);
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. System.out.println("ok");
  106. }
  107. public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException {
  108. // 获取table名
  109. TableName tableName = table.getTableName();
  110. // 判断table是否存在,如果存在则先停用并删除
  111. if (admin.tableExists(tableName)) {
  112. // 停用表
  113. admin.disableTable(tableName);
  114. // 删除表
  115. admin.deleteTable(tableName);
  116. }
  117. // 创建表
  118. admin.createTable(table);
  119. }
  120. }

上面展示了一个完整的创建,插入,查询,删除的过程。读者可以debug并配合hbase shell 来查看数据变化。

发表评论

表情:
评论列表 (有 0 条评论,352人围观)

还没有评论,来说两句吧...

相关阅读