HBase API 判断表是否存在(新版API)

1.DDL与DML的主要用途区别:

        //DDL:
        //    1.判断表是否存在
        //    2.创建表
        //    3.创建命名空间
        //    4.删除表

        //DML:
        //    5.插入数据
        //    6.查数据(get)
        //    7.查数据(scan)
        //    8.删除数据

2.配置文件和连接信息

    //    两个变量的声明:
    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();
        }
    }

3.判断表是否存在

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

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

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

4.单独创建关闭资源

    //    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();
            }
        }
    }

5.测试和关闭

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

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

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

6.返回结果为true或false

整体详情:

package test;

        import org.apache.hadoop.conf.Configuration;
        import org.apache.hadoop.hbase.HBaseConfiguration;
        import org.apache.hadoop.hbase.TableName;
        import org.apache.hadoop.hbase.client.*;

        import java.io.IOException;

public class TestAPI {

    //    两个变量的声明:
    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;
    }

    //    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("student"));

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值