Hbase记录-HBase基本操作(二)

HBase Exists

可以使用exists命令验证表的存在。下面的示例演示了如何使用这个命令。

hbase(main):024:0> exists 'emp'
Table emp does exist

0 row(s) in 0.0750 seconds

==================================================================

hbase(main):015:0> exists 'student'
Table student does not exist

0 row(s) in 0.0480 seconds

使用Java API验证表的存在

可以使用HBaseAdmin类的tableExists()方法验证表在HBase中是否存在。按照下面给出的步骤验证HBase表存在。

第1步

Instantiate the HBaseAdimn class

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

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

第2步

使用tableExists()方法来验证表的存在。

下面给出的是使用java程序中的Java API来测试一个HBase表的存在。

import java.io.IOException;

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

public class TableExists{

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

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

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

   // Verifying the existance of the table
   boolean bool = admin.tableExists("emp");
   System.out.println( bool);
   }
}

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

$javac TableExists.java
$java TableExists

下面列出的是输出:

true

HBase删除表

用drop命令可以删除表。在删除一个表之前必须先将其禁用。

hbase(main):018:0> disable 'emp'
0 row(s) in 1.4580 seconds


hbase(main):019:0> drop 'emp'
0 row(s) in 0.3060 seconds

使用exists 命令验证表是否被删除。

hbase(main):020:0> exists 'emp'
Table emp does not exist

0 row(s) in 0.0730 seconds

drop_all

这个命令是用来在给出删除匹配“regex”表。它的语法如下:

hbase> drop_all t.*’

注意:要删除表,则必须先将其禁用。

示例

假设有一些表的名称为raja, rajani, rajendra, rajesh, 和 raju。

hbase(main):017:0> list
TABLE
raja
rajani
rajendra 
rajesh
raju
9 row(s) in 0.0270 seconds

所有这些表以字母raj开始。首先使用disable_all命令禁用所有这些表如下所示。

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

现在,可以使用 drop_all 命令删除它们,如下所示。

hbase(main):018:0> drop_all 'raj.*'
raja
rajani
rajendra
rajesh
raju

Drop the above 5 tables (y/n)?

y
5 tables successfully dropped

使用Java API删除表

可以使用 HBaseAdmin 类的deleteTable()方法删除表。按照下面给出是使用Java API来删除表中的步骤。

第1步

实例化HBaseAdmin类。

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

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

第2步

使用HBaseAdmin类的disableTable()方法禁止表。

admin.disableTable("emp1");

第3步

现在使用HBaseAdmin类的deleteTable()方法删除表。

admin.deleteTable("emp12");

下面给出的是完整的Java程序用于删除HBase表。

import java.io.IOException;

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

public class DeleteTable {

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

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

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

      // disabling table named emp
      admin.disableTable("emp12");

      // Deleting emp
      admin.deleteTable("emp12");
      System.out.println("Table deleted");
   }
}

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

$javac DeleteTable.java
$java DeleteTable

下面是输出结果:

Table deleted

HBase关闭

exit

可以通过键入exit命令退出shell。

hbase(main):021:0> exit

停止HBase

要停止HBase,浏览进入到HBase主文件夹,然后键入以下命令。

./bin/stop-hbase.sh

使用Java API停止HBase

可以使用HBaseAdmin类的shutdown()方法关闭HBase。按照下面给出关闭HBase的步骤:

第1步

实例化HbaseAdmin类。

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

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

第2步

使用HBaseAdmin类的shutdown()方法关闭HBase。

admin.shutdown();

下面给出的是停止HBase的程序。

import java.io.IOException;

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

public class ShutDownHbase{

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

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

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

      // Shutting down HBase
      System.out.println("Shutting down hbase");
      admin.shutdown();
   }
}

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

$javac ShutDownHbase.java
$java ShutDownHbase

下面是输出结果:

Shutting down hbase
原文地址:https://www.cnblogs.com/xinfang520/p/7717337.html