03 HBase高级特性

一、HBase Java API

Hbase是用Java语言编写的

  1. HBaseAdmin类

    • 管理表
    • org.apache.hadoop.hbase.client
    • 可以执行管理员任务
    • 使用Connection.getAdmin()方法获取管理员的实例

03 HBase高级特性 - 图1

  1. Descriptor类
    这个类包含一个HBase的表结构信息
    03 HBase高级特性 - 图2

03 HBase高级特性 - 图3

  1. HBaseConfiguration类
    添加HBase的配置到配置文件。这个类属于org.apache.hadoop.hbase包。
    03 HBase高级特性 - 图4

  2. HTable类
    HTable是HBase表中HBase的内部类,用于实现单个HBase表进行通信。这个类属于org.apache.hadoop.hbase.client类。

03 HBase高级特性 - 图5

03 HBase高级特性 - 图6

  1. Put类
    此类用于为单个行执行Put操作。它属于org.apache.hadoop.hbase.client包
    03 HBase高级特性 - 图7

03 HBase高级特性 - 图8

  1. Get类

此类用于对单行执行get操作。这个类属于org.apache.hadoop.hbase.client包

03 HBase高级特性 - 图9

03 HBase高级特性 - 图10

  1. Delete类
    这个类用于对单行执行删除操作。要删除整行,实例化一个Delete对象用于删除行。这个类属于org.apache.hadoop.hbase.client包
    03 HBase高级特性 - 图11

03 HBase高级特性 - 图12

  1. Result类
    这个类是用来获取Get或扫描查询的单行结果
    03 HBase高级特性 - 图13

03 HBase高级特性 - 图14

二、HBase Java API示例

  1. 创建项目

  2. 导入/usr/local/hbase/lib下所有jar包

  3. 完整代码 ```java package com.hbaseapi;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes;

public class HBaseJavaClient {

  1. /**
  2. * hbase操作必备
  3. * @return
  4. */
  5. private static Configuration getConfiguration() {
  6. // 获得HBase配置
  7. Configuration conf = HBaseConfiguration.create();
  8. // 连接HBase
  9. conf.set("hbase.rootdir", "hdfs://hadoop0:9000/hbase");
  10. // 使用eclipse时必须添加这个,否则无法定位
  11. conf.set("hbase.zookeeper.quorum", "hadoop0");
  12. return conf;
  13. }
  14. /**
  15. * 创建一张表
  16. * @param tableName
  17. * @param columnFamily
  18. * @throws IOException
  19. */
  20. public static void create(String tableName, String columnFamily) throws IOException {
  21. // 获取HBase的管理权
  22. HBaseAdmin admin = new HBaseAdmin(getConfiguration());
  23. if (admin.tableExists(tableName)) {
  24. System.out.println("table exists!");
  25. } else {
  26. // 创建Table描述器
  27. HTableDescriptor tableDesc = new HTableDescriptor(tableName);
  28. // 创建列描述器
  29. HColumnDescriptor hcd = new HColumnDescriptor(columnFamily);
  30. // 添加列族
  31. tableDesc.addFamily(hcd);
  32. // 建表
  33. admin.createTable(tableDesc);
  34. System.out.println("create table success!");
  35. }
  36. }
  37. /**
  38. * 添加数据
  39. * @param tableName
  40. * @param row
  41. * @param columnFamily
  42. * @param column
  43. * @param data
  44. * @throws IOException
  45. */
  46. public static void put(String tableName, String row, String columnFamily, String column, String data)
  47. throws IOException {
  48. // 连接表
  49. HTable table = new HTable(getConfiguration(), tableName);
  50. // 将 数据封装成一个 put对象
  51. // 参数表示行键
  52. Put p1 = new Put(Bytes.toBytes(row));
  53. // 列族/列簇 列名 值
  54. // p1.add(family, qualifier, value);
  55. p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
  56. // 添加数据
  57. table.put(p1);
  58. System.out.println("put'" + row + "'," + columnFamily + ":" + column + "','" + data + "'");
  59. }
  60. /**
  61. * 查询数据
  62. * @param tableName
  63. * @param row
  64. * @param family
  65. * @param qualifier
  66. * @throws IOException
  67. */
  68. public static void get(String tableName, String row, String family, String qualifier) throws IOException {
  69. // 连接表
  70. HTable table = new HTable(getConfiguration(), tableName);
  71. // 参数表示行键
  72. Get get = new Get(Bytes.toBytes(row));
  73. // 查询数据
  74. Result result = table.get(get);
  75. // 获取值
  76. byte[] bs = result.getValue(family.getBytes(), qualifier.getBytes());
  77. System.out.println("Get: " + new String(bs));
  78. }
  79. /**
  80. * 显示所有数据
  81. * @param tableName
  82. * @throws IOException
  83. */
  84. public static void scan(String tableName) throws IOException {
  85. // 连接表
  86. HTable table = new HTable(getConfiguration(), tableName);
  87. // 创建扫描器
  88. Scan scan = new Scan();
  89. /*
  90. * 进行扫描 扫描返回的结果不止一条
  91. */
  92. ResultScanner scanner = table.getScanner(scan);
  93. for (Result result : scanner) {
  94. byte[] value = result.getValue("apics".getBytes(), "newc".getBytes());
  95. if (value != null)
  96. System.out.println(new String(value));
  97. System.out.println("Scan: " + result);
  98. }
  99. }
  100. /**
  101. * 删除表
  102. * @param tableName
  103. * @throws IOException
  104. */
  105. public static void delete(String tableName) throws IOException {
  106. // 获取管理权
  107. HBaseAdmin admin = new HBaseAdmin(getConfiguration());
  108. // 判断表是否存在
  109. if (admin.tableExists(tableName)) {
  110. try {
  111. // 删除前需要先进行disable表
  112. admin.disableTable(tableName);
  113. // 删除表
  114. admin.deleteTable(tableName);
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. System.out.println("Delete " + tableName + " 失败");
  118. }
  119. }
  120. System.out.println("Delete " + tableName + " 成功");
  121. }
  122. /**
  123. *
  124. * @param args
  125. * @throws Exception
  126. */
  127. public static void main(String[] args) throws Exception {
  128. create("apitable", "apics");
  129. put("apitable", "NewUser", "apics", "newc", "Jerry");
  130. get("apitable", "NewUser", "apics", "newc");
  131. scan("apitable");
  132. delete("apitable");
  133. }

}

  1. 4.
  2. 运行
  3. ![](03/image-20210114161342985.png#alt=image-20210114161342985)
  4. > 可配置log4j对日志进行处理
  5. > ```properties
  6. ### log4j.properties ###
  7. ###\u8BBE\u7F6E 设置 ###
  8. log4j.rootLogger = info,stdout
  9. ###\u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u53F0 输出信息到控制台###
  10. log4j.appender.stdout = org.apache.log4j.ConsoleAppender
  11. log4j.appender.stdout.Target = System.out
  12. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
  13. log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n