Hbase 创建表

Posted jomini

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hbase 创建表相关的知识,希望对你有一定的参考价值。

创建表

Pom

   <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>

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

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.6.2</version>
            <scope>compile</scope>
        </dependency>
  </dependencies>

  

Java

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

public class createTable {

    public static Connection conn;
    public static Configuration conf;

    static {

        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.222.128");  //hbase 服务地址
        conf.set("hbase.zookeeper.property.clientPort", "2181"); //端口号

        try {
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {

        String tableName = "studentTable";
        if(isTableExist(tableName))
        {
            createtable(tableName,"info");
        }
        System.out.println("end...");
    }

    public static void createtable(String tablename, String... ColumnFamilys) throws IOException {

        Admin admin = conn.getAdmin();
        /*if (admin != null) {
            try {
                //获取到数据库所有表信息
                HTableDescriptor[] allTable = admin.listTables();
                for (HTableDescriptor hTableDescriptor : allTable) {
                    System.out.println(hTableDescriptor.getNameAsString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/

        HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tablename));
        for (String family : ColumnFamilys) {
            HColumnDescriptor columnfamily = new HColumnDescriptor(family);
            table.addFamily(columnfamily);
        }
        admin.createTable(table);

        System.out.println("create sucsssss");
    }


    public static boolean isTableExist(String tableName) throws MasterNotRunningException,
            ZooKeeperConnectionException, IOException{

        HBaseAdmin admin = new HBaseAdmin(conf);
        return admin.tableExists(tableName);
    }


}

  

以上是关于Hbase 创建表的主要内容,如果未能解决你的问题,请参考以下文章

HBase基本数据操作具体解释

创建Hbase表&Hbse表映射到hive表

使用mapreduce复制hbase表

HBase:创建多个表或具有多列的单个表?

如何查找 hbase 表创建日期

HBase Shell操作HBase创建表添加数据查看表数据操作列簇删除表