11

I want to delete all tables in HBase. I am using HBase shell commands for doing this operation:

$ hbase shell
 > disable_all .*
 > drop_all .*

How can i write a shell script for doing this operations ?

Note: While executing above commands, it asks for the user confirmation i.e y/n before disabling and dropping all tables.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Vrushank Doshi
  • 2,428
  • 5
  • 20
  • 34

5 Answers5

16

I use the following:

#!/bin/sh
echo -e "disable_all '.*'\ny" | hbase shell -n
echo -e "drop_all '.*'\ny" | hbase shell -n

The -e flag to echo causes it to process escape sequences so you can build the confirmation into it. The -n tells hbase shell this is a non-interactive session.

Martin Serrano
  • 3,727
  • 1
  • 35
  • 48
  • `-n` flag is very helpful, since run commands from file and redirect the output to another file will get hbase shell blocked without this flag – LittleLittleQ Feb 23 '18 at 03:26
7

Shell Script : deleteHbaseTables.sh

#!/bin/sh
echo "disable_all .* " | hbase shell 
echo "drop_all .* " | hbase shell
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
shakirgooty
  • 134
  • 2
  • 8
  • 2
    this doesn't work because it asks for a confirmation before disabling and dropping the tables. – Vrushank Doshi Jun 30 '15 at 17:01
  • Excellent. I have seen this paradigm for interacting with the mail utility: passing commands via pipe. I had tried hbase shell "list" but did not work. – Paul Aug 28 '23 at 14:00
4

This script will fetch all the tables from HBase and perform disable and drop operation on 1 table at a time.

#Creating a temp file to store the output of "list" command.
echo "list" | hbase shell > tableListSummary.txt

#fetch only the list of tables and store it in an another temp file.
tail -1 tableListSummary.txt > tableList.txt

#Separate tables by commas and iterate to perform disable and drop commands.
cat tableList.txt | sed -n 1'p' | tr ',' '\n' | while read word; do
    com=$(echo "$word" | sed 's/[][]//g')
    table="${com#\"}"
    table="${table%\"}"
    echo $table
    echo "disable '$table'" | hbase shell
    echo "drop '$table'" | hbase shell
done

#removing temporary files
rm tableListSummary.txt tableList.txt

Thanks.

Vrushank Doshi
  • 2,428
  • 5
  • 20
  • 34
2

If you are using a hbase version with "--non-interactive / -n" option, example from Cloudera:

#!/bin/bash
echo 'list' | hbase shell -n
status=$?
if [$status -ne 0]; then
  echo "The command may have failed."
fi

If you are using hbase 1.0.0 without "--non-interactive", you can load commands from a file. Example from HBase documentation:

echo "create 'test', 'cf'" > ./sample_commands.txt
hbase shell ./sample_commands.txt
angelcervera
  • 3,699
  • 1
  • 40
  • 68
  • Hi Angelcervera, This "status" wont work because the status whatever you have mentioned actually checks the successful unix command execution not the actual hbase execution. Any given time $? checks the unix command execution. Please correct if I am wrong – Karthi Jun 07 '17 at 14:05
  • Hi @karthi I used it and I don't remember to have problems. In fact, that code is from Cloudera documentation. The link to the source is in the answer, just before the code fragment. – angelcervera Jun 07 '17 at 20:16
2
  1. input your hbase commands in a file : myfile.txt
  2. run : "hbase shell < myfile.txt"

  3. observe the hbase output on screen.

  4. alternatively , output all from the command into a file with this : "hbase shell < myfile.txt > IwantToseeTheOutputOfThis.log"