使用Java API在Hbase中创建表

时间:2017-12-12 03:51:05

标签: java hbase

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");
    }
}

我已经使用上面的代码来创建Hbase表。我已经下载了所有需要的Jars,并将它们添加为错误时间。但是仍然会收到很多与zookeeper相关的错误。 我的疑问是,在创建表之前,我是否需要使用JAVA API连接Zookeeper。我正在使用JDK 1.8 请引导我使用JAVA API在Hbase中创建表的确切步骤。以下是Zookeeper的错误,但我添加了Zookeeper Jar。 17/12/11 12:44:10 INFO zookeeper.ZooKeeper:客户环境:zookeeper.version = 3.3.1-942149,建于05/07/2010 17:14 GMT 17/12/11 12:44:10 INFO zookeeper.ZooKeeper:客户端环境:host.name = quickstart.cloudera 17/12/11 12:44:10 INFO zookeeper.ZooKeeper:客户端环境:java.version = 1.8.0_144 17/12/11 12:44:10 INFO zookeeper.ZooKeeper:客户端环境:java.vendor = Oracle Corporation 17/12/11 12:44:10 INFO zookeeper.ZooKeeper:客户端环境:java.home = / home / cloudera / jdk1.8.0_144 / jre 17/12/11 12:44:10 INFO zookeeper.ZooKeeper:客户端环境: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:客户端环境:java.library.path = / usr / java / packages / lib / amd64:/ usr / lib64:/ lib64:/ lib:/ usr / lib

1 个答案:

答案 0 :(得分:0)

以下是适合我的代码:

        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);

并在bash_profile

中添加以下env变量
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