我正在尝试从shell脚本运行参数化查询,并且我需要能够提供参数。
这是我要运行的命令:
bq --location=US query --use_legacy_sql=False \
--parameter='gender::M' \
--parameter='states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]' \
'SELECT name, sum(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE gender = @gender
AND state IN UNNEST(@states)
GROUP BY name
ORDER BY count DESC
LIMIT 10;'
如果直接复制/粘贴到外壳中,则可以使用。 现在,这就是我要运行的:
在文件 script.sh
中runQuery(){
states="'$1'"
bq --location=US query --use_legacy_sql=False --parameter='gender::M' --parameter=$states \
'SELECT name, sum(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE gender = @gender
AND state IN UNNEST(@states)
GROUP BY name
ORDER BY count DESC
LIMIT 10;'
}
runQuery 'states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]'
但这不起作用。
这是错误日志:
我很确定这与参数同时具有单引号和双引号有关,并且在处理'states:ARRAY:[“ WA”,“ WI”, “ WV”,“ WY”]',但我只是找不到解决方法。
提前谢谢!
答案 0 :(得分:0)
发现错误:
更改了runQuery 'states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]'
致runQuery 'states:ARRAY<STRING>:["WA","WI","WV","WY"]'
请注意,已从数组中删除了空格。