简体   繁体   English

通过 Lambda 运行 aws Athena 查询:未定义错误名称“response”

[英]Run aws Athena query by Lambda: error name 'response' is not defined

I create an AWS lambda function with python 3.9 to run the Athena query and get the query result我创建了一个 AWS lambda function 和 python 3.9 来运行 Athena 查询并获取查询结果

import time
import boto3

# create Athena client

client = boto3.client('athena')

# create Athena query varuable

query = 'select * from mydatabase.mytable limit 8'
DATABASE = 'mydatabase'
output='s3://mybucket/'

def lambda_handler(event, context):
    # Execution
    response = client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': DATABASE
        },
        ResultConfiguration={
            'OutputLocation': output,
        }
    )
    
query_execution_id = response['QueryExecutionId']
    
time.sleep(10)
    
result = client.get_query_results(QueryExecutionId=query_execution_id)
    
for row in results['ResultSet']['Rows']:
    print(row)

I get this error message when I test it "[ERROR] NameError: name 'response' is not defined"我在测试时收到此错误消息“[ERROR] NameError:未定义名称'response'”

You define the response variable inside the lambda_handler function. But you are referencing it in the global scope, outside of that function, here:您在lambda_handler function 中定义response变量。但您在全局 scope 中引用它,在 function 之外,此处:

query_execution_id = response['QueryExecutionId']

The variable isn't defined on that scope, thus the error message.该变量未在该 scope 上定义,因此出现错误消息。 It appears that you may simply be missing indentation on all these lines:看来您可能只是缺少所有这些行的缩进:

query_execution_id = response['QueryExecutionId']
    
time.sleep(10)
    
result = client.get_query_results(QueryExecutionId=query_execution_id)
    
for row in results['ResultSet']['Rows']:
    print(row)

In Python indentation is syntax !在 Python 缩进是语法 If you intend those lines to be part of the lambda_handler function, then they need to have the correct indentation to place them inside the scope of the function, like so:如果您希望这些行成为lambda_handler function 的一部分,那么它们需要有正确的缩进才能将它们放在 function 的 scope 中,如下所示:

def lambda_handler(event, context):
    # Execution
    response = client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': DATABASE
        },
        ResultConfiguration={
            'OutputLocation': output,
        }
    )
    
    query_execution_id = response['QueryExecutionId']
    
    time.sleep(10)
    
    result = client.get_query_results(QueryExecutionId=query_execution_id)
    
    for row in results['ResultSet']['Rows']:
        print(row)

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

相关问题 Aws Wrangler 给出未实现的错误:无法查询 aws athena - Aws Wrangler giving not implemented error: impossible to query aws athena NameError:未定义名称“athena”,从另一个 jupyter notebook 导入 athena 查询 function 时 - NameError: name 'athena' is not defined , when importing athena query function from another jupyter notebook 在 Go SDK 等待 AWS Athena 查询执行 - Wait for AWS Athena query execution in Go SDK AWS Athena 查询 JSON 数组与 AND 条件 - AWS Athena query JSON array with AND Condition 在等待数据库查询之前返回 AWS Lambda 响应 - 无服务器框架 - Returning AWS Lambda response before waiting for DB query - Serverless framework AWS Athena 联合查询 - 为 PostgreSQL 运行数据库查询时出现 GENERIC_USER_ERROR - AWS Athena Federated Query - GENERIC_USER_ERROR when running DB query for PostgreSQL aws athena查询json数组数据 - aws athena query json array data 如何使用 AWS ATHENA 查询填充数据表或数据集? - How to fill a datatable or dataset with an AWS ATHENA query? AWS Athena MSCK REPAIR TABLE“table_name”添加新分区时出错 - AWS Athena MSCK REPAIR TABLE "table_name" Error adding new partitions AWS LAMBDA api 网关错误“格式错误的 Lambda 代理响应”状态错误 502 - AWS LAMBDA api gateway error “Malformed Lambda proxy response” status error 502
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM