ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用psycopg2和Lambda来更新Redshift(Python)

2019-09-23 19:56:13  阅读:319  来源: 互联网

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


我试图使用python从Lambda函数更新Redshift.为此,我试图合并两个代码片段.当我单独运行它们时,两个片段都是有效的.

>从PyDev for Eclipse更新Redshift

import psycopg2

conn_string = "dbname='name' port='0000' user='name' password='pwd' host='url'"
conn = psycopg2.connect(conn_string)

cursor = conn.cursor()

cursor.execute("UPDATE table SET attribute='new'")
conn.commit()
cursor.close()

>接收上传到S3 Bucket的内容(Lambda上可用的预建模板)

from __future__ import print_function

import json
import urllib
import boto3

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')

    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']

    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

由于这两个段都有效,我尝试将它们组合起来,以便在将文件上传到s3时更新Redshift:

from __future__ import print_function

import json
import urllib
import boto3
import psycopg2

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')

    conn_string = "dbname='name' port='0000' user='name' password='pwd' host='url'"

    conn = psycopg2.connect(conn_string)

    cursor = conn.cursor()

    cursor.execute("UPDATE table SET attribute='new'")
    conn.commit()
    cursor.close()

    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        print("CONTENT TYPE: " + response['Body'].read())
        return response['Body'].read()
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

由于我使用的是外部库,因此我需要创建一个部署包.我创建了一个新文件夹(lambda_function1)并将我的.py文件(lambda_function1.py)移动到该文件夹​​.我运行以下命令在该文件夹中安装psycopg2:

pip install psycopg2 -t \lambda_function1

我收到以下反馈:

Collecting psycopg2
  Using cached psycopg2-2.6.1-cp34-none-win_amd64.whl
Installing collected packages: psycopg2
Successfully installed psycopg2-2.6.1 

然后我压缩了目录的内容.并将该zip文件上传到我的lambda函数中.当我将文档上传到功能监视器的存储桶时,我在cloudwatch日志中收到以下错误:

Unable to import module 'lambda_function1': No module named _psycopg 

当我查看图书馆时,唯一名为“_psycopg”的是“_psycopg.pyd”.

是什么导致了这个问题?当我使用3.4时,Lambda使用Python 2.7是否重要?我在Windows机器上压缩文件内容是否重要?有没有人能够成功连接到lambda的Redshift?

解决方法:

为了使其工作,您需要使用静态链接的libpq.so库构建psycopg2.看看这个repo https://github.com/jkehler/awslambda-psycopg2.它已经构建了psycopg2包,并说明了如何自己构建它.

回到你的问题:

是什么导致了这个问题?

psycopg2需要构建一个使用静态链接库编译的Linux.

当我使用3.4时,Lambda使用Python 2.7是否重要?

是的,lambda只支持2.7版本.只需创建虚拟环境并在其中安装所有必需的软件包.

我在Windows机器上压缩文件内容是否重要?

只要您压缩的所有库都可以在Linux上运行,它就不会

有没有人能够成功连接到lambda的Redshift?

是.

标签:python,amazon-web-services,aws-lambda,amazon-redshift,aws-sdk
来源: https://codeday.me/bug/20190923/1815656.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有