码迷,mamicode.com
首页 > 其他好文 > 详细

Hbase 创建表

时间:2020-02-17 12:17:48      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:family   bool   keep   name   pac   mpi   static   compile   master   

创建表

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 创建表

标签:family   bool   keep   name   pac   mpi   static   compile   master   

原文地址:https://www.cnblogs.com/Jomini/p/12320933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!