Tags

, , , , , ,

For latest updates on this post check
my new blog site

hbase-client.jar will be used to get connected to HBase using Java and this 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 Table object to perform insert operation.

String tableName = "users";
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf(tableName));

Now we need to use this table object to put the data into HBase using the following snippet

//    creating sample data that can be used to save into hbase table
String[][] people = {
    { "1", "Marcel", "Haddad", "marcel@xyz.com", "M", "26" },
    { "2", "Franklin", "Holtz", "franklin@xyz.com", "M", "24" },
    { "3", "Dwayne", "McKee", "dwayne@xyz.com", "M", "27" },
    { "4", "Rae", "Schroeder", "rae@xyz.com", "F", "31" },
    { "5", "Rosalie", "burton", "rosalie@xyz.com", "F", "25" },
    { "6", "Gabriela", "Ingram", "gabriela@xyz.com", "F", "24" } };

for (int i = 0; i < people.length; i++) {
    Put person = new Put(Bytes.toBytes(people[i][0]));
    person.addColumn(Bytes.toBytes("name"), Bytes.toBytes("first"), Bytes.toBytes(people[i][1]));
    person.addColumn(Bytes.toBytes("name"), Bytes.toBytes("last"), Bytes.toBytes(people[i][2]));
    person.addColumn(Bytes.toBytes("contact_info"), Bytes.toBytes("email"), Bytes.toBytes(people[i][3]));
    person.addColumn(Bytes.toBytes("personal_info"), Bytes.toBytes("gender"), Bytes.toBytes(people[i][4]));
    person.addColumn(Bytes.toBytes("personal_info"), Bytes.toBytes("age"), Bytes.toBytes(people[i][5]));
    table.put(person);
}

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.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

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

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

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

         String tableName = "users";

         Connection connection = null;
         Table table = null;

         try {
            connection = ConnectionFactory.createConnection(config);
            table = connection.getTable(TableName.valueOf(tableName));

            //    creating sample data that can be used to save into hbase table
            String[][] people = {
                    { "1", "Marcel", "Haddad", "marcel@xyz.com", "M", "26" },
                    { "2", "Franklin", "Holtz", "franklin@xyz.com", "M", "24" },
                    { "3", "Dwayne", "McKee", "dwayne@xyz.com", "M", "27" },
                    { "4", "Rae", "Schroeder", "rae@xyz.com", "F", "31" },
                    { "5", "Rosalie", "burton", "rosalie@xyz.com", "F", "25" },
                    { "6", "Gabriela", "Ingram", "gabriela@xyz.com", "F", "24" } };

            for (int i = 0; i < people.length; i++) {
                Put person = new Put(Bytes.toBytes(people[i][0]));
                person.addColumn(Bytes.toBytes("name"), Bytes.toBytes("first"), Bytes.toBytes(people[i][1]));
                person.addColumn(Bytes.toBytes("name"), Bytes.toBytes("last"), Bytes.toBytes(people[i][2]));
                person.addColumn(Bytes.toBytes("contact_info"), Bytes.toBytes("email"), Bytes.toBytes(people[i][3]));
                person.addColumn(Bytes.toBytes("personal_info"), Bytes.toBytes("gender"), Bytes.toBytes(people[i][4]));
                person.addColumn(Bytes.toBytes("personal_info"), Bytes.toBytes("age"), Bytes.toBytes(people[i][5]));
                table.put(person);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (table != null) {
                    table.close();
                }

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

To create tables in HBase programmatically using Java, refer my previous post here

For complete working code you can check here

Advertisements