从 Lambda 读取胶水目录

问题描述 投票:0回答:1

是否可以将 lambda 连接到粘合数据目录以查询目录表中的数据?如果是,我该如何连接? 我正在尝试将 Glue 目录连接到 Lambda 函数来查询表,但无法找到任何相关信息。 如果有人可以提供任何资源或代码,那将会很有帮助。

amazon-web-services aws-lambda aws-glue
1个回答
0
投票

您可以使用AWS Athena查询胶水表数据。

将 Boto3 与 Python 结合使用非常简单

import boto3

def lambda_handler(event, context):
    # Set parameters for the Athena query
    query = "SELECT * FROM your_table_name"
    output_location = "s3://your-athena-query-results-bucket/query-results/"

    # Create Athena client
    athena_client = boto3.client('athena')

    # Execute Athena query
    response = athena_client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': 'your_database_name'
        },
        ResultConfiguration={
            'OutputLocation': output_location,
        }
    )

    # Get the query execution ID
    query_execution_id = response['QueryExecutionId']

    # Wait for the query to complete
    waiter = athena_client.get_waiter('query_executed')
    waiter.wait(
        QueryExecutionId=query_execution_id,
        WaiterConfig={
            'Delay': 2,
            'MaxAttempts': 30
        }
    )

    # Retrieve query results
    results = athena_client.get_query_results(
        QueryExecutionId=query_execution_id
    )

    # Process the results
    for row in results['ResultSet']['Rows']:
        print([field.get('VarCharValue', '') for field in row['Data']])

    return {
        'statusCode': 200,
        'body': 'Query executed successfully.'
    }

© www.soinside.com 2019 - 2024. All rights reserved.