Pythonを使ってLambda関数からRedshiftを更新しようとしています。これを行うために、私は2つのコードフラグメントを結合しようとしています。両方のフラグメントは、別々に実行すると機能的です。Redshift(Python)を更新するためにpsycopg2とLambdaを使う
エクリプス
ためPyDevはから赤方偏移を更新 S3バケット(プレ内蔵ラムダに利用可能なテンプレート)これらの両方ので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
にアップロードされたコンテンツを受信
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()
私はそれらを組み合わせて、Redshiftをs3へのファイルのアップロード時に更新できるようにしました:
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)をそのフォルダに移動しました。
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形式で圧縮私はそのフォルダにpsycopg2をインストールするには、以下のコマンドを実行しました。そして、私のラムダ関数にそのzipをアップロードしました。私はバケツに機能モニターをドキュメントをアップロードするとき、私は私のCloudWatchのログに次のエラーが表示されます。
Unable to import module 'lambda_function1': No module named _psycopg
私は図書館で見てみると、「_psycopg」という名前だけの事は「_psycopg.pyd」です。
この問題の原因は何ですか?私は3.4を使うときにLambdaがPython 2.7を使用することは重要ですか? Windowsマシンでファイルの内容を圧縮しても問題はありませんか?誰もラムダからRedshiftに接続できましたか?
悲しいことに、Windowsで作成したホイールファイルをラムダで使用することはできません。 LambdaでRedshiftを使ってPure Pythonのpg8000を使うことができます。 – bbayles