• Hbase实验:java创建和删除table


    开启zookeeper、hadoop、hbase:


    打开eclipse创一个java project,然后导入所需jar包:

    写好java代码,运行create,然后去hbase shell里查看:

    运行delete:

    package hh;
    
    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 ad {
        public static Configuration configuration;
      
        public static void main(String[] args) {  
            // createTable();
        	deleteTable();
         }  
      
        public static void createTable() {
            Configuration config = HBaseConfiguration.create();
            config.set("hbase.zookeeper.quorum", "127.0.0.1");
            config.set("hbase.zookeeper.property.clientPort", "2181");
    
            Connection connection = null;
            Admin admin = null;
    
            try {
                connection = ConnectionFactory.createConnection(config);
                admin = connection.getAdmin();
                
                String tableName = "mytable";
    
                if (!admin.isTableAvailable(TableName.valueOf(tableName))) {
                    HTableDescriptor hbaseTable = new HTableDescriptor(TableName.valueOf(tableName));
                    hbaseTable.addFamily(new HColumnDescriptor("name"));
                    hbaseTable.addFamily(new HColumnDescriptor("contact_info"));
                    hbaseTable.addFamily(new HColumnDescriptor("personal_info"));
                    admin.createTable(hbaseTable);
                    }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (admin != null) {
                        admin.close();
                        }
                    if (connection != null && !connection.isClosed()) {
                        connection.close();
                        }
                } catch (Exception e2) {
                    e2.printStackTrace();
                    }
               }
        }
        
        public static void deleteTable() {
        	  Configuration config = HBaseConfiguration.create();
            config.set("hbase.zookeeper.quorum", "127.0.0.1");
            config.set("hbase.zookeeper.property.clientPort", "2181");
    
            Connection connection = null;
            Admin admin = null;
            try {
            	   connection = ConnectionFactory.createConnection(config);
                admin = connection.getAdmin();
                  
                String tableName = "mytable";
                admin.disableTable(TableName.valueOf(tableName));
                admin.deleteTable(TableName.valueOf(tableName));
            } catch(Exception e) {
                e.printStackTrace();
               }
        }
    }
    
  • 相关阅读:
    Mybatis 框架下 SQL 注入攻击的方式
    Vue 环境准备
    HTTP.sys漏洞的检测和修复(附补丁包下载)
    BPM工作流中的一些业务场景
    关系型数据库
    .NET中使用Redis总结——2.项目实战
    Java 开源项目整合
    在IIS 搭建FTP站点
    悲观锁和乐观锁详解
    C# 通过一个控制台打开另一个控制台
  • 原文地址:https://www.cnblogs.com/AlphaWA/p/11921901.html
Copyright © 2020-2023  润新知