Hbase实验:java创建和删除table

Posted 徐嘤嘤的菜园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
           }
    }
}

以上是关于Hbase实验:java创建和删除table的主要内容,如果未能解决你的问题,请参考以下文章

删除hbase的region步骤和代码

HBASE基础使用Java API实现DDL与DML

Hbase JavaAPI使用

hbase

Hbase常用操作记录

HBase创建,删除,清空数据表