Hbase 简明教程

HBase - Enabling a Table

Enabling a Table using HBase Shell

启用表的语法:

enable ‘emp’

Example

以下是启用表的示例。

hbase(main):005:0> enable 'emp'
0 row(s) in 0.4580 seconds

Verification

启用表后,扫描它。如果能看到该架构,则说明表已成功启用。

hbase(main):006:0> scan 'emp'

   ROW                        COLUMN + CELL

1 column = personal data:city, timestamp = 1417516501, value = hyderabad

1 column = personal data:name, timestamp = 1417525058, value = ramu

1 column = professional data:designation, timestamp = 1417532601, value = manager

1 column = professional data:salary, timestamp = 1417524244109, value = 50000

2 column = personal data:city, timestamp = 1417524574905, value = chennai

2 column = personal data:name, timestamp = 1417524556125, value = ravi

2 column = professional data:designation, timestamp = 14175292204, value = sr:engg

2 column = professional data:salary, timestamp = 1417524604221, value = 30000

3 column = personal data:city, timestamp = 1417524681780, value = delhi

3 column = personal data:name, timestamp = 1417524672067, value = rajesh

3 column = professional data:designation, timestamp = 14175246987, value = jr:engg

3 column = professional data:salary, timestamp = 1417524702514, value = 25000

3 row(s) in 0.0400 seconds

is_enabled

此命令用于查找表是否已启用。其语法如下:

hbase> is_enabled 'table name'

以下代码验证名为 emp 的表是否已启用。如果已启用,它将返回 true;如果未启用,它将返回 false。

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

Enable a Table Using Java API

要验证表是否已启用,使用 isTableEnabled() 方法;要启用表,使用 enableTable() 方法。这些方法属于 HBaseAdmin 类。按照以下步骤启用表。

Step1

按如下所示实例化 HBaseAdmin 类。

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

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

Step 2

使用 isTableEnabled() 方法验证表是否已启用,如下所示。

Boolean bool = admin.isTableEnabled("emp");

Step 3

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

if(!bool){
   admin.enableTable("emp");
   System.out.println("Table enabled");
}

以下是完整的程序,用于验证表是否已启用,如果未启用,则如何启用它。

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 EnableTable{

   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 whether the table is disabled
      Boolean bool = admin.isTableEnabled("emp");
      System.out.println(bool);

      // Enabling the table using HBaseAdmin object
      if(!bool){
         admin.enableTable("emp");
         System.out.println("Table Enabled");
      }
   }
}

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

$javac EnableTable.java
$java EnableTable

输出应如下所示:

false
Table Enabled