Implementation:Heibaiying BigData Notes HBaseUtils CreateTable
| Knowledge Sources | |
|---|---|
| Domains | NoSQL, Big_Data |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Concrete tool for creating HBase tables with specified column families provided by the HBaseUtils utility class.
Description
The HBaseUtils.createTable() method encapsulates the complete HBase 2.x table creation workflow. It performs the following steps:
- Obtains an
Adminhandle from the shared connection. - Checks if the table already exists using
admin.tableExists(). - If the table exists, returns
falsewithout modification. - If the table does not exist, builds a
TableDescriptorwithColumnFamilyDescriptorentries for each specified column family. - Calls
admin.createTable()and returnstrueon success.
This method uses the HBase 2.x builder pattern (TableDescriptorBuilder and ColumnFamilyDescriptorBuilder) rather than the deprecated 1.x classes.
Usage
Call this method during application initialization or schema setup to ensure required tables exist. The existence check makes it safe to call repeatedly (idempotent).
Code Reference
Source Location
code/Hbase/hbase-java-api-2.x/src/main/java/com/heibaiying/HBaseUtils.java (Lines 36-54)
Signature
public static boolean createTable(String tableName, List<String> columnFamilies) throws IOException
Import
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import java.util.List;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tableName | String | Yes | The name of the HBase table to create (e.g., "users").
|
| columnFamilies | List<String> | Yes | A list of column family names to attach to the table (e.g., ["info", "metrics"]).
|
Outputs
| Name | Type | Description |
|---|---|---|
| result | boolean | Returns true if the table was created successfully; false if the table already exists.
|
Usage Examples
import java.util.Arrays;
import java.util.List;
// Define column families for the table
List<String> columnFamilies = Arrays.asList("info", "metrics");
// Create the table (returns false if it already exists)
boolean created = HBaseUtils.createTable("users", columnFamilies);
if (created) {
System.out.println("Table 'users' created successfully.");
} else {
System.out.println("Table 'users' already exists.");
}
The internal implementation builds descriptors using the 2.x builder pattern:
// Internal logic (simplified)
Admin admin = connection.getAdmin();
TableName table = TableName.valueOf(tableName);
if (admin.tableExists(table)) {
return false;
}
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(table);
for (String cf : columnFamilies) {
ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder
.newBuilder(cf.getBytes())
.build();
builder.setColumnFamily(cfd);
}
admin.createTable(builder.build());
return true;