• HBASE基础(4):语法(2) API (1) DDL


    1 环境准备

    新建项目后在pom.xml中添加依赖

    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>2.0.5</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>2.0.5</version>
    </dependency>

    2 DDL

    创建HBase_DDL

    2.1 判断表是否存在

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.NamespaceDescriptor;
    import org.apache.hadoop.hbase.NamespaceExistException;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBase_DDL {
    
        //TODO 判断表是否存在
        public static boolean isTableExist(String tableName) throws IOException {
    
            //1.创建配置信息并配置
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    
            //2.获取与HBase的连接
            Connection connection = ConnectionFactory.createConnection(configuration);
    
            //3.获取DDL操作对象
            Admin admin = connection.getAdmin();
    
            //4.判断表是否存在操作
            boolean exists = admin.tableExists(TableName.valueOf(tableName));
    
            //5.关闭连接
            admin.close();
            connection.close();
    
            //6.返回结果
            return exists;
        }
    
    }

    2.2 创建表

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.NamespaceDescriptor;
    import org.apache.hadoop.hbase.NamespaceExistException;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBase_DDL {
    
        //TODO 创建表
        public static void createTable(String tableName, String... cfs) throws IOException {
    
            //1.判断是否存在列族信息
            if (cfs.length <= 0) {
                System.out.println("请设置列族信息!");
                return;
            }
    
            //2.判断表是否存在
            if (isTableExist(tableName)) {
                System.out.println("需要创建的表已存在!");
                return;
            }
    
            //3.创建配置信息并配置
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    
            //4.获取与HBase的连接
            Connection connection = ConnectionFactory.createConnection(configuration);
    
            //5.获取DDL操作对象
            Admin admin = connection.getAdmin();
    
            //6.创建表描述器构造器
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
    
            //7.循环添加列族信息
            for (String cf : cfs) {
                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
                tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
            }
    
            //8.执行创建表的操作
            admin.createTable(tableDescriptorBuilder.build());
    
            //9.关闭资源
            admin.close();
            connection.close();
        }
    
    }

    2.3 删除表

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.NamespaceDescriptor;
    import org.apache.hadoop.hbase.NamespaceExistException;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBase_DDL {
    
        //TODO 删除表
        public static void dropTable(String tableName) throws IOException {
    
            //1.判断表是否存在
            if (!isTableExist(tableName)) {
                System.out.println("需要删除的表不存在!");
                return;
            }
    
            //2.创建配置信息并配置
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    
            //3.获取与HBase的连接
            Connection connection = ConnectionFactory.createConnection(configuration);
    
            //4.获取DDL操作对象
            Admin admin = connection.getAdmin();
    
            //5.使表下线
            TableName name = TableName.valueOf(tableName);
            admin.disableTable(name);
    
            //6.执行删除表操作
            admin.deleteTable(name);
    
            //7.关闭资源
            admin.close();
            connection.close();
        }
    
    }

    2.4 创建命名空间

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.NamespaceDescriptor;
    import org.apache.hadoop.hbase.NamespaceExistException;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBase_DDL {
    
        //TODO 创建命名空间
        public static void createNameSpace(String ns) throws IOException {
    
            //1.创建配置信息并配置
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    
            //2.获取与HBase的连接
            Connection connection = ConnectionFactory.createConnection(configuration);
    
            //3.获取DDL操作对象
            Admin admin = connection.getAdmin();
    
            //4.创建命名空间描述器
            NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
    
            //5.执行创建命名空间操作
            try {
                admin.createNamespace(namespaceDescriptor);
            } catch (NamespaceExistException e) {
                System.out.println("命名空间已存在!");
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            //6.关闭连接
            admin.close();
            connection.close();
    
        }
    }

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/13401205.html

  • 相关阅读:
    信息安全系统设计基础第八周期中复习总结
    layui下各种富文本的冲突情况
    TP3.2+find_set_in 以及 find_set_in和like的区别
    tp5+linux+apache php7.1.30环境下,上传图片报错:mkdir():permission denied
    一次基于老古董thinkPHP3.1的修改尝试
    微信网页开发 thinkphp5.0的try-catch和重定向
    CentOS 7.2下服务器配置(linux+apache+php+mysql)
    微信小程序踩坑(不定时更新)
    PHP 定时自动执行代码
    PHP TP5 文章评论+积分+签到
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/13401205.html
Copyright © 2020-2023  润新知