简体   繁体   English

使用 AWS lambda 在 Athena 上运行查询

[英]Using AWS lambda to run query on Athena

I have a very simple table on AWSAthena with three column: name, city and price.我在 AWSAthena 上有一个非常简单的表,其中包含三列:名称、城市和价格。 I can run the following simple query:我可以运行以下简单查询:

select * from mytestdb.test where city='austin'

and the running time is less than 1 second.并且运行时间小于1秒。 I create a lambda function using boto3 to run the same query however the query can not be finished after 3 mins我使用 boto3 创建了一个 lambda 函数来运行相同的查询,但是查询无法在 3 分钟后完成

import time
import boto3

# athena constant
DATABASE = 'mytestdb'
TABLE = 'test'
# S3 constant
output='s3://mybucket'


COLUMN = 'city'

def lambda_handler(event, context):
    # get keyword
    keyword = 'Austin'
    # created query
    query = "SELECT * FROM %s.%s where %s = '%s';" % (DATABASE, TABLE, COLUMN, keyword)
    # athena client
    client = boto3.client('athena')


    # Execution
    response = client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': DATABASE
        },
        ResultConfiguration={
            'OutputLocation': output,
        }
    )

    # get query execution id
    query_execution_id = response['QueryExecutionId']
    print(query_execution_id)

    # get execution status


    # get query execution

    query_status = client.get_query_execution(QueryExecutionId=query_execution_id)
    print('Amir2')
    query_execution_status = query_status['QueryExecution']['Status']['State']


    time.sleep(200)        

    if query_execution_status == 'SUCCEEDED':
        result = client.get_query_results(QueryExecutionId=query_execution_id)
    else:
        print('killed')
        client.stop_query_execution(QueryExecutionId=query_execution_id)

    return

the table has total 10 rows so it is not large at all.该表总共有 10 行,所以它一点也不大。 Is the right way to lookup for the desirable value based on city or there is better way to do that?是基于城市查找理想价值的正确方法还是有更好的方法来做到这一点?

UPDATE: here is my lambda permission (It has full access to athena and s3:更新:这是我的 lambda 权限(它具有对 athena 和 s3 的完全访问权限:

{   "Version": "2012-10-17",   "Statement": [
    {
      "Sid": "Stmt1547414166585",
      "Action": [
        "athena:*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Sid": "Stmt1547414166586",
      "Action": [
        "s3:*"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::your-bucket-name/*"
    }    ] }

A little late here but make sure the Role for your lambda has the correct policies.这里有点晚了,但请确保您的 lambda 角色具有正确的策略。

I solved it by giving full access to not only Athena but also Glue which you may be missing.我通过不仅可以完全访问 Athena 还可以完全访问您可能缺少的 Glue 来解决它。 Add those policies to your lambda role.将这些策略添加到您的 lambda 角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM