HBase Create Table with Java API & Shell Example

โšก Smart Summary

HBase stores data in tables, and you can create and handle those tables two ways: through the Java API with HTableDescriptor and HBaseAdmin, or directly in the HBase shell using the create command.

  • โ˜• Java API: Create tables in code with HTableDescriptor, HColumnDescriptor, and HBaseAdmin.
  • ๐Ÿš Shell create: The create command builds a table from a name and one or more column families.
  • ๐Ÿงฌ Column families: Every table needs at least one column family, defined when the table is created.
  • ๐Ÿ”ง Alter tables: The alter command adds column families, sets VERSIONS, or changes table-scope attributes.
  • ๐Ÿ—‘๏ธ Table lifecycle: Disable a table before you alter or drop it, then enable it again to resume writes.
  • ๐Ÿค– AI assist: AI assistants draft create, alter, and drop commands and review column-family design.

HBase create table with Java API and shell command example

How to Create Table in HBase with Java API

In HBase, tables can be created in two ways: through the Java API and through the HBase shell. This section covers the Java API, which lets an application create tables and load data programmatically as part of a larger job.

Through Java API, we can create tables in HBase and also load data into tables using Java coding. The two things we set up here are:

  • Establishing a connection with HBase through Java API.
  • Using Eclipse for Java coding, debugging, and testing.

Following are the steps to create tables in HBase through Java API.

Step 1) Create a Java Project in Eclipse

In this step, we are going to create a Java project in Eclipse for the HBase connection, named “HbaseConnection”.

The New Java Project dialog below shows the “HbaseConnection” project being set up:

Eclipse New Java Project dialog creating the HbaseConnection project with a JavaSE-1.7 environment

If we observe the screenshot above:

  • Give the project name in this box. In our case, the project name is “HbaseConnection”.
  • Check this box for the default location to be saved. Here /home/hduser/work/HbaseConnection is the path.
  • Check the box for the Java environment here. In this case JavaSE-1.7 is the Java edition.
  • Choose the option for where you want to save the file. In our case, we selected the second option, “Create a separate folder for sources and class files”.
  • Click on the Finish button, which creates the “HbaseConnection” project in Eclipse and opens the Eclipse home page.

Step 2) Configure the Build Path in Eclipse

On the Eclipse home page, follow these steps to open the build path configuration:

Right click on project -> Select Build Path -> Configure build path

The screenshot below shows the right-click menu used to open the build path configuration:

Eclipse project right-click menu selecting Build Path then Configure Build Path

From the above screenshot:

  • Right-click on a project.
  • Select Build Path.
  • Select Configure Build Path.

After clicking Configure Build Path, it opens another window, shown in the screenshot below. The Libraries tab is where the HBase and Hadoop JAR files are added to the project:

Eclipse Java Build Path Libraries tab adding external HBase and Hadoop JAR files

In this step, we add the relevant HBase JARs into the Java project as shown in the screenshot.

  • Important JARs to be added: hbase-0.94.8.jar and hadoop-core-1.1.2.jar.
  • Come to the Libraries tab.
  • Press the option Add External JARs.
  • Select the required important JARs.
  • Press the Finish button to add these files to the “src” of the Java project under Libraries.

After the JARs are added, they appear under the project “src” location as shown below:

Eclipse project explorer showing the added HBase and Hadoop JAR files under the project source

All the JAR files that fall under the project are now ready for usage with the Hadoop ecosystem.

Step 3) Establish the HBase Connection

In this step, using HBaseConnection.java, the HBase connection is established through Java coding.

On the Eclipse top menu, execute a Java program as shown below: Run -> Run As -> Java Application. The menu below shows the program being launched:

Eclipse Run As Java Application menu launching the HBaseConnection program

  • Select Run.
  • Select Run As Java Application.
  • This code establishes a connection with HBase through Java API.
  • After running this code, the “guru99” table is created in HBase with two column families named “education” and “projects”. At present, only the empty schema is created in HBase.

The code excerpt below highlights the HTableDescriptor and addFamily calls that build the table schema:

Java code using HTableDescriptor and addFamily to define the guru99 table with education and projects column families

From the screenshot above, we are performing the following functions:

  • Using HTableDescriptor, we can create the “guru99” table in HBase.
  • Using the addFamily method, we add “education” and “projects” as column families to the “guru99” table.

The code below is going to establish a connection with HBase and create the “guru99” table with two column families. Place this code under the HBaseConnection.java document:

// Place this code inside Hbase connection
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
Import org.apache.hadoop.hbase.client.HBaseAdmin;

public class HBaseConnection
{
    public static void main(String[] args) throws IOException
    {
	HBaseConfigurationhc = new HBaseConfiguration(new Configuration());
	HTableDescriptorht = new HTableDescriptor("guru99");

	ht.addFamily( new HColumnDescriptor("education"));
	ht.addFamily( new HColumnDescriptor("projects"));
	System.out.println( "connecting" );
	HBaseAdminhba = new HBaseAdmin( hc );

	System.out.println( "Creating Table" );
	hba.createTable( ht );
	System.out.println("Done......");
    }
}

This is the required code you have to place in HBaseConnection.java before you run the Java program.

