Hbase 简明教程

HBase - Describe & Alter

describe

此命令返回表的描述。其语法如下:

This command returns the description of the table. Its syntax is as follows:

hbase> describe 'table name'

以下是 emp 表上 describe 命令的输出:

Given below is the output of the describe command on the emp table.

hbase(main):006:0> describe 'emp'
   DESCRIPTION
      ENABLED

'emp', {NAME ⇒ 'READONLY', DATA_BLOCK_ENCODING ⇒ 'NONE', BLOOMFILTER
⇒ 'ROW', REPLICATION_SCOPE ⇒ '0', COMPRESSION ⇒ 'NONE', VERSIONS ⇒
'1', TTL true

⇒ 'FOREVER', MIN_VERSIONS ⇒ '0', KEEP_DELETED_CELLS ⇒ 'false',
BLOCKSIZE ⇒ '65536', IN_MEMORY ⇒ 'false', BLOCKCACHE ⇒ 'true'}, {NAME
⇒ 'personal

data', DATA_BLOCK_ENCODING ⇒ 'NONE', BLOOMFILTER ⇒ 'ROW',
REPLICATION_SCOPE ⇒ '0', VERSIONS ⇒ '5', COMPRESSION ⇒ 'NONE',
MIN_VERSIONS ⇒ '0', TTL

⇒ 'FOREVER', KEEP_DELETED_CELLS ⇒ 'false', BLOCKSIZE ⇒ '65536',
IN_MEMORY ⇒ 'false', BLOCKCACHE ⇒ 'true'}, {NAME ⇒ 'professional
data', DATA_BLO

CK_ENCODING ⇒ 'NONE', BLOOMFILTER ⇒ 'ROW', REPLICATION_SCOPE ⇒ '0',
VERSIONS ⇒ '1', COMPRESSION ⇒ 'NONE', MIN_VERSIONS ⇒ '0', TTL ⇒
'FOREVER', K

EEP_DELETED_CELLS ⇒ 'false', BLOCKSIZE ⇒ '65536', IN_MEMORY ⇒
'false', BLOCKCACHE ⇒ 'true'}, {NAME ⇒ 'table_att_unset',
DATA_BLOCK_ENCODING ⇒ 'NO

NE', BLOOMFILTER ⇒ 'ROW', REPLICATION_SCOPE ⇒ '0', COMPRESSION ⇒
'NONE', VERSIONS ⇒ '1', TTL ⇒ 'FOREVER', MIN_VERSIONS ⇒ '0',
KEEP_DELETED_CELLS

⇒ 'false', BLOCKSIZE ⇒ '6

alter

Alter 是用于对现有表进行更改的命令。使用此命令,您可以更改列系列的最大单元格数,设置和删除表范围运算符,以及从表中删除列系列。

Alter is the command used to make changes to an existing table. Using this command, you can change the maximum number of cells of a column family, set and delete table scope operators, and delete a column family from a table.

Changing the Maximum Number of Cells of a Column Family

以下是更改列系列的最大单元格数的语法:

Given below is the syntax to change the maximum number of cells of a column family.

hbase> alter 't1', NAME ⇒ 'f1', VERSIONS ⇒ 5

在以下示例中,单元格的最大数目设置为 5。

In the following example, the maximum number of cells is set to 5.

hbase(main):003:0> alter 'emp', NAME ⇒ 'personal data', VERSIONS ⇒ 5
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.3050 seconds

Table Scope Operators

使用 alter,您可以设置和删除表范围运算符,例如 MAX_FILESIZE、READONLY、MEMSTORE_FLUSHSIZE、DEFERRED_LOG_FLUSH 等。

Using alter, you can set and remove table scope operators such as MAX_FILESIZE, READONLY, MEMSTORE_FLUSHSIZE, DEFERRED_LOG_FLUSH, etc.

Setting Read Only

以下是使表变为只读的语法:

Below given is the syntax to make a table read only.

hbase>alter 't1', READONLY(option)

在以下示例中,我们使 emp 表变为只读。

In the following example, we have made the emp table read only.

hbase(main):006:0> alter 'emp', READONLY
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.2140 seconds

Removing Table Scope Operators

我们还可以移除表作用域运算符。以下是从 emp 表中移除 "MAX_FILESIZE" 的语法:

We can also remove the table scope operators. Given below is the syntax to remove ‘MAX_FILESIZE’ from emp table.

hbase> alter 't1', METHOD ⇒ 'table_att_unset', NAME ⇒ 'MAX_FILESIZE'

Deleting a Column Family

使用 alter,您还可以删除一个列系列。以下是使用 alter 删除一个列系列的语法:

Using alter, you can also delete a column family. Given below is the syntax to delete a column family using alter.

hbase> alter ‘ table name ’, ‘delete’ ⇒ ‘ column family ’

以下是从 "emp" 表中删除一个列系列的示例:

Given below is an example to delete a column family from the ‘emp’ table.

假设 HBase 中有一个名为 employee 的表。它包含以下数据:

Assume there is a table named employee in HBase. It contains the following data:

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

   ROW                   COLUMN+CELL

row1 column = personal:city, timestamp = 1418193767, value = hyderabad

row1 column = personal:name, timestamp = 1418193806767, value = raju

row1 column = professional:designation, timestamp = 1418193767, value = manager

row1 column = professional:salary, timestamp = 1418193806767, value = 50000

1 row(s) in 0.0160 seconds

现在让我们使用 alter 命令删除名为 professional 的列系列。

Now let us delete the column family named professional using the alter command.

hbase(main):007:0> alter 'employee','delete'⇒'professional'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.2380 seconds

现在验证更改后的表中的数据。观察列系列 "professional" 不存在了,因为我们已经将其删除了。

Now verify the data in the table after alteration. Observe the column family ‘professional’ is no more, since we have deleted it.

hbase(main):003:0> scan 'employee'
   ROW             COLUMN + CELL
row1 column = personal:city, timestamp = 14181936767, value = hyderabad

row1 column = personal:name, timestamp = 1418193806767, value = raju

1 row(s) in 0.0830 seconds

Adding a Column Family Using Java API

您可以使用 HBAseAdmin 类的 addColumn() 方法向表中添加一个列系列。按照以下步骤向表中添加一个列系列。

You can add a column family to a table using the method addColumn() of HBAseAdmin class. Follow the steps given below to add a column family to a table.

Step 1

实例化 HBaseAdmin 类。

Instantiate the HBaseAdmin class.

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

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

Step 2

addColumn() 方法需要一个表名和一个 HColumnDescriptor 类对象。因此实例化 HColumnDescriptor 类。 HColumnDescriptor 的构造函数又需要一个要添加的列系列名称。这里我们向现有的 "employee" 表中添加一个名为 "contactDetails" 的列系列。

The addColumn() method requires a table name and an object of HColumnDescriptor class. Therefore instantiate the HColumnDescriptor class. The constructor of HColumnDescriptor in turn requires a column family name that is to be added. Here we are adding a column family named “contactDetails” to the existing “employee” table.

// Instantiating columnDescriptor object

HColumnDescriptor columnDescriptor = new
HColumnDescriptor("contactDetails");

Step 3

使用 addColumn 方法添加列系列。将表名和 HColumnDescriptor 类对象作为参数传递给此方法。

Add the column family using addColumn method. Pass the table name and the HColumnDescriptor class object as parameters to this method.

// Adding column family
admin.addColumn("employee", new HColumnDescriptor("columnDescriptor"));

以下是向现有表中添加列系列的完整程序。

Given below is the complete program to add a column family to an existing table.

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

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

public class AddColoumn{

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

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

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

      // Instantiating columnDescriptor class
      HColumnDescriptor columnDescriptor = new HColumnDescriptor("contactDetails");

      // Adding column family
      admin.addColumn("employee", columnDescriptor);
      System.out.println("coloumn added");
   }
}

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

