用于执行hbase命令的Shell脚本 - 删除所有hbase表

时间:2015-06-29 21:58:58

标签: shell hbase

我想要删除HBase中的所有表。我正在使用HBase shell命令执行此操作:

$ hbase shell
 > disable_all .*
 > drop_all .*

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

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

5 个答案:

答案 0 :(得分:7)

我使用以下内容:

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

echo的-e标志使它处理转义序列,因此您可以在其中构建确认。 -n告诉hbase shell这是一个非交互式会话。

答案 1 :(得分:6)

Shell脚本:deleteHbaseTables.sh

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

答案 2 :(得分:3)

此脚本将从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 :(得分:2)

如果您使用的是带有“--non-interactive / -n”选项的hbase版本,请参阅Cloudera中的示例:

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

如果您使用的是没有“--non-interactive”的hbase 1.0.0,则可以从文件加载命令。 HBase documentation的示例:

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

答案 4 :(得分:1)

  1. 在文件中输入您的hbase命令: myfile.txt的
  2. 运行: “hbase shell< myfile.txt”

  3. 在屏幕上观察hbase输出。

  4. 或者,将所有命令输出到一个文件中: “hbase shell< myfile.txt> IwantToseeTheOutputOfThis.log”
相关问题