16

I want to empty a table in hbase... eg: user. Is there any command or function to empty the table without deleting it...

My table structure is :

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

Can someone help me?

svick
  • 236,525
  • 50
  • 385
  • 514
Micku
  • 550
  • 4
  • 9
  • 23

9 Answers9

72

If you execute this in HBase shell:

 > truncate 'yourTableName'

Then HBase will execute this operations for 'yourTableName':

 > disable 'yourTableName'
 > drop 'yourTableName'
 > create 'yourTableName', 'f1', 'f2', 'f3'
Alexis Gamarra
  • 4,362
  • 1
  • 33
  • 23
  • 13
    'truncate' doesn't preserve the table's permissions. However, the newer command 'truncate_preserve' does – selle Dec 01 '16 at 13:04
5

Another efficient option is to actually delete the table then reconstruct another one with all the same settings as the previous.

I don't know how to do this in php, but I do know how to do it in Java. The corresponding actions in php should be similar, you just need to check how the API looks like.

In Java using 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);
André Staltz
  • 13,304
  • 9
  • 48
  • 58
  • 1
    Even better, there is a shell command availble for doing exactly this. And it is called truncate. Read about it here: http://wiki.apache.org/hadoop/Hbase/Shell – David Wickstrom May 29 '13 at 11:44
4

Using hbase shell, truncate <table_name> will do the task.

The snapshot of truncate 'customer_details' command is shown below:enter image description here

where customer_details is the table name

Farooque
  • 3,616
  • 2
  • 29
  • 41
2

truncate command in hbase shell will do the job for you:

Before truncate: enter image description here

After truncate: enter image description here

Heapify
  • 2,581
  • 17
  • 17
1

For the purposes of this you can use HAdmin. It is an UI tool for Apache HBase administration. There are "Truncate table" and even "Delete table" buttons in alter table page. enter image description here

grbulat
  • 136
  • 3
1

HBase thrift API (which is what php is using) doesn't provide a truncate command only deleteTable and createTable functionality (what's the diff from your point of view?)

otherwise you have to scan to get all the keys and deleteAllRow for each key - which isn't a very efficient option

Arnon Rotem-Gal-Oz
  • 25,469
  • 3
  • 45
  • 68
0

Using alter command

alter '<table_name>', NAME=>'column_family',TTL=><number_of_seconds>

here number_of_seconds stands for duration after which data will be automatically deleted.

Milesh
  • 271
  • 1
  • 8
  • 24
-4

Perhaps using one of these two commands:

DELETE FROM your_table WHERE 1;

Or

TRUNCATE your_table;

Regards!

Lobo
  • 4,001
  • 8
  • 37
  • 67
  • @Micku These are standard SQL statements, it is strange not work. The user you use to connect to the database, do you have permission to perform a DELETE or TRUNCATE?. – Lobo Mar 28 '12 at 08:08
-4

There's no single command to clear Hbase table, but you can use 2 workarounds: disable, delete, create table, or scan all records and delete each.

Actually, disable, delete and create table again takes about 4 seconds.

// 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);

If there's not a lot of data in table - scan all rows and delete each is much faster.

$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);
}

In this case data is not deleted physically, actually it's "marked as deleted" and stays in table until next major compact.

starl1ng
  • 136
  • 3