Tags

, , , , , ,

For latest updates on this post check
my new blog site

To create table into HBase by using Java we will use hbase-client.jar which is available in maven repository. The following dependency can be added in our pom.xml

<dependency>
     <groupId>org.apache.hbase</groupId>
     <artifactId>hbase-client<artifactId>
     <version>1.1.0.1</version>
<dependency>

Once we have added the dependency we need to create Configuration object specifying core-site.xml and hbase-site.xml as resources.

Configuration config = HBaseConfiguration.create();
config.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));
config.addResource(new Path("/etc/hadoop/conf/core-site.xml"));

Instead we can set hbase.zookeeper.quorum and hbase.zookeeper.property.clientPort values to Configuration object. These values can be found in hbase-site.xml

 Configuration config = HBaseConfiguration.create();
 config.set("hbase.zookeeper.quorum", "127.0.0.1");
 config.set("hbase.zookeeper.property.clientPort", "2181");

Once we have Configuration object we can create Connection to HBase and from Connection object we obtain Admin object to perform create table operation.

Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();

Once we have Admin object, we can create the table by using the following code. Here we are creating users table

String tableName = "users";

if (!admin.isTableAvailable(TableName.valueOf(tableName))) {
    HTableDescriptor hbaseTable = new HTableDescriptor(TableName.valueOf(tableName));
    hbaseTable.addFamily(new HColumnDescriptor("name"));
    hbaseTable.addFamily(new HColumnDescriptor("contact_info"));
    hbaseTable.addFamily(new HColumnDescriptor("personal_info"));
    admin.createTable(hbaseTable);
}

This will validate whether there exists a table with name “users”, if not then we are creating a new table with column families name, contact_info and personal_info.

The complete program will look like below

package com.wordpress.khodeprasad;

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

/**
* @author Prasad Khode
*
*/
public class CreateTable {

    public static void main(String[] args) {
        CreateTable object = new CreateTable();
        object.createTable();
    }

    public void createTable() {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", "127.0.0.1");
        config.set("hbase.zookeeper.property.clientPort", "2181");

        Connection connection = null;
        Admin admin = null;

        try {
            connection = ConnectionFactory.createConnection(config);
            admin = connection.getAdmin();

            String tableName = "users";

            if (!admin.isTableAvailable(TableName.valueOf(tableName))) {
                HTableDescriptor hbaseTable = new HTableDescriptor(TableName.valueOf(tableName));
                hbaseTable.addFamily(new HColumnDescriptor("name"));
                hbaseTable.addFamily(new HColumnDescriptor("contact_info"));
                hbaseTable.addFamily(new HColumnDescriptor("personal_info"));
                admin.createTable(hbaseTable);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (admin != null) {
                    admin.close();
                }

                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}

For complete working code you can check here

Advertisements