How to Create A Table in HBase for Beginners

I have accumulated some knowledge and know-how about MapReduce, Hadoop, and HBase since I participated in some projects. From hence, I’ll post the know-how of HBase by period. Today, I’m going to introduce a way to make a hbase table in java.

HBase provides two ways to allow a Hbase client to connect HBase master. One is to use a instance of HBaseAdmin class. HBaseAdmin provides some methods for creating, modifying, and deleting tables and column families. Another way is to use an instance of HTable class. This class almost provides some methods to manipulate data like inserting, modifying, and deleting rows and cells.

Thus, in order to make a hbase table, we need to connect a HBase master by initializing a instance of HBaseAdmin like line 4. HBaseAdmin requires an instance of HBaseConfiguration. If necessary, you may set some configurations like line 2.

In order to describe HBase schema, we make an instances of HColumnDescriptor for each column family. In addition to column family names, HColumnDescriptor enables you to set various parameters, such as maxVersions, compression type, timeToLive, and bloomFilter. Then, we can create a HBase table by invoking createTable like line 10.

HBaseConfiguration conf = new HBaseConfiguration();
conf.set("hbase.master","localhost:60000");

HBaseAdmin hbase = new HBaseAdmin(conf);
HTableDescriptor desc = new HTableDescriptor("TEST");
HColumnDescriptor meta = new HColumnDescriptor("personal".getBytes());
HColumnDescriptor prefix = new HColumnDescriptor("account".getBytes());
desc.addFamily(meta);
desc.addFamily(prefix);
hbase.createTable(desc);

Finally, you can check your hbase table as the following commands.

c0d3h4ck@code:~/Development/hbase$ bin/hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Version: 0.20.1, r822817, Wed Oct  7 11:55:42 PDT 2009
hbase(main):001:0> list
TEST

1 row(s) in 0.0940 seconds

10 Comments on “How to Create A Table in HBase for Beginners”

  1. seong choi says:

    thanks for the post. It was really helpful 🙂

    • none says:

      what is the other way ???
      you say 2 but only mention one?

      • Hyunsik Choi says:

        The title of this post is ‘How to Create A Table in HBase’. Two ways that I mentioned are for connecting HBase. Between both ways, only one way is related to creating HBase tables. Another way is for manipulating data in HBase. So, I described one way.

  2. karaca says:

    this is really great, I was looking for a way to create a table with the java api for ages. thanks.

  3. Misty says:

    Can you please guide me how to use Hbase in Java code. I cant seem to install the Hbase server. Is there any other alternative?????

  4. Bayu says:

    can you guide me how to use mapreduce  in hbase….???  thank you

  5. Great post Hyunsik..Really helpful for newbies like me..Thanks a lot.

  6. Jagan Mohan says:

    Please tell about how to add column existing table in hbase using java code

  7. Sheriff says:

    good tutorial, but how do you set the max column version using HColumnDescriptor, I have created an object of HColumnDescriptor and called the setMaxVersions(100) but the column version is still set to three. Below is my code.

    public static void createHBaseTable(String tablename, String tables, Configuration conf) throws IOException {
    HTableDescriptor htd = new HTableDescriptor(tablename);
    for(String family : tables.split(“,”)){
    HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes(family));
    hcd.setMaxVersions(100);
    htd.addFamily(hcd);
    }
    HBaseAdmin admin = new HBaseAdmin(conf);
    if(admin.tableExists(tablename)) {
    admin.disableTable(tablename);
    admin.deleteTable(tablename);
    }

    admin.createTable(htd);
    }

    Any help will be fantastic.

  8. shweta says:

    Thanks for the post, it is really helpful to connect and create tables on local Hbase but how to connect Hbase cluster created on AWS EMR.. could you please guide me on this…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s