python - 如何使用 python 中的 lambda 函数在通过 S3 连接的 AWS 雅典娜中进行查询
问题描述
我将 .csv 文件保存在 S3 存储桶中。我可以使用 AWS Athena 查询 S3 的数据。有什么方法可以将 lambda 函数连接到 athena 并从 lambda 函数查询数据。请帮忙
谢谢
解决方案
就像 Chris Pollard 所说,您可以使用 boto3 从 Lambda 函数中查询 Athena。
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 中检索结果(可能使用第二个函数,由于时间限制 - 请参阅文档- 另请注意,您按 100 毫秒运行时间付费),您将使用get_query_execution
来确定查询的状态:
queryExecution = client.get_query_execution(QueryExecutionId=queryStart['QueryExecutionId'])
您将需要解析返回的对象以获取该QueryExecution.Status.State
字段的值。使用 继续更新对象,get_query_execution()
直到结果为Succeeded
.
注意:请不要get_query_execution()
连续循环调用。相反,使用指数退避算法来防止被该 API 限制。您应该将此方法用于所有 API 调用。
然后您可以使用get_query_results()
检索结果进行处理:
results = client.get_query_results(QueryExecutionId=queryStart['QueryExecutionId'])
推荐阅读
- github - AWS CodeBuild Github webhook 不更新 PullRequest 的状态
- html - VBA在没有名称/ ID的IE中单击下拉链接
- http - craigslist 之类的网站如何根据您的计算机所在的城市创建内容
- java - 使用 Camel 中的查询字符串对 REST Web 服务进行 HTTP GET 调用
- jetty - 如何仅允许本地主机的码头服务器状态信息
- c++ - 时间:2019-04-10 标签:c++socketacceptblockscout
- c# - 作为 Windows 服务运行时的 C# 内存泄漏,而不是作为控制台应用程序运行时
- python - Pandas:如果日期时间索引中的日期不同,则从第一个值开始的百分比变化
- python - 如何让 2 个类实例相互引用?
- java - 从另一个可运行的应用程序更改 JList