使用shell脚本运行linux配置单元查询

hgb9j2n6  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(473)

我有一个Hive查询,工作正常。但是当我想在shell脚本中运行它时,我无法使它工作。
下面是shell脚本。


# !/bin/bash

start_date='2019-08-01'
end_date='2019-08-31'

while IFS='|' read -r table_name val;
do
     hive -e "set hive.cli.print.header=true;select source_to_date, target_count from testing.log_final where project_name= '${val}' and source_to_date between '${start_date}' and '${end_date}' order by source_to_date;" | sed 's/[\t]/,/g'  > /x/home/SUER/btree/"${table_name}".csv

done < /x/home/"$USER"/bt_tables.txt

的内容 bt_tables.txt :

merchants|102_merchants_project
payments|103_payments_project

运行良好的查询:

hive -e "set hive.cli.print.header=true;select source_to_date, target_count from testing.log_final where project_name= '102_merchants_project' and source_to_date between '2019-08-01' and '2019-08-31' order by source_to_date;" | sed 's/[\t]/,/g'  > /x/home/SUER/btree/merchants.csv

我做错什么了?

798qvoo8

798qvoo81#

您给定的命令可能没有问题,但您正在输出文件中重定向,而不是附加。
看男人狂欢

Redirecting Output
   Redirection  of output causes the file whose name results from the expansion of word to be opened for writing
   on file descriptor n, or the standard output (file descriptor 1) if n is not specified.  If the file does not
   exist it is created; if it does exist it is truncated to zero size.

   The general format for redirecting output is:

          [n]>word

可以将append redirect output与>>一起使用,也可以在while执行结束后写入文件

Appending Redirected Output
   Redirection of output in this fashion causes the file whose name results from the expansion  of  word  to  be
   opened  for appending on file descriptor n, or the standard output (file descriptor 1) if n is not specified.
   If the file does not exist it is created.

   The general format for appending output is:

          [n]>>word

就你而言:

while IFS='|' read -r table_name val;
do
     hive -e "cmd"  >> /x/home/SUER/btree/"${table_name}".csv
done < /x/home/"$USER"/bt_tables.txt

或者

while IFS='|' read -r table_name val;
do
     hive -e "cmd"
done < /x/home/"$USER"/bt_tables.txt >/x/home/SUER/btree/"${table_name}".csv

ihth公司

相关问题