Compile and execute the above program as shown below.

$javac AddColumn.java
$java AddColumn

只有在您在 " .bashrc " 中设置了类路径时,上述编译才有效。如果您还没有,请按照以下步骤编译您的 .java 文件。

The above compilation works only if you have set the classpath in “ .bashrc ”. If you haven’t, follow the procedure given below to compile your .java file.

//if "/home/home/hadoop/hbase " is your Hbase home folder then.

$javac -cp /home/hadoop/hbase/lib/*: Demo.java

如果一切顺利,它将生成以下输出:

If everything goes well, it will produce the following output:

column added

Deleting a Column Family Using Java API

您可以使用 HBAseAdmin 类的 deleteColumn() 方法从表中删除一个列系列。按照以下步骤向表中添加一个列系列。

You can delete a column family from a table using the method deleteColumn() of HBAseAdmin class. Follow the steps given below to add a column family to a table.

Step1

实例化 HBaseAdmin 类。

Instantiate the HBaseAdmin class.

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

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

Step2

使用 deleteColumn() 方法添加列系列。将表名和列系列名称作为参数传递给此方法。

Add the column family using deleteColumn() method. Pass the table name and the column family name as parameters to this method.

// Deleting column family
admin.deleteColumn("employee", "contactDetails");

以下是从现有表中删除一个列系列的完整程序。

Given below is the complete program to delete a column family from an existing table.

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

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

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

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

      // Deleting a column family
      admin.deleteColumn("employee","contactDetails");
      System.out.println("coloumn deleted");
   }
}

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

Compile and execute the above program as shown below.

$javac DeleteColumn.java
$java DeleteColumn

输出应如下所示:

The following should be the output:

column deleted