linux - 从 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 导入语句,以 ; 结尾。
解决方案
您可以在一行 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")
}
推荐阅读
- xml - 从不在同一行的xml标签中提取数据
- php - 使用PHP:从给定点查找多边形中最近的点
- matlab - 在轮廓图上覆盖等值线
- r - 将数据帧从宽格式转换为长格式,密钥存储在 R 行中
- spring-integration - 发布订阅流后从入站适配器移动文件
- node.js - 捕获错误 401 adonis.js 错误 E_INVALID_SESSION: Invalid session
- angular - 使用 Ahead-of-Time 编译时如何更改 Angular 的插值字符?
- machine-learning - PyTorch 中的 torch.optim.SGD 结果为 NaN
- google-kubernetes-engine - 每个客户是否应该拥有自己的 Kubernetes 集群
- php - Laravel - 使用表单更新多行值