我正在嘗試創建一個 shell 腳本,它將從多個資料庫中提取所有表中的行數。所有資料庫都遵循相同的命名約定“the_same_databasename_<%>”,除了名稱中的最后一層,它有所不同。我正在嘗試運行以下命令:
use <database_name>;
show tables;
select count(*) from <table_name>;
由于我有 40 個不同的資料庫,我需要為每個資料庫運行前兩個查詢 40 次不同的時間,再加上選擇計數查詢,具體取決于資料庫中有多少表(非常耗時)。我將 PuTTy 配置設定設定為將 PuTTy 會話保存到本地目錄中的 .txt 檔案中,因此我可以在命令列界面中直接顯示行計數結果。到目前為止,這是我所擁有的,但不確定如何包含最終命令以從每個資料庫中的表中獲取實際行數。
#!/bin/bash
for db in $(hive -e "show databases like 'the_same_databasename_*;")
do
tbl_count=$(hive -S -e "use $db; show tables;" | wc -l)
echo "Database $db contains $tbl_count tables."
done
我在 shell 腳本方面不是很有經驗,所以非常感謝任何指導/幫助。提前致謝。
uj5u.com熱心網友回復:
您可以使用嵌套的 for 回圈:
#!/bin/bash
for db in $(hive -e "show databases like 'the_same_databasename_*;")
do
tbl_count=$(hive -S -e "use $db; show tables;" | wc -l)
echo "Database $db contains $tbl_count tables."
for table in $(hive -S -e "use $db; show tables;")
do
count=$(hive -S -e "use $db; select count(*) from $table;")
echo "Table $db.$table contains $count rows."
done
done
或者您可以使用變數來增加表的計數
#!/bin/bash
for db in $(hive -e "show databases like 'the_same_databasename_*;")
do
tbl_count=0
for table in $(hive -S -e "use $db; show tables;")
do
(( tbl_count ))
count=$(hive -S -e "use $db; select count(*) from $table;")
echo "Table $db.$table contains $count rows."
done
echo "Database $db contains $tbl_count tables."
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363918.html
上一篇:如何直接在/usr/bin中安裝python3.8?
下一篇:跟蹤。任務與執行緒ID相同嗎?
