A script that deletes all tables in Hbase

2019-01-24 02:43发布

问题:

I can tell hbase to disable and delete particular tables using:

disable 'tablename'
drop 'tablename'

But I want to delete all the tables in the database without hardcoding the names of any of the tables. Is there a way to do this? I want to do this through the command-line utility ./hbase shell, not through Java or Thrift.

回答1:

disable_all and drop_all have been added as commands in the HBase ruby shell. These commands were added in jira HBASE-3506 These commands take a regex of tables to disable/drop. And they will ask for confirmation before continuing. That should make droping lots of tables pretty easy and not require outside libraries or scripting.



回答2:

I have a handy script that does exactly this, using the Python Happybase library:

import happybase

c = happybase.Connection()

for table in c.tables():
  c.disable_table(table)
  c.delete_table(table)
  print "Deleted: " + table

You will need Happybase installed to use this script, and you can install it as:

sudo easy_install happybase


回答3:

You can pipe commands to the bin/hbase shell command. From there you can use some scripting to grab the table names and pipe the disable/delete commands back to hbase.

i.e.

echo "list" | bin/hbase shell | ./filter_table_names.pl > table_names.txt
./turn_table_names_into_disable_delete_commands.pl table_names.txt | bin/hbase shell 


回答4:

There is a hack. Open $HBASE_HOME/lib/ruby/shell/commands/list.rb file and add below line at the bottom of command method.

return list

After that, list command returns an array of names of all tables. And then do just like this.

list.each {|t| disable t;drop t}


回答5:

I'm not deleting tables through the hbase shell but I deleting them from the command line by,
- deleting my hadoop distributed filesystem directory, then,
- creating a new clean hadoop distributed filesystem directory, then,
- formatting my hadoop distributed filesystem with 'hadoop namenode -format', then,
- start-all.sh and start-hbase.sh

Reference:
http://hadoop.apache.org/common/docs/r0.20.1/api/overview-summary.html#overview_description



回答6:

If you're looking for something that will do this in a 'one-liner' via a shell script you can use this method:

$ echo 'list.each {|t| disable t; drop t}; quit;' | hbase shell

NOTE: The above was run from Bash shell prompt. It echoes the commands into hbase shell and does a loop through all the tables that are returned from the list command, and then disables & drops each table as it iterates through the array that list returned. Once it's done, it quits.