Creating a Table

Creating a Table using HBase Shell

You can create a table using the create command, here you must specify the table name and the Column Family name. The syntax to create a table in HBase shell is shown below.

create ‘<table name>’,’<column family>’ 


Given below is a sample schema of a table named emp. It has two column families: “personal data” and “professional data”. You can create this table in HBase shell as shown below.

create 'emp', 'personal data', 'professional data'

And it will give you the following output.

0 row(s) in 1.1300 seconds
=> Hbase::Table - emp
Creating a Table Using java API

You can create a table in HBase using the createTable() method of HBaseAdmin class. This class belongs to the org.apache.hadoop.hbase.client package. Given below are the steps to create a table in HBase using java API.

Step 1: Instantiate HBaseAdmin

HBaseAdmin class requires the Configuration object as a parameter, therefore initially instantiate the Configuration class and pass this instance to HBaseAdmin.

Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
Step 2: Create TableDescriptor

HTableDescriptor is a class that belongs to the org.apache.hadoop.hbase class. This class is like a container of table names and column families.

HTableDescriptor table = new HTableDescriptor(toBytes("Table name"));
HColumnDescriptor family = new HColumnDescriptor(toBytes("column family"));
Step 3: Execute through Admin

Using the createTable() method of HBaseAdmin class, you can execute the created table in Admin mode.



package com.ashok.hbase;


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;
import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.conf.Configuration;

 * @author ashok.mariyala
public class CreateTable {
   public static void main(String[] args) throws IOException {

      // Instantiating configuration class
      Configuration conf = HBaseConfiguration.create();

      // Instantiating HbaseAdmin class
      HBaseAdmin admin = new HBaseAdmin(conf);

      // Instantiating table descriptor class
      HTableDescriptor td = new HTableDescriptor(TableName.valueOf("emp"));

      // Adding column families to table descriptor
      td.addFamily(new HColumnDescriptor("personal"));
      td.addFamily(new HColumnDescriptor("professional"));

      // Execute the table through admin
      System.out.println(" Table created successfully");
Creating a Table
Scroll to top