HBase 禁用表
使用 HBase Shell 禁用表
要删除表或更改其设置,您需要先使用 disable 命令禁用该表。您可以使用 enable 命令重新启用它。
下面给出了禁用表的语法:
示例
下面给出的示例展示了如何禁用表。
hbase(main):025:0> disable 'emp'
0 row(s) in 1.2760 seconds
验证
禁用表后,您仍然可以通过
list 和
exists 命令感知它的存在。你不能扫描它。它会给你以下错误。
hbase(main):028:0> scan 'emp'
ROW COLUMN + CELL
ERROR: emp is disabled.
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
此命令用于禁用与给定正则表达式匹配的所有表。
disable_all 命令的语法如下所示。
假设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
使用 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
以下应该是输出: