首页 > 解决方案 > 从 shell 脚本执行 sqoop 语句

问题描述

我有一个如下所示的输入文件。在第 1 列中具有表名并且在该行中剩下的部分是以 ; 结尾的 sqoop 语句。我必须循环表并运行每个 sqoop 语句。我能够读取表格,但由于 sqoop 语句有空格,我无法将整个语句读取为 coulmn2。有人能帮忙吗。

input file
tbl1|sqoop import --options file /location --table tbl1 --target-dir /location --hiveimport ;
tbl2|sqoop import --options file /location --table tbl2 --target-dir /location --hiveimport --compression;
tbl3|sqoop import --options file /location --table tbl3 --target-dir /location --hiveimport --compression;

现在我能够读取输入文件的第一列,但我不知道如何将整个 sqoop 语句读取为一个。下面是我尝试过但没有奏效的方法。

while read line; do
tbl_name=$(echo "$line"|awk 'BEGIN{FS=","}{print $1}'}
echo "tbl name is $tbl_name" >> "$tbl_name".log
tablestring=${line#"$tbl_name")
for sqoop_statement in ${tablestring//;/ }; do
echo "$sqoop_statement" >> "$tbl_name".log
echo "sqoop statement executed successfully for  $tbl_name" >> "$tbl_name".log
done
done < input.txt

注意:输入文件是自己创建的,可以修改,输入文件中的每一行都有表名,然后是管道分隔符,然后是 sqoop 导入语句,以 ; 结尾。

标签: linuxshellunixscriptingsqoop

解决方案


您可以在一行 awk 中实现您正在做的事情。

awk -F"|" '{print "running the command for "$1"\n"$2" >> "$1".log"; st=system($2">>"$1".log");print (st==0?"Success\n":"Failure\n")}' input.txt

扩展版

{
    print "running the command for " $1 "\n" $2 " >> " $1 ".log"
    st = system($2 ">>" $1 ".log")
    print (st == 0 ? "Success\n" : "Failure\n")
}

推荐阅读