【问题标题】:Writing to databse with AWS Lambda error - Calling the invoke API action failed with this message: Network Error使用 AWS Lambda 错误写入数据库 - 调用调用 API 操作失败并显示以下消息:网络错误
【发布时间】:2020-07-29 17:56:20
【问题描述】:

我正在查询 API,将其存储到 pandas 数据帧,对其进行转换,然后将其写入 AWS Redshift 数据库。在我的本地机器上没有问题,一切正常。当我将代码放在 AWS Lambda 中时,我收到了这个错误:

Calling the invoke API action failed with this message: Network Error

我读到这可能是由于它可以写入数据库的行数限制,然后我尝试只向数据库写入 1 行但仍然出现相同的错误。

我写的代码是这样的:

conn = create_engine('postgresql://user:password@redshifteu-west-1.redshift.amazonaws.com:5439/dev')
result.to_sql('table_1', conn, index=False, if_exists='replace', schema='schema')

我正在使用 pandas to_sql 方法和 sqlalchemy。如何使用 AWS Lambda 将我的数据帧写入 Redshift 数据库?

【问题讨论】:

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


    【解决方案1】:

    请注意,您可能需要psycopg2 才能通过 SQLAlchemy 连接到 Redshift:

    pip install psycopg2
    

    现在另一种可能性是您基本上超过了limit of S3 的速率。


    如果以上都行不通,并且你有信心没有超过速率限制,你可以给pandas_redshift一试:

    # pip install pandas-redshift
    import pandas_redshift as pr
    
    
    pr.connect_to_redshift(
        dbname='dev', host='redshifteu-west-1.redshift.amazonaws.com', 
        port=5439, user='user', password='password'
    )
    
    pr.connect_to_s3(
       aws_access_key_id='aws_access_key_id',
       aws_secret_access_key='aws_secret_access_key',
       bucket='bucket_name',
       subdirectory='subdirectory'
    )
    
    pr.pandas_to_redshift(data_frame=result, redshift_table_name='table_1')
    

    【讨论】:

    • 我从我找到的 github 上专门为 aws 服务器安装了 psycopg2。谢谢,试试你的方法。
    • 如果我只想写redshift,是否需要connect_to_s3部分?
    • 仍然遇到同样的问题 - 调用调用 API 操作失败并显示以下消息:网络错误
    • @JonasPalačionis 限制如何?
    • 但我没有使用 S3(据我了解),我只使用 AWS lambda 来查询 API 并将这些 pandas 数据帧写入 Redshift。我假设如果代码在我的本地机器上运行良好,它也应该在 lambda 上运行。
    猜你喜欢
    • 2019-04-10
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2015-02-02
    • 2021-06-10
    相关资源
    最近更新 更多