【问题标题】:Shell script for executing hbase commands - Deleting all hbase tables用于执行 hbase 命令的 shell 脚本 - 删除所有 hbase 表
【发布时间】:2015-06-29 21:58:58
【问题描述】:

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

$ hbase shell
 > disable_all .*
 > drop_all .*

我如何编写一个 shell 脚本来执行这些操作?

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

【问题讨论】:

标签: shell hbase


【解决方案1】:

我使用以下:

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

-e 标志回显会导致它处理转义序列,以便您可以在其中构建确认。 -n 告诉 hbase shell 这是一个非交互式会话。

【讨论】:

  • -n 标志非常有用,因为从文件运行命令并将输出重定向到另一个文件会在没有此标志的情况下阻止 hbase shell
【解决方案2】:

Shell 脚本:deleteHbaseTables.sh

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

【讨论】:

  • 这不起作用,因为它在禁用和删除表之前要求确认。
【解决方案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

谢谢。

【讨论】:

    【解决方案4】:

    如果您使用带有“--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
    

    【讨论】:

    • 您好 Angelcervera,此“状态”不起作用,因为您提到的状态实际上检查的是成功的 unix 命令执行,而不是实际的 hbase 执行。任何给定的时间$?检查 unix 命令的执行。如有错误请指正
    • 嗨@karthi 我用过它,但我不记得有问题。事实上,该代码来自 Cloudera 文档。源链接在答案中,就在代码片段之前。
    【解决方案5】:
    1. 在文件中输入您的 hbase 命令: 我的文件.txt
    2. 运行: "hbase shell

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

    4. 或者,将命令中的所有内容输出到一个文件中: "hbase shell IwantToseeTheOutputOfThis.log"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-28
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2017-12-23
      • 1970-01-01
      相关资源
      最近更新 更多