HBase 根据表名与列信息与配置信息创建表

2019-03-12 16:29:26 浏览数 (1)

代码语言:javascript复制
  private static Configuration configuration=null;
    static{
        configuration= HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort","2081");
        configuration.set("hbase.zookeeper.quorum","192.168.136.135");
        configuration.set("hbase.master","192.168.136.135:60000");
    }

    /**
     * HBase 根据表名与列信息与配置信息创建表
     * @param tableName   表名称
     * @param clolumnsInfos  列集合
     * @param configuration   org.apache.hadoop.conf.Configuration  配置对象
     * @throws Exception
     */
    public static void createTable(String tableName, List<String> clolumnsInfos,Configuration configuration) throws Exception{
        HBaseAdmin hbaseAdmin=new HBaseAdmin(configuration);
        if(hbaseAdmin.tableExists(tableName)){
            //如果表存在
            hbaseAdmin.disableTable(tableName);
            //删除表
            hbaseAdmin.deleteTable(tableName);
        }
        HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
        //添加列族
        for(String column:clolumnsInfos){
            tableDescriptor.addFamily(new HColumnDescriptor(column));
        }
        hbaseAdmin.createTable(tableDescriptor);
        hbaseAdmin.close();
    }

0 人点赞