Shell script to execute hbase commands - Removing all hbase tables

I want to delete all tables in HBase. I use the HBase shell commands to perform this operation:

$ hbase shell
 > disable_all .*
 > drop_all .*

How can I write a shell script to perform these operations?

Note. When executing the above commands, it asks for user confirmation ie y / n before disconnecting and deleting all tables.

+4
source share
4 answers

Shell Script: deleteHbaseTables.sh

#!/bin/sh
echo "disable_all .* " | hbase shell 
echo "drop_all .* " | hbase shell
+6
source

I am using the following:

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

The flag -efor the echo forces it to process escape sequences so that you can create a confirmation in it. -ntells the hbase shell this is a non-interactive session.

+5
source

script HBase 1 .

#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

.

+3
source

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

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

If you use 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
+2
source

All Articles