2015-06-29 56 views
3

我想删除HBase中的所有表。我正在使用HBase shell命令来执行此操作:用于执行hbase命令的shell脚本 - 删除所有hbase表

$ hbase shell 
> disable_all .* 
> drop_all .* 

如何编写用于执行此操作的shell脚本?

注意:在执行上述命令时,它会在禁用和删除所有表之前要求用户确认,即y/n。

+1

是否有任何方法可以强制删除此表?即不需要确认? –

+2

看起来像之前回答: http://stackoverflow.com/questions/3990952/a-script-that-deletes-all-tables-in-hbase – Shyam

+0

是的,但脚本是在Python中。我想写一个shell脚本。 –

回答

5

Shell脚本:deleteHbaseTables.sh

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

这不起作用,因为它要求在禁用之前进行确认,并且放下桌子。 –

1

该脚本将获取从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 

谢谢。

5

我使用以下命令:

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

-e标志呼应使其处理转义序列,以便您可以生成确认了进去。 -n告诉hbase shell这是一个非交互式会话。

+0

'-n'标志是非常有用的,因为从文件运行命令并将输出重定向到另一个文件将获得hbase外壳阻塞没有这个标志 – AnnabellChan

2

如果您使用的是带有 “--non交互式/ -n” 选项,例如HBase的版本从Cloudera

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

如果您正在使用HBase的1.0.0没有“--non交互式“,你可以从文件加载命令。来自HBase documentation的示例:

echo "create 'test', 'cf'" > ./sample_commands.txt 
hbase shell ./sample_commands.txt 
+0

嗨Angelcervera,这种“状态”不会工作,因为无论你提到的状态实际上检查成功的unix命令执行不是实际的hbase执行。任何给定的时间$?检查unix命令的执行情况。请纠正,如果我错了 – Karthi

+0

嗨@karthi我用它,我不记得有问题。实际上,该代码来自Cloudera文档。源代码的链接位于代码片段之前的答案中。 – angelcervera