【问题标题】:AWS Wrangler error with establishing engine connection in Python, must specify a region?在 Python 中建立引擎连接时出现 AWS Wrangler 错误,必须指定区域吗?
【发布时间】:2020-10-20 17:54:16
【问题描述】:

这可能是一个简单的修复,但我无法运行此代码。我一直在使用 AWS Secrets Manager,在 Pycharm 2020.2.3 上没有任何问题。然而,AWS Wrangler 的问题如下:

读入数据框

test_df = pd.read_csv(source, encoding='latin-1')

检查 df 数据类型

data_types_df = test_df.dtypes
print('Data type of each column of Dataframe:')
print(data_types_df)

将列转换为正确的数据类型

test_df['C'] = pd.to_datetime(test_df['C'])

test_df['E'] = pd.to_datetime(test_df['E'])

检查 df 数据类型

df_new = test_df.dtypes
print('Data type of each column of Dataframe:')
print(df_new)

我已经尝试了下面的两个 sn-ps,我得到了同样的错误:

engine = wr.catalog.get_engine("aws-data-wrangler-redshift", region_name=region_name)

engine = wr.catalog.get_engine('redshift+psycopg2://' + Username + ":" + Password + ClusterURL)

错误:

botocore.exceptions.NoRegionError: You must specify a region.

然后我将尝试使用以下两种方法之一将 Pandas Dataframe 转换为 redshift 中的自定义表:

path = f"s3://{bucket}/stage/"
iam_role = 'ARN'

复制df到redshift自定义表

wr.db.copy_to_redshift(
    df=df_new,
    path=path,
    con=engine,
    schema="custom",
    table="test_df",
    mode="overwrite",
    iam_role=iam_role,
    primary_keys=["c"]
)

熊猫 df 到红移

wr.pandas.to_redshift(
    dataframe=df_new,
    path=path,
    schema="custom",
    table="test_df",
    connection=con,
    iam_role="YOUR_ROLE_ARN",
    mode="overwrite",
    preserve_index=False
)

任何帮助将不胜感激:)

【问题讨论】:

    标签: python pandas amazon-web-services amazon-redshift


    【解决方案1】:

    Data Wrangler 在后台使用 Boto3。 Boto3 将寻找 AWS_DEFAULT_REGION 环境变量。所以你有两个选择:

    在您的~/.aws/config 文件中设置:

    [default]  
    region=us-east-1
    

    或者在您的 PC 中将此设置为环境变量:

    export AWS_DEFAULT_REGION=us-east-1
    

    更具体的可以set environment variables in PyCharm

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2020-08-02
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多