0
votes
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 CreateTable {

    public static void main(String[] args) throws IOException {
        Configuration con = HBaseConfiguration.create();
        HBaseAdmin admin = new HBaseAdmin(con);

        HTableDescriptor ht = new HTableDescriptor("emp");

        ht.addFamily(new HColumnDescriptor("add"));
        ht.addFamily(new HColumnDescriptor("dept"));

        admin.createTable(ht);

        System.out.println("Table Created");
    }
}

I have used the above code to create Hbase table. I have downloaded all the Jars which are required and added them as an when am getting errors. But still am getting lots of errors related to zookeeper. My doubt is, do I have to Zookeeper connection using JAVA API before creating the table. And am using JDK 1.8 Please guide me the exact procedure in creating the table in Hbase using JAVA API. These are the below errors am getting with Zookeeper, but I have added the Zookeeper Jar. 17/12/11 12:44:10 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.3.1-942149, built on 05/07/2010 17:14 GMT 17/12/11 12:44:10 INFO zookeeper.ZooKeeper: Client environment:host.name=quickstart.cloudera 17/12/11 12:44:10 INFO zookeeper.ZooKeeper: Client environment:java.version=1.8.0_144 17/12/11 12:44:10 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Oracle Corporation 17/12/11 12:44:10 INFO zookeeper.ZooKeeper: Client environment:java.home=/home/cloudera/jdk1.8.0_144/jre 17/12/11 12:44:10 INFO zookeeper.ZooKeeper: Client environment:java.class.path=/home/cloudera/workspace/HbaseTbl/bin:/home/cloudera/Downloads/apache-logging-log4j.jar:/home/cloudera/Downloads/hadoop-core-0.20.2-cdh3u0.jar:/home/cloudera/Downloads/hbase-0.94.0.jar:/home/cloudera/Downloads/org-apache-commons-logging.jar:/home/cloudera/Downloads/zookeeper.jar 17/12/11 12:44:10 INFO zookeeper.ZooKeeper: Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

1
Can you please add the error message you are getting? And also the status of the zookeeper, is its up and running?Vinoth Chinnasamy
Actually am running this code in Virtual Machine, I got lot of errors about zookeeper.Karthik
And since am creating the Hbase table in the Virtual machine for testing purpose am adding the required Jars. And not using jars of the project, Please correct me if I am wrongKarthik
Added the errors in the question itself.Karthik

1 Answers

0
votes

Here is the code which worked fine for me:

        conf = HBaseConfiguration.create();
        admin = new HBaseAdmin(conf);

        TableName tableName =  TableName.valueOf("test");
        HTableDescriptor htd = new HTableDescriptor(tableName);
        HColumnDescriptor hcd = new HColumnDescriptor("data");

        htd.addFamily(hcd);
        admin.createTable(htd);

And also add the following env variables in bash_profile

export HADOOP_HOME=/Users/karan.verma/Documents/backups/h/hadoop-2.6.4
export PATH=$PATH:$HADOOP_HOME/bin
export PATH=$PATH:$HADOOP_HOME/sbin
export HADOOP_PREFIX=/Users/karan.verma/Documents/backups/h/hadoop-2.6.4
export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export YARN_HOME=$HADOOP_HOME

export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib"
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar