Apache HBase JavaAPI 创建表myuser

Apache HBase JavaAPI 创建表myuser
package com.czxy.demo01;

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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

public class Test01 {
    /**
     * 创建一个HBase 数据表
     * @param args
     */
    public static void main(String[] args)throws Exception {
        /**
         * 数据库操作流程
         * 1、连接数据库
         * 2、获取数据表
         * 3、对表进行操作
         * 4、关闭连接
         */
        
        Configuration conf = HBaseConfiguration.create();

        //设置连接ZooKeeper
        conf.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);

        Admin admin = connection.getAdmin();
        //设置表名称
        TableName tableName = TableName.valueOf("myuser");
        //"myuser"是字符串

        //设置表的列族
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

        HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor("f1");
        HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor("f2");
        HColumnDescriptor hColumnDescriptor3 = new HColumnDescriptor("f3");
        //为表添加列族
        hTableDescriptor.addFamily(hColumnDescriptor1);
        hTableDescriptor.addFamily(hColumnDescriptor2);
        hTableDescriptor.addFamily(hColumnDescriptor3);

        //创建表
        admin.createTable(hTableDescriptor);

        //关闭连接
        admin.close();
        connection.close();

    }

}

注意linux中的hosts文件中的配置需要和windows的hosts文件内容一致,即配置对应ip地址映射。