HBase创建表的Java代码

HBase 1.x和2.x版本的建表的java代码如下:

	/**
     * @Description 创建HBase表
     * @Params []
     * @Return
     * @Author zhangbinu
     */
    @PostConstruct
    public synchronized boolean createHBaseTable() throws IOException {
        boolean isSuccess = false;
        String tableName = "您想要创建的表名";
        TableName table = TableName.valueOf(tableName);
        Admin admin = HBaseUtil.connection.getAdmin();
        if (!admin.tableExists(table)) {
            log.info("show: HBase中不存在表[" + table + "],开始创建...");
            //HBase 1.x版本建表
            HTableDescriptor hbaseTable = new HTableDescriptor(table);
            hbaseTable.addFamily(new HColumnDescriptor("info").setTimeToLive(180 * 24 * 60 * 60));
            admin.createTable(hbaseTable);
            //HBase 2.x版本建表
            //TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(table);
            //tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).setTimeToLive(180 * 24 * 60 * 60).build());
            //admin.createTable(tableDescriptor.build());
            log.info("show: HBase创建表[" + table + "]成功。");
            isSuccess = true;
        } else {
            log.info("show: HBase中已存在表[" + table + "],不需要再创建。");
        }
        return isSuccess;
    }

HBaseUtil连接工具类如下:

/**
 * Created by zhangbn on 2019/11/20.
 */
public class HBaseUtil {

    static Logger log = LoggerFactory.getLogger(HBaseUtil.class);

    /**
     * 连接池对象
     */
    public static Connection connection;
    private static final String ZK_QUORUM = "hbase.zookeeper.quorum";
    private static final String ZK_CLIENT_PORT = "hbase.zookeeper.property.clientPort";
    private static final String ZK_POS = "hadoop01,hadoop02,hadoop03";
    private final static String ZK_PORT_VALUE = "2181";

    /**
     * @Description 静态构造
     * @Params
     * @Return
     * @Author zhangbn
     */
    static {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.rootdir", "hdfs://nncluster/user/hbase");
        configuration.set(ZK_QUORUM, ZK_POS);
        configuration.set(ZK_CLIENT_PORT, ZK_PORT_VALUE);
        configuration.set("hbase.client.ipc.pool.type", "RoundRobin");
        configuration.set("hbase.client.ipc.pool.size", "100");
        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            log.error(e.toString());
        }
    }

}

版权声明:本文为zhangbinu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>