简体   繁体   English

Python 使用 Athena 的 boto3 参数化查询

[英]Python boto3 parameterized queries with Athena

I cant seem to find the document on how to pass execution parameters to Athena using boto3.我似乎找不到有关如何使用 boto3 将执行参数传递给 Athena 的文档。 I did find how to do it using aws cli, like so:我确实找到了如何使用 aws cli 来做到这一点,如下所示:

aws athena start-query-execution 
--query-string "SELECT * FROM table WHERE x = ? AND y = ?"
--query-execution-context "Database"="default" 
--result-configuration "OutputLocation"="s3://..."
--execution-parameters "1" "2"

Is there a way to do the same using boto3 with something like:有没有办法使用 boto3 和类似的东西来做同样的事情:

import boto3

client = boto3.client(
            "athena",
            aws_access_key_id=XXX,
            aws_secret_access_key=YYY,
            region_name=ZZZ,
)
response = client.start_query_execution(
            QueryString="SELECT * FROM table WHERE x = ? AND y = ?",
            QueryExecutionContext={"Database": "default"},
            ResultConfiguration={"OutputLocation": "s3://..."},
            WorkGroup=self._kwargs.get('workgroup'),
)

Is it possible to do it with boto3 without using prepared statements?是否可以在不使用准备好的语句的情况下使用 boto3 来做到这一点?

You can also use awswrangler to do it very simply:您还可以使用awswrangler非常简单地完成此操作:

import awswrangler as wr
df = wr.athena.read_sql_query(
    sql="SELECT * FROM table WHERE x=:x; AND y=:y;",
    params={"x": "'x_value'", "y": "'y_value'"}
)

To read more how the read_sql_query function works and list of params: https://aws-sdk-pandas.readthedocs.io/en/stable/stubs/awswrangler.athena.read_sql_query.html要了解更多read_sql_query function 的工作原理和参数列表: https://aws-sdk-pandas.readthedocs.io/en/stable/stubs/awswrangler.athena.read_sql_query.html

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

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