如何清除hbase中的表?

时间:2012-03-28 06:40:38

标签: php hbase

我想在hbase中清空一个表...例如:user。是否有任何命令或函数来清空表而不删除它...

我的表结构是:

$mutations = array(
                new Mutation( array(
                    'column' => 'username:1',
                    'value' =>$name
                ) ),
                new Mutation( array(
                    'column' => 'email:1',
                    'value' =>$email
                ) )
        );          
$hbase->mutateRow("user",$key,$mutations);

有人可以帮助我吗?

8 个答案:

答案 0 :(得分:54)

如果在HBase shell中执行此操作:

 > truncate 'yourTableName'

然后HBase将对'yourTableName'执行此操作:

 > disable 'yourTableName'
 > drop 'yourTableName'
 > create 'yourTableName', 'f1', 'f2', 'f3'

答案 1 :(得分:5)

另一个有效的选择是实际删除表,然后使用与前一个相同的设置重建另一个表。

我不知道如何在php中执行此操作,但我知道如何在Java中执行此操作。 php中的相应操作应该类似,您只需要检查API的外观。

在Java中使用HBase 0.90.4:

// Remember the "schema" of your table
HBaseAdmin admin = new HBaseAdmin(yourConfiguration);
HTableDescriptor td = admin.getTableDescriptor(Bytes.toBytes("yourTableName");

// Delete your table
admin.disableTable("yourTableName");
admin.deleteTable("yourTableName");

// Recreate your talbe
admin.createTable(td);

答案 2 :(得分:4)

使用hbase shelltruncate <table_name>将完成任务。

truncate 'customer_details'命令的快照如下所示:enter image description here

其中customer_details是表名

答案 3 :(得分:2)

hbase shell中的

truncate命令将为您完成任务:

截断前: enter image description here

截断后: enter image description here

答案 4 :(得分:1)

HBase thrift API(这是php正在使用的)不提供truncate命令只有deleteTable和createTable功能(从你的观点来看,差异是什么?)

否则你必须扫描以获得每个键的所有键和deleteAllRow - 这不是一个非常有效的选项

答案 5 :(得分:1)

为此,您可以使用HAdmin。它是用于Apache HBase管理的UI工具。在alter table页面中有“ Truncate table”甚至“ Delete table”按钮。 enter image description here

答案 6 :(得分:-3)

没有单一命令来清除Hbase表,但您可以使用2个解决方法:禁用,删除,创建表或扫描所有记录并删除每个记录。

实际上,再次禁用,删除和创建表大约需要4秒钟。

// get Hbase client
$client = <Your code here>;
$t = "table_name";

$tables = $client->getTableNames();

if (in_array($t, $tables)) {
  if ($client->isTableEnabled($t))
    $client->disableTable($t);

  $client->deleteTable($t);
}

$descriptors = array(
  new ColumnDescriptor(array("name" =>  "c1", "maxVersions" => 1)),
  new ColumnDescriptor(array("name" =>  "c2", "maxVersions" => 1))
);

$client->createTable($t, $descriptors);

如果表格中没有大量数据 - 扫描所有行并删除每个行的速度要快得多。

$client = <Your code here>;
$t = "table_name";

// i don't remember, if list of column families is actually needed here
$columns = array("c1", "c2");

$scanner = $client->scannerOpen($t, "", $columns);
while ($result = $client->scannerGet($scanner)) {
  $client->deleteAllRow($t, $result[0]->row);
}

在这种情况下,数据不会被物理删除,实际上它被“标记为已删除”并保留在表中直到下一个主要契约。

答案 7 :(得分:-4)

也许使用以下两个命令之一:

DELETE FROM your_table WHERE 1;

或者

TRUNCATE your_table;

问候!