After running this program, it establishes a connection with HBase and, in turn, creates a table with column names.

  • The table name is “guru99”.
  • Column families are “education” and “projects”.

Step 4) Check the Created Table in HBase

We can check whether the “guru99” table is created with two column families in HBase by using HBase shell mode with the “list” command. The “list” command gives information about all the tables created in HBase.

Running the list command in the HBase shell confirms the new table, as shown below:

HBase shell list command output showing the guru99 table was created

  • Code checking in the HBase shell by executing the “list” command.
  • If we run the “list” command, it displays the table created in HBase. In our case, we can see the “guru99” table is created.

HBase Create Table with Shell

Besides the Java API, you can create a table directly from the HBase shell, which is the fastest way to set up a table for quick tests. The syntax to create a table in HBase using the shell is:

Syntax: create <tablename>, <columnfamilyname>

Example:-

hbase(main):001:0> create 'education' ,'guru99'
0 rows(s) in 0.312 seconds
=>Hbase::Table โ€“ education

The above example explains how to create a table in HBase with the specified name given according to the specifications for each column family. In addition to this, we can also pass some table-scope attributes into it.

create 'guru99', {NAME=>'Edu', VERSIONS=>213423443}

Here the {NAME=>’Edu’, VERSIONS=>…} block sets a column family named “Edu” and the number of cell versions it keeps. The large number is only illustrative; in practice, VERSIONS is a small value such as 3 or 5.

List, Describe, and Verify HBase Tables

Creating a table is only the first part of handling tables in HBase. Once the “guru99” table exists, a few general commands let you inspect it and confirm its state before you read, write, or modify data. You can run all of them from the HBase shell.

The list command returns every user table in HBase:

hbase> list

The describe command shows a table’s structure, including its column families and whether it is currently enabled:

hbase> describe 'guru99'

The exists command checks whether a table is present, and is_enabled reports whether the table can accept writes:

hbase> exists 'guru99'
hbase> is_enabled 'guru99'

These read-only checks are safe to run at any time. They are the quickest way to confirm that a table was created correctly and to review its column families before loading data.

Alter an HBase Table

An HBase table schema is not fixed after creation. The alter command changes a table’s column families and table-scope attributes without recreating it. Depending on your HBase version, you may need to disable the table first, then run alter and enable it again.

To add a new column family to the “guru99” table:

hbase> alter 'guru99', {NAME => 'contact'}

To set how many versions a column family keeps:

hbase> alter 'guru99', NAME => 'education', VERSIONS => 5

To delete a column family from the table:

hbase> alter 'guru99', {NAME => 'contact', METHOD => 'delete'}

You can also change table-scope attributes. For example, mark the table read-only, or set the maximum region file size to 128 MB:

hbase> alter 'guru99', READONLY
hbase> alter 'guru99', MAX_FILESIZE => '134217728'

Because alter rewrites the schema, plan changes carefully on a production table, and confirm the result afterward with describe.

Disable, Enable, and Drop HBase Tables

To remove a table or apply certain changes, HBase requires you to disable it first. Disabling takes the table offline so no client can read or write to it, which makes structural changes safe.

Disable a table, and check its state with is_disabled:

hbase> disable 'guru99'
hbase> is_disabled 'guru99'

Enable the table again to resume reads and writes:

hbase> enable 'guru99'

A table must be disabled before it can be dropped. Dropping deletes the table and all of its data permanently, so use it with care:

hbase> disable 'guru99'
hbase> drop 'guru99'

To remove several tables at once, drop_all deletes every table whose name matches a regular expression. Together, create, alter, disable, enable, and drop cover the full life cycle of handling data in HBase tables.

FAQs

Best practice keeps the number of column families low, ideally one to three. HBase flushes and compacts all families of a region together, so extra families spread data across more files and slow reads and writes. Group related columns into a single family when possible.

No. At create time you only define column families. Column qualifiers are dynamic and can be added on any row when you write data. This schema-flexible design lets each row hold different columns without altering the table first.

Use truncate ‘guru99’, which disables the table, drops it, and recreates it with the same column families. The schema stays intact while every row is removed. This is faster and cleaner than deleting rows one at a time.

The example uses HBaseAdmin and HTableDescriptor, both deprecated since HBase 1.0. Modern code obtains an Admin object from a Connection through ConnectionFactory and builds the schema with TableDescriptorBuilder and ColumnFamilyDescriptorBuilder. The table-creation logic stays the same.

VERSIONS sets how many timestamped versions of each cell a column family keeps. The default is 1. Requesting older versions with a get or scan returns them until the retained count is exceeded, after which compaction removes the oldest cells.

HBase suits very large, sparse datasets that need fast random reads and writes on top of Hadoop and HDFS. Typical uses include time-series data, messaging, and real-time analytics where tables grow to billions of rows across many nodes.

AI coding assistants and large language models draft create, alter, and drop commands from plain-language prompts, explain column-family design, and flag risky operations such as dropping a table. Machine learning can also analyze access patterns to recommend better row-key and family layouts, though an engineer should verify each suggestion.

Yes. GitHub Copilot can generate HBase shell commands and Java client code for creating, altering, and dropping tables from a short comment. Review its output for the correct table name, column families, and the current Admin API before running it.

Summarize this post with: