![](/img/trans.png)
[英]Python boto3 AWS Dynamodb table Query & Scan methods on 'Client' object vs 'Resource' object
[英]Athena query executed through boto3 python client gives smaller result compared to query executed through AWS cli
我想通過 Athena 執行一個非常簡單的查詢。
查詢: select * from information_schema.tables
當我使用帶有以下代碼的 boto3 客戶端執行查詢時:
...
def run_query(query_string):
query_execution_context = {"Catalog": "awsdatacatalog", "Database": "information_schema"}
response = athena_client.start_query_execution(
QueryString=query_string, QueryExecutionContext=query_execution_context, WorkGroup="primary"
)
return response
query_string_get_tables = "select * from information_schema.tables"
response = run_query(query_string_get_tables)
我在 0.6 秒內得到了 9 行的結果。
當我然后 go 到 AWS 控制台並重新運行相同的查詢時,我在 6 秒內得到了 500 行的結果。
AWS 控制台的結果是正確的。 如何使用 boto3 客戶端獲得相同的結果?
編輯:我下載了查詢歷史並比較了查詢字符串。 如您所見,它們完全相同。 我還在 boto3 客戶端調用中刪除了 QueryExecutionContext ,但這並沒有改變任何東西。 此外,我嘗試了所有單引號/雙引號的組合。
查詢歷史:
37b72ac5-3223-496f-8293-79eab8a661a0,select * from information_schema.tables,2022-12-02T18:23:09.738-08:00,SUCCEEDED,6.503 sec,39.01 KB,Athena engine version 2,'-
9d3a274a-8109-4988-aaf8-bba9c8733208,select * from information_schema.tables,2022-12-02T18:14:11.385-08:00,SUCCEEDED,520 ms,0.67 KB,Athena engine version 2,'-
正如評論中提到的,使用start_query_execution
需要一些努力來啟動查詢執行,等待其完成,然后get_query_results
( https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/athena.html#Athena。客戶端.get_query_results )。
為了讓您的生活更輕松,您可以使用開源庫 AWSWrangler 或AWS-SDK-Pandas 。 使用此庫可以以阻塞方式獲取結果:
# Retrieving the data from Amazon Athena
df = wr.athena.read_sql_query("SELECT * FROM my_table", database="my_db")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.