HBase: Create table with same schema as existing table

you can use snapshot feature to do this. like this;

hbase> snapshot 'tableName', 'tableSnapshot'
hbase> clone_snapshot 'tableSnapshot', 'newTableName'
hbase> delete_snapshot 'tableSnapshot'
hbase> truncate 'newTableName'

i hope your table is not huge. And you can not copy column qualifiers with empty values, if i did not understand wrong, you mean this in your question. you can either copy all data to new table or only table structure with column families, coprocessors.. etc.


Here is the full-proof approach, I used. Hope it will help other people.

1) launch HBase shell vial below command

 hbase shell

2) Query existing table metadata with below command

 hbase> describe ‘existing_table’;

Output would be similar to:

{NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING 
=> 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION
_SCOPE => '0'}                                                                                                                  
{NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING 
=> 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION
_SCOPE => '0'}

3) Copy this output to a notepad and do below changes:

  a) Replace  TTL => 'FOREVER' with TTL => org.apache.hadoop.hbase.HConstants::FOREVER
  b) Put an additional comma (,) between each column family description to connect column family definition.
  c) Remove newline characters (\n, \r) for the text; such that the content become one line text.

4) Finally running the create query with new table name:

create ‘copy_of_exsting_table_schema’, {NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING 
=> 'NONE', TTL => org.apache.hadoop.hbase.HConstants::FOREVER, COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION
_SCOPE => '0'} ,{NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING 
=> 'NONE', TTL => org.apache.hadoop.hbase.HConstants::FOREVER, COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION
_SCOPE => '0'}

And you are done. The new table schema is exactly same as existing table.