Search code examples
phphbase

How to clear a table in hbase?


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?


Solution

  • 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.