我有一个蜂巢查询工作正常。但是当我想在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
我在这里做错什么了?
最佳答案
给定的命令可能可以,但您正在输出文件中重定向,而不是追加。
看人肉
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
您可以使用带>>的追加重定向输出,或在执行结束后写入文件
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
第1页
关于linux - 使用Shell脚本运行Hive查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57966316/