aws使用aws wrangler从athena错误中读取数据

aws read data from athena error using aws wrangler

我正在使用 python3

我正在尝试使用 awswrangler 包从 aws athena 读取数据。

下面是代码

import boto3
import awswrangler as wr
import pandas as pd

df_dynamic=wr.athena.read_sql_query("select * from test",database="tst")

错误:

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/awswrangler/_config.py", line 361, in wrapper

       File "/usr/local/lib/python3.6/site-packages/botocore/regions.py", line 148, in _ 
   endpoint_for_partition
     raise NoRegionError()
      botocore.exceptions.NoRegionError: You must specify a region.

我不确定要指定和在哪里才能使 sql 查询正常工作

与 AWS API 的所有交互(包括通过 SDK,如 boto3)都需要凭据,您可以找到有关 boto3 如何管理凭据的更多信息 here

由于您在 EC2 实例上 运行,最佳实践建议通过实例配置文件管理凭据。假设您已经为 EC2 实例分配了一个 IAM 角色,您需要做的就是为您的代码指定一个区域。您可以在官方 AWS documentation.

上找到有关如何为您的 EC2 分配 IAM 角色的信息

AWS Data Wrangler 依赖于 boto3 并允许像这样指定区域:

boto3.setup_default_session(region_name="us-east-2")

来源:AWS Data Wrangler - Sessions

您可以像上面的示例一样对区域进行硬编码,或者您可以使用 instance metadata endpoint.

检索部署了 EC2 的区域

以下端点:

curl http://169.254.169.254/latest/dynamic/instance-identity/document

将 return json 包含 EC2 区域等信息:

{
  "privateIp" : "172.31.2.15",
  "instanceId" : "i-12341ee8",
  "billingProducts" : null,
  "instanceType" : "t2.small",
  "accountId" : "1234567890",
  "pendingTime" : "2015-11-03T03:09:54Z",
  "imageId" : "ami-383c1956",
  "kernelId" : null,
  "ramdiskId" : null,
  "architecture" : "x86_64",
  "region" : "ap-northeast-1", # <- region
  "version" : "2010-08-31",
  "availabilityZone" : "ap-northeast-1c",
  "devpayProductCodes" : null
}

您可以在 Python 中或根据需要通过其他方式轻松实现此请求。