如何在使用Lambda和Boto3时在Athena中分页查询。

huangapple go评论77阅读模式
英文:

how do i paginate my query in Athena using Lambda and Boto3

问题

我正在使用Boto3从Lambda查询Athena中的数据。我的结果是JSON格式。当我运行我的Lambda函数时,我获得整个记录。现在如何分页这些数据。我只想每页获取少量数据,然后将这个小数据集发送到UI上显示。

以下是我的Python代码:

def lambda_handler(event, context):
    athena = boto3.client('athena')
    s3 = boto3.client('s3')
    query = event['query']
    # 执行查询
    query_id = athena.start_query_execution(
        QueryString=query,
        QueryExecutionContext={'Database': DATABASE},
        ResultConfiguration={'OutputLocation': output}
    )['QueryExecutionId']

我使用Postman传递我的查询以获取数据,并且我知道SQL查询中的LIMIT和OFFSET,但想知道是否有其他更好的方法在我的函数中传递LIMIT和OFFSET参数。请在这种情况下帮助我。谢谢。

英文:

I am querying my data in Athena from lambda using Boto3.
My result is json format.
when I run my lambda function I get the whole record.
Now how can I paginate this data.
I only want to get fewer data per page and
send that small dataset to the UI to display.

Here is my Python code:

def lambda_handler(event, context):
    athena = boto3.client('athena')
    s3 = boto3.client('s3')
    query = event['query']
    # Execution  
    query_id = athena.start_query_execution(
        QueryString=query,
        QueryExecutionContext={'Database': DATABASE},
        ResultConfiguration = {'OutputLocation': output}
    )['QueryExecutionId']

I use postman to pass my query to get data and
I am aware of the SQl query LIMIT and OFFSET
but want to know if there is any other better way to pass LIMIT and OFFSET parameter in my function.
Please help me in this case.

Thanks.

答案1

得分: 1

A quick google search and found this answer in the Athena docs, which seems to be promising. Example from the docs

response_iterator = paginator.paginate(
    QueryExecutionId='string',
    PaginationConfig={
        'MaxItems': 123,
        'PageSize': 123,
        'StartingToken': 'string'
    })

I hope this helps!

英文:

A quick google search and found this answer in the Athena docs, which seems to be promising. Example from the docs

response_iterator = paginator.paginate(
QueryExecutionId='string',
PaginationConfig={
    'MaxItems': 123,
    'PageSize': 123,
    'StartingToken': 'string'
})

I hope this helps!

huangapple
  • 本文由 发表于 2023年1月9日 10:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定