wiki:NCHCCloudCourse100929_4_HBEX5
HBase 進階課程
程式範例練習

上一關 < 第五關 > 下一關

範例五: 刪除資料表

$ bin/hadoop jar TCRCExample.jar DropTable "ex1Table"
package org.nchc.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.util.GenericOptionsParser;

public class DropTable {

  static void drop(String tablename) {
    HBaseConfiguration conf = new HBaseConfiguration();

    HBaseAdmin admin;
    try {
      admin = new HBaseAdmin(conf);
      if (admin.tableExists(tablename)) {
        admin.disableTable(tablename);
        admin.deleteTable(tablename);
        System.out.println("Droped the table [" + tablename + "]");
      } else {
        System.out.println("Table [" + tablename + "] was not found!");
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] argv) {
      // eclipse only
//      String[] arg = {"ex1Table"};
//      argv = arg;
    String[] otherArgs = new GenericOptionsParser(new Configuration(), argv)
        .getRemainingArgs();
    if (otherArgs.length < 1) {
      System.out.println("DropTable <TableName> ");
      return;
    }
    
    drop(otherArgs[0]);
  }
}

  • 執行結果
Droped the table [ex1Table]
Last modified 13 years ago Last modified on Apr 24, 2011, 6:23:50 PM