This chapter provides information about performing operations using HBase native APIs. This information is not exhaustive, and provides a quick reference in addition to the User API Reference. The examples here are not comprehensive or complete, and should be used for purposes of illustration only.

Apache HBase also works with multiple external APIs. See Apache HBase External APIs for more information.

96. Examples

Example 25. 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.getTableDescriptor(tableName);
  43. // Update existing table
  44. HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
  45. newColumn.setCompactionCompressionType(Algorithm.GZ);
  46. newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
  47. admin.addColumn(tableName, newColumn);
  48. // Update existing column family
  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 an existing table
  55. admin.disableTable(tableName);
  56. // Delete an existing column family
  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. //Add any necessary configuration files (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. }