HBase API 创建表

1.配置文件和连接信息

    //    两个变量的声明:
    private static Connection connection = null;
    private  static Admin admin = null;

    static{
        try {
//            1.获取配置文件信息
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");

//            2.创建连接对象
            connection = ConnectionFactory.createConnection(configuration);

//            3.创建admin对象
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2.判断表是否存在

    public static boolean isTableExist(String tableName) throws IOException {

//        5.判断表是否存在
        boolean exists = admin.tableExists(TableName.valueOf(tableName));

//        6.返回结果
        return exists;
    }

3.创建表

    public static void creaTable(String tableName,String... cfs) throws IOException {
//        10.1  是否存在列族信息
        if(cfs.length <= 0){
            System.out.println("请设置列族信息!");
            return;
        }

//        10.2  判断表是否存在
        if(isTableExist(tableName)){
            System.out.println(tableName + "表已存在!");
            return;
        }

//        10.3  创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

//        10.4  循环添加列族信息
        for (String cf : cfs) {
//            10.5  创建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//            10.6 添加具体的列族信息
            hTableDescriptor.addFamily(hColumnDescriptor);
        }

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


    }

4.单独创建关闭资源

    public static void close(){
        if(admin != null){
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(connection!=null){
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

6.测试和关闭

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

//        8.测试表是否存在
        System.out.println(isTableExist("stu1"));

//        11.创建表测试
        creaTable("stu1","info1","info2");
//        12.创建完测试表是否存在
        System.out.println(isTableExist("stu1"));

//        9.关闭资源的调用
        close();
    }

整体详情:

package test;

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;
import org.apache.hadoop.hbase.client.HTable;

import java.io.IOException;

public class CreateTable {


    //    两个变量的声明:
    private static Connection connection = null;
    private  static Admin admin = null;

    static{
        try {
//            1.获取配置文件信息
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");

//            2.创建连接对象
            connection = ConnectionFactory.createConnection(configuration);

//            3.创建admin对象
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //    4.判断表是否存在
    public static boolean isTableExist(String tableName) throws IOException {

//        5.判断表是否存在
        boolean exists = admin.tableExists(TableName.valueOf(tableName));

//        6.返回结果
        return exists;
    }

//    10.创建表
    public static void creaTable(String tableName,String... cfs) throws IOException {
//        10.1  是否存在列族信息
        if(cfs.length <= 0){
            System.out.println("请设置列族信息!");
            return;
        }

//        10.2  判断表是否存在
        if(isTableExist(tableName)){
            System.out.println(tableName + "表已存在!");
            return;
        }

//        10.3  创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

//        10.4  循环添加列族信息
        for (String cf : cfs) {
//            10.5  创建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//            10.6 添加具体的列族信息
            hTableDescriptor.addFamily(hColumnDescriptor);
        }

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


    }



    //    7.关闭资源
    public static void close(){
        if(admin != null){
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(connection!=null){
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws IOException {

//        8.测试表是否存在
        System.out.println(isTableExist("stu1"));

//        11.创建表测试
        creaTable("stu1","info1","info2");
//        12.创建完测试表是否存在
        System.out.println(isTableExist("stu1"));

//        9.关闭资源的调用
        close();
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值