我正在研究一個審計程式,以洗掉空的Hive資料庫。我有大量的資料庫需要檢查,我想在Linux中使用一個shell腳本(.sh),它可以運行hive -e查詢來識別空的資料庫,并在一些輸出檔案或日志中列出它們(不知道.txt檔案是否是一種選擇?然后我將把這個串列發送給我們的管理員,以 "放棄 "這些空資料庫。 我們所有的資料庫都遵循完全相同的命名規則。 ">>> environment_area_<state>
現在我使用的是.xt檔案。
現在我正在使用下面的查詢來完成作業,但這是非常手動的,而且非常慢...... 我最終會在Linux的命令列上花費大量的時間。
我首先在PuTTY中連接到Hive,一旦連接,我就運行:
show databases;
使用 environment_area_<state>。
顯示表。
如果資料庫中沒有顯示表,我就把它添加到我的需要洗掉的資料庫串列中。我為每個資料庫反復運行 "使用 "和 "顯示表 "查詢。
正如你所知道的,這是一個非常耗時的方法,創建一個 shell 腳本將真的有幫助。
我在網上搜索并觀看了一些 YouTube 教程,但沒有遇到可以幫助我的使用案例。 希望對shell腳本更有經驗的人能夠幫助我超越#!/bin/bash,然后是我上面列出的查詢。
uj5u.com熱心網友回復:
在后臺保留蜂巢的回答命令可能會大大改善性能:
tempdir=$(mktemp -d)
# 在執行結束后進行清理。
trap 'rm -fr -- "$tempdir"; exit' EXIT INT
hivein="$tempdir/hivein"
hiveout="$tempdir/hiveout"。
mkfifo "$hivein" "$hiveout"
# Prepare file descriptors IO to talk to hive
exec 3<>"$hivein"
exec 4<>"$hiveout"
# 在后臺啟動蜂巢。
hive -S <&3 >&4 &
# 初始化蜂巢。
printf '%s
' 'set hive.cli.print.header=false;' >&3
# 等待hive回應并獲得資料庫串列
printf '%s
' "SHOW DATABASES LIKE 'environment_area_*';" >& 3
mapfile -u 4 -t databases
empty_databases=()
for db in "${databases[@]}"/span>; do
printf 'USE %s; SHOW TABLES;
' "$db" >& 3
mapfile -u 4 -t tables
tbl_count="${#tables[@]}"
printf '資料庫%s包含%d表。
' "$db" "$tbl_count"
if [ "$tbl_count"/span> -eq 0 ]; then
# record empty db
empty_databases =("$db"/span>)
fi
done
# Close the hive-cli in case closing the file descriptors is not enough[/span]。
printf '%s
' '!exit' >&3
printf '%s
' "${empty_databases[@]}" > empty_databases_list.txt
uj5u.com熱心網友回復:
要開始做一些事情,你可以修改這個腳本。我沒有檢查過。也許show tables會回傳一些標題或額外的新行,然后相應地修改腳本(wc -l計算輸出中的新行)。
腳本:
#!/bin/bash
for db in $(hive -S -e "show databases;"/span>)
做
tbl_count=$(hive -S -e "use $db; show tables;"/span> | wc -l)
echo "資料庫$db包含$tbl_count表。"
if [ ${tbl_count} -eq 0 ]; then
# Add db name to the file
echo "$db">> empty_databases_list.txt
# Do something else, for example drop db, etc
fi
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/324958.html
標籤:
