Shell脚本中Impala查询因`date`解析问题执行失败求助
问题解决:Shell脚本中Impala查询的反引号解析错误
问题原因
你的脚本里,SQL中的date字段用反引号包裹,但Shell会把反引号识别为命令替换语法,自动执行date命令并将结果(比如Tue Jan 17 02:18:20 EST 2023)替换到SQL语句中,导致Impala收到的SQL出现语法错误。
解决方案
有两种简单的修复方式:
1. 转义SQL中的反引号
在每个反引号前添加反斜杠\,让Shell将其当作普通字符传递给Impala,而不是执行命令替换。
修改后的脚本:
impala-shell -B -i $IMPALA_CON --ssl -q "use db_test ; select concat(substring(\`date\`,3,2),substring(\`date\`,6,2),substring(\`date\`,9,2)) id from db_test.date_d where \`date\`<=cast(current_timestamp() as string)" -o $FILE/abc.txt '--output_delimiter=,'
2. 用单引号包裹SQL语句
Shell不会解析单引号内的命令替换逻辑,如果不需要在SQL里解析Shell变量,直接用单引号把整个SQL括起来,就能保留Impala需要的反引号语法。如果需要保留Shell变量的解析,单独给变量加双引号即可:
修改后的脚本:
impala-shell -B -i "$IMPALA_CON" --ssl -q 'use db_test ; select concat(substring(`date`,3,2),substring(`date`,6,2),substring(`date`,9,2)) id from db_test.date_d where `date`<=cast(current_timestamp() as string)' -o "$FILE/abc.txt" '--output_delimiter=,'
这样Impala就能正确识别date作为字段名,不会出现语法错误。
内容的提问来源于stack exchange,提问作者John
相关产品推荐
相关产品推荐

