正如 Chris Pollard 所说,您可以使用 boto3 从 Lambda 函数查询 Athena。
http://boto3.readthedocs.io/en/latest/reference/services/athena.html http://boto3.readthedocs.io/en/latest/reference/services/athena.html
初始化 Athena 客户端:
import boto3
client = boto3.client('athena')
然后您将执行您的查询:
queryStart = client.start_query_execution(
QueryString = 'SELECT * FROM myTable',
QueryExecutionContext = {
'Database': 'myDatabase'
},
ResultConfiguration = { 'OutputLocation': 's3://your-bucket/key'}
)
如果您想在 Lambda 中检索结果(由于时间限制,可能使用第二个函数 - 请参阅docs https://docs.aws.amazon.com/lambda/latest/dg/limits.html#limits-list- 另请注意,您按 100 毫秒的运行时间付费),您将使用get_query_execution
确定查询的状态:
queryExecution = client.get_query_execution(QueryExecutionId=queryStart['QueryExecutionId'])
您需要解析返回的对象的值QueryExecution.Status.State
场地。使用继续更新对象get_query_execution()
直到结果为Succeeded
.
注意:请勿致电get_query_execution()
在连续循环中。相反,使用指数退避算法 https://docs.aws.amazon.com/general/latest/gr/api-retries.html以防止被该 API 限制。您应该对所有 API 调用使用此方法。
然后你可以使用get_query_results()
检索结果进行处理:
results = client.get_query_results(QueryExecutionId=queryStart['QueryExecutionId'])