HBase 禁用表

1. 使用HBase Shell禁用表

要删除表格或更改其设置,您需要先使用disable命令禁用表格。您可以使用enable命令重新启用它。

下面给出的是禁用表的语法:

disable ‘emp’

范例

下面给出了一个示例,说明如何禁用表。

hbase(main):025:0> disable 'emp'
0 row(s) in 1.2760 seconds

验证

禁用表后,仍然可以通过 列表存在 命令检测其存在。你无法扫描它。它会给你以下错误。

hbase(main):028:0> scan 'emp'
ROW         COLUMN + CELL
ERROR: emp is disabled.

被禁用

该命令用于查找表是否被禁用。其语法如下。

hbase> is_disabled 'table name'

以下示例验证名为emp的表是否被禁用。如果它被禁用,它将返回true,否则返回false。

hbase(main):031:0> is_disabled 'emp'
true
0 row(s) in 0.0440 seconds

禁用所有

该命令用于禁用所有匹配给定正则表的表。下面给出 disable_all 命令的语法。

hbase> disable_all 'r.*'

假设HBase有5张桌子,分别是raja,rajani,rajendra,rajesh和raju。以下代码将禁用所有以 raj 开头的表

hbase(main):002:07> disable_all 'raj.*'
raja
rajani
rajendra
rajesh
raju
Disable the above 5 tables (y/n)?
y
5 tables successfully disabled

 

2. 使用Java API禁用表

要验证表是否被禁用,使用 isTableDisabled() 方法并禁用表,使用 disableTable() 方法。这些方法属于 HBaseAdmin 类。按照以下步骤禁用表格。

步骤1

实例化 HBaseAdmin 类,如下所示。

// Creating configuration object
Configuration conf = HBaseConfiguration.create();

// Creating HBaseAdmin object
HBaseAdmin admin = new HBaseAdmin(conf);

第2步

使用 isTableDisabled() 方法验证表是否被禁用,如下所示。

Boolean b = admin.isTableDisabled("emp");

第3步

如果表未被禁用,请按如下所示禁用它。

if(!b){
   admin.disableTable("emp");
   System.out.println("Table disabled");
}

下面给出的是完整的程序来验证表是否被禁用; 如果没有,如何禁用它。

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class DisableTable{

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

      // Instantiating configuration class
      Configuration conf = HBaseConfiguration.create();

      // Instantiating HBaseAdmin class
      HBaseAdmin admin = new HBaseAdmin(conf);

      // Verifying weather the table is disabled
      Boolean bool = admin.isTableDisabled("emp");
      System.out.println(bool);

      // Disabling the table using HBaseAdmin object
      if(!bool){
         admin.disableTable("emp");
         System.out.println("Table disabled");
      }
   }
}

编译并执行上述程序,如下所示。

$javac DisableTable.java
$java DsiableTable

以下应该是输出:

false
Table disabled