我正在尝试通过AWS CLI命令运行一组Athena SQL语句,我需要知道以下内容
SQL查询集在一个文件中,我需要通过python脚本为每个查询在以下CLI命令中顺序迭代--query-string
我可以使用FOR循环从文件中获取sql命令,但我不知道如何在AWS CLI命令中对其进行参数化。
aws athena start-query-execution \
--query-string **"select count(*) from tablename;"** \
--query-execution-context Database=test45 \
--result-configuration OutputLocation=s3://test/ \
--output text
import boto3
client = boto3.client('athena')
fd = open('Athenacli.sql', 'r')
sqlFile = fd.read()
fd.close()
sqlCommands = sqlFile.split(';')
for command in sqlCommands:
print command
response = client.start_query_execution(
QueryString=command,
QueryExecutionContext={
'Database': 'Test45'
},
ResultConfiguration={
'OutputLocation': 's3://test/'
}
)
print response
select count(*) CNT from Test45.table1;
Traceback (most recent call last):
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/Python27/mypycode/CLIFrame.py", line 18, in <module>
'OutputLocation': 's3://test/'
File "C:\Users\AppData\Roaming\Python\Python27\site-packages\botocore\client.py", line 253, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:\Users\AppData\Roaming\Python\Python27\site-packages\botocore\client.py", line 531, in _make_api_call
api_params, operation_model, context=request_context)
File "C:\Users\AppData\Roaming\Python\Python27\site-packages\botocore\client.py", line 586, in _convert_to_request_dict
api_params, operation_model)
File "C:\Users\AppData\Roaming\Python\Python27\site-packages\botocore\validate.py", line 291, in serialize_to_request
raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid length for parameter QueryString, value: 0, valid range: 1-inf
Process finished with exit code 1
答案 0 :(得分:2)
#!/bin/bash
while IFS='' read -r sql || [[ -n "$sql" ]]; do
aws athena start-query-execution \
--query-string "$sql" \
--query-execution-context Database=test45 \
--result-configuration OutputLocation=s3://test/ \
--output text
done < "$1"
您可以调用文件run_athena_query.sh
:
$ chmod +x run_athena_query.sh
$ ./run_athena_query.sh <file_with_query>
答案 1 :(得分:2)
不确定这是否是你的整个问题,但是你正在分裂“;”所以你会得到两个问题,第一个:
select count(*) CNT from Test45.table1
和第二个:
""
这可以解释错误消息:
Invalid length for parameter QueryString, value: 0, valid range: 1-inf
在错误发生前你得到任何输出吗?