删除所有hbase表

vwkv1x7d  于 2021-06-09  发布在  Hbase
关注(0)|答案(5)|浏览(523)

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

$ hbase shell
 > disable_all .*
 > drop_all .*

如何编写执行此操作的shell脚本?
注意:在执行上述命令时,它要求用户确认,即在禁用和删除所有表之前是/否。

fv2wmkja

fv2wmkja1#

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

如果您使用的hbase 1.0.0没有“--非交互式”,则可以从文件中加载命令。hbase文档示例:

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

mlmc2os52#

shell脚本:deletehbasetables.sh


# !/bin/sh

echo "disable_all .* " | hbase shell 
echo "drop_all .* " | hbase shell
xbp102n0

xbp102n03#

我使用以下方法:


# !/bin/sh

echo -e "disable_all '.*'\ny" | hbase shell -n
echo -e "drop_all '.*'\ny" | hbase shell -n

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

i1icjdpr

i1icjdpr4#

在一个文件中输入hbase命令:myfile.txt
运行:“hbase shell<myfile.txt”
观察屏幕上的hbase输出。
或者,将命令中的所有内容输出到一个文件中:“hbase shell<myfile.txt>i想要查看this.log的输出”

wooyq4lh

wooyq4lh5#

此脚本将从hbase获取所有表,并一次对一个表执行disable和drop操作。


# 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

谢谢。

相关问题