How to rename a table in HBase

Document created by sumesh_kurup on Feb 8, 2016
Version 1Show Document
  • View in full screen mode

Author: Sumesh Kurup

 

Original Publication Date: March 13, 2015

To rename an HBase table the HBase snapshot functionality is used.  To turn on snapshot support set the property 'hbase.snapshot.enabled' to 'true' in /opt/mapr/hbase/hbase-<version>/conf/hbase-site.xml on all HBase Master nodes and restart the HBase Master service on all nodes for the changes to take effect. Note that snapshot support is enabled by default in 0.96.x and later and disabled by default in 0.94.x.

 

<property>

  <name>hbase.snapshot.enabled</name>

  <value>true</value>

  </property>

 

The steps to rename a HBase table are as follows. We are going to rename the Hbase table "cp_data_mh_98" to "modcp_cp_data_mh_98". Note: This article is specific to HBase tables. The table rename operations mentioned in this article apply only to HBase and are not applicable for MapR-DB tables.

 

 


1.  List the table in the HBase shell.

 

# hbase shell

HBase Shell; enter 'help<RETURN>' for list of supported commands.

Type "exit<RETURN>" to leave the HBase Shell

Version 0.94.24-mapr-1501, rc3e5c97beeaf597d26ea5ef136fa6805f7bc7052, Tue Jan 13 18:21:33 PST 2015

Not all HBase shell commands are applicable to MapR tables.

Consult MapR documentation for the list of supported commands.

hbase(main):001:0> list

Listing HBase tables. Specify a path or configure namespace mappings to list M7 tables.

TABLE

PerformanceData_2014_11

apache_access_log

apache_access_log_mod

cp_data_mh_98

8 row(s) in 0.2190 seconds

2. Run "describe <table_name>" on the table you want to rename to see the attributes.

 

hbase(main):007:0> describe 'cp_data_mh_98'

DESCRIPTION ENABLED

'cp_data_mh_98', {NAME => 'd', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SC true

OPE => '0', COMPRESSION => 'NONE', VERSIONS => '3', TTL => '-1', MIN_VERSIONS => '0', KEEP_DELETED_

CELLS => 'false', BLOCKSIZE => '65536', ENCODE_ON_DISK => 'true', IN_MEMORY => 'false', BLOCKCACHE

=> 'false'}

1 row(s) in 0.3560 seconds

3. Disable the HBase table

hbase(main):008:0> disable 'cp_data_mh_98'

0 row(s) in 2.2690 seconds

Note: When the table is "disabled" all access to the table including read and writes is blocked. Please ensure that you have downtime to complete this activity so that there is no impact on production table operations.

 

4. Run "describe <table_name>" to confirm the table is disabled by checking the "ENABLED" field. If the table is disabled it will show up as "false" as below.

 

hbase(main):010:0> describe 'cp_data_mh_98'

DESCRIPTION ENABLED

'cp_data_mh_98', {NAME => 'd', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SC false

OPE => '0', COMPRESSION => 'NONE', VERSIONS => '3', TTL => '-1', MIN_VERSIONS => '0', KEEP_DELETED_

CELLS => 'false', BLOCKSIZE => '65536', ENCODE_ON_DISK => 'true', IN_MEMORY => 'false', BLOCKCACHE

=> 'false'}

1 row(s) in 0.2460 seconds

5. Create a snapshot of the table.

 

hbase(main):011:0> snapshot 'cp_data_mh_98', 'snap_cp_data_mh_98' 
0 row(s) in 1.0850 seconds

6. Create a new table from the snapshot using the "clone" operation.

hbase(main):013:0> clone_snapshot 'snap_cp_data_mh_98', 'modcp_cp_data_mh_98' 
0 row(s) in 1.6940 seconds

7. Run "describe <table_name>" on the newly created table to confirm the attributes match those of the original table.

 

hbase(main):014:0> describe 'modcp_cp_data_mh_98'

DESCRIPTION ENABLED

'modcp_cp_data_mh_98', {NAME => 'd', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICAT true

ION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS => '3', TTL => '-1', MIN_VERSIONS => '0', KEEP_DE

LETED_CELLS => 'false', BLOCKSIZE => '65536', ENCODE_ON_DISK => 'true', IN_MEMORY => 'false', BLOCK

CACHE => 'false'}

1 row(s) in 0.2490 seconds

Once the clone operation completes the new table can be used in place of the original table . The original table can be dropped and the snapshot taken can be deleted if they are no longer needed. The steps to do so are as follows:

 

8. To delete the snapshot use the following:

hbase(main):017:0> delete_snapshot 'snap_cp_data_mh_98' 
0 row(s) in 0.0430 seconds
9. To drop the original table use the following:

 

hbase(main):018:0> drop 'cp_data_mh_98' 
0 row(s) in 1.5500 seconds

 

To implement this procedure using the HBase API you can use code similar to the following:

 

void rename( String oldTableName, String newTableName) throws IOException {

 

  Configuration conf = HBaseConfiguration.create();

  HBaseAdmin admin = new HBaseAdmin(conf);

  String snapshotName = randomName();

  admin.disableTable(oldTableName);

  admin.snapshot(snapshotName, oldTableName);

  admin.cloneSnapshot(snapshotName, newTableName);

  admin.deleteSnapshot(snapshotName);

  admin.deleteTable(oldTableName);

}

The following is a sample implementation of the above code segment to rename and HBase table using the HBase API.

import java.io.IOException;

 

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.client.HBaseAdmin;

 

public class HbaseRenameTable {

  public static void rename(String oldTableName, String newTableName,

  String snapshotName) throws IOException, InterruptedException {

 

  Configuration conf = HBaseConfiguration.create();

  HBaseAdmin admin = new HBaseAdmin(conf);

  admin.disableTable(oldTableName);

  admin.snapshot(snapshotName, oldTableName);

  admin.cloneSnapshot(snapshotName, newTableName);

  admin.deleteSnapshot(snapshotName);

  admin.deleteTable(oldTableName);

  admin.close();

  }

 

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

  if (args.length < 3) {

  System.out

  .println("Not enough arguments : Usage HbaseRenameTable oldTableName newTableName snapshotName");

  return;

  }

  rename(args[0], args[1], args[2]);

  }

}

 

1 person found this helpful

Attachments

    Outcomes