Apache HBase APIs

本章提供HBase本地api接口的相关操作。这里信息不过详细,只提供了API的快速参考。本章的例子是不完整的,只是为了举例而用。

75.example

Example 40. Create, modify and delete a Table Using 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.getTableDescripto(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. // disable存在表
  55. admin.disableTable(tableName);
  56. //删除存在的列族
  57. admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8"));
  58. // Delete a table (Need to be disabled first)
  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. }