Updated on 2025-07-25 GMT+08:00

Creating an HBase Cold and Hot Data Separation Table

Function Description

HBase allows you to create a table using the createTable method of the org.apache.hadoop.hbase.client.Admin object. You need to specify a table name, a column family name, and the time boundary.

You can create a table by using either of the following methods, but the latter one is recommended:

  • Quickly create a table. A newly created table contains only one region, which will be automatically split into multiple new regions as data increases.
  • Create a table using pre-assigned regions. You can pre-assign multiple regions before creating a table. This mode accelerates data write at the beginning of massive data write.

The table name and column family name of a table consist of letters, digits, and underscores (_) but cannot contain any special characters.

Sample Code

public void testCreateTable() {
  LOG.info("Entering testCreateTable.");

  // Specify the table descriptor.
  HTableDescriptor htd = new HTableDescriptor(tableName); // (1)

  // Set the column family name to info.
  HColumnDescriptor hcd = new HColumnDescriptor("info"); // (2)
  
  // Set hot and cold data boundary
  hcd.setValue(HColumnDescriptor.COLD_BOUNDARY, "86400");
  htd.addFamily(hcd); // (3)

  Admin admin = null;
  try {
    // Instantiate an Admin object.
    admin = conn.getAdmin(); // (4)
    if (!admin.tableExists(tableName)) {
      LOG.info("Creating table...");
      admin.createTable(htd); // Note [1] (5)
      LOG.info(admin.getClusterStatus());
      LOG.info(admin.listNamespaceDescriptors());
      LOG.info("Table created successfully.");
    } else {
      LOG.warn("table already exists");
    }
  } catch (IOException e) {
    LOG.error("Create table failed.", e);
  } finally {
    if (admin != null) {
      try {
        // Close the Admin object.
        admin.close();
      } catch (IOException e) {
        LOG.error("Failed to close admin ", e);
      }
    }
  }
  LOG.info("Exiting testCreateTable.");
}
  • Description of code numbers
    • (1) Create a table descriptor.
    • (2) Create a column family descriptor.
    • (3) Add the column family descriptor to the table descriptor.
    • (4) Obtain the Admin object. You use the Admin object to create a table and a column family, check whether the table exists, modify the table structure and column family structure, and delete the table.
    • (5) Invoke the Admin object to create a table.
  • Precautions
    • For details about how to set other attributes of the table and column family, see "Developing HBase Applications".

      Note [1] refers to admin.createTable(htd); // Note [1] (5) in the sample code.