如何解决雅典娜查询执行结果后如何获取S3网址
我正在尝试将雅典娜查询结果存储在S3存储桶中。 我能够将查询结果存储在S3中,并且可以在S3中看到数据。 但是我需要在查询执行回调之后返回存储数据的S3 URL。
请在下面找到我的代码
const getQueryResults = async (QueryExecutionId) => {
await sleep(5000);
const { QueryExecution } = await athena
.getQueryExecution({ QueryExecutionId })
.promise();
if (
QueryExecution.Status.State !== "SUCCEEDED" &&
QueryExecution.Status.State !== "FAILED" &&
QueryExecution.Status.State !== "CANCELLED"
) {
console.log(`Not ready yet: ${JSON.stringify(QueryExecution,null,2)}`);
return getQueryResults(QueryExecutionId);
}
console.info(`(${QueryExecutionId}) Results ready,download started`);
const data = await athena.getQueryResults({ QueryExecutionId }).promise();
**// **Here I need to get S3 url instead of results rows instead of data.ResultSet.Rows****
return data.ResultSet.Rows;
};
解决方法
没有API调用可直接给出此信息。但是您可以通过将查询运行的年,月和日附加到输出位置来获取确切的输出CSV文件。
- 使用脚本中的getQueryExecution()获取输出位置,您已经具有以下内容:
s3://aws-athena-query-results-XXXXXXXXX-us-west-2/
- 如果您查询的不是命名查询,则以其他方式追加未保存的查询名称,如下所示:
s3://aws-athena-query-results-XXXXXXXXX-us-west-2/Unsaved
->如果未命名查询
s3://aws-athena-query-results-XXXXXXXXX-us-west-2/<QueryName>
- 现在只需通过获取当前日期并对其进行解析即可将年,月和日追加到您的输出位置,这样应该会有所帮助。
s3://aws-athena-query-results-XXXXXXXXX-us-west-2/2020/07/22/
- 作为最后一步,您需要将查询ID末尾连接.CSV,如下所示:
s3://aws-athena-query-results-XXXXXXXXXX-us-west-2/Unsaved/2020/07/22/<query_id>.csv
现在,您拥有由查询生成的确切S3文件。
,查询执行响应包含查询完成时结果的S3 URI。当QueryExecution.Status.State
为SUCCEEDED
时,QueryExecution.ResultConfiguration.OutputLocation
将包含URI。
如果您只需要CSV,则无需进行getQueryResults
API调用。
下面是一个基于您的代码的示例,该示例返回结果URI:
const getQueryResultsUri = async (QueryExecutionId) => {
const {QueryExecution} = await athena.getQueryExecution({QueryExecutionId}).promise()
const state = QueryExecution.Status.State
while (state !== "SUCCEEDED" && state !== "FAILED" && state !== "CANCELLED") {
await sleep(5000)
}
if (state === "SUCCEEDED") {
return QueryExecution.ResultConfiguration.OutputLocation
} else {
throw new Error("Execution failed or was cancelled")
}
}
我还重写了它,以使用循环而不是递归。