Apache HBase APIs

贡献者:xixici

本章节提供有关使用基础 HBase API 执行操作的方法。 此方法并非详尽无遗,快速参考 User API Reference。 此处的示例不全面,仅用于说明目的。

Apache HBase 也拥有多种外部 APIs. 详见:Apache HBase External APIs

96. 例子

Example 25. 使用 Java 创建.修改和删除表

  1. package com.example.hbase.admin;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.hbase.HBaseConfiguration;
  6. import org.apache.hadoop.hbase.HColumnDescriptor;
  7. import org.apache.hadoop.hbase.HConstants;
  8. import org.apache.hadoop.hbase.HTableDescriptor;
  9. import org.apache.hadoop.hbase.TableName;
  10. import org.apache.hadoop.hbase.client.Admin;
  11. import org.apache.hadoop.hbase.client.Connection;
  12. import org.apache.hadoop.hbase.client.ConnectionFactory;
  13. import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
  14. public class Example {
  15. private static final String TABLE_NAME = "MY_TABLE_NAME_TOO";
  16. private static final String CF_DEFAULT = "DEFAULT_COLUMN_FAMILY";
  17. public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException {
  18. if (admin.tableExists(table.getTableName())) {
  19. admin.disableTable(table.getTableName());
  20. admin.deleteTable(table.getTableName());
  21. }
  22. admin.createTable(table);
  23. }
  24. public static void createSchemaTables(Configuration config) throws IOException {
  25. try (Connection connection = ConnectionFactory.createConnection(config);
  26. Admin admin = connection.getAdmin()) {
  27. HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
  28. table.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.NONE));
  29. System.out.print("Creating table. ");
  30. createOrOverwrite(admin, table);
  31. System.out.println(" Done.");
  32. }
  33. }
  34. public static void modifySchema (Configuration config) throws IOException {
  35. try (Connection connection = ConnectionFactory.createConnection(config);
  36. Admin admin = connection.getAdmin()) {
  37. TableName tableName = TableName.valueOf(TABLE_NAME);
  38. if (!admin.tableExists(tableName)) {
  39. System.out.println("Table does not exist.");
  40. System.exit(-1);
  41. }
  42. HTableDescriptor table = admin.getTableDescriptor(tableName);
  43. // 更新表格
  44. HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
  45. newColumn.setCompactionCompressionType(Algorithm.GZ);
  46. newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
  47. admin.addColumn(tableName, newColumn);
  48. // 更新列族
  49. HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT);
  50. existingColumn.setCompactionCompressionType(Algorithm.GZ);
  51. existingColumn.setMaxVersions(HConstants.ALL_VERSIONS);
  52. table.modifyFamily(existingColumn);
  53. admin.modifyTable(tableName, table);
  54. // 禁用表格
  55. admin.disableTable(tableName);
  56. // 删除列族
  57. admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8"));
  58. // 删除表格(需提前禁用)
  59. admin.deleteTable(tableName);
  60. }
  61. }
  62. public static void main(String... args) throws IOException {
  63. Configuration config = HBaseConfiguration.create();
  64. //添加必要配置文件(hbase-site.xml, core-site.xml)
  65. config.addResource(new Path(System.getenv("HBASE_CONF_DIR"), "hbase-site.xml"));
  66. config.addResource(new Path(System.getenv("HADOOP_CONF_DIR"), "core-site.xml"));
  67. createSchemaTables(config);
  68. modifySchema(config);
  69. }
  70. }