AWSGlue作业3.0中无法导入psycopg2,该如何解决?
创始人
2024-09-25 18:02:13
0

在 AWS Glue 作业3.0 中无法直接导入 psycopg2,因为 AWS Glue 是运行在托管环境中的服务器。解决方法是使用 AWS Glue 的 Job Bookmarks 功能和Lambda Layers。

步骤如下:

  1. 创建用于将 psycopg2 安装到 AWS Lambda Layer 的 Python 包。以下示例使用 pipreqs 来创建 requirements.txt 文件。
pip install pipreqs
pipreqs /path/to/your/python/code --force
  1. 创建 AWS Lambda Layer 并将 psycopg2 安装到该层。可以使用以下示例代码:
import os
import boto3
from botocore.exceptions import ClientError
import tempfile
import zipfile
import shutil
 
#Install psycopg2 in temp directory 
tempdir=tempfile.mkdtemp()
print("Temp dir=",tempdir)
cmd="pip3 install psycopg2-binary -t  "+tempdir
os.system(cmd)
 
# Create a zip file with the psycopg2 package
zip_file = tempfile.mkstemp(suffix='.zip')[1]
with zipfile.ZipFile(zip_file, mode='w') as package:
    for root, dirs, files in os.walk(tempdir):
        for file in files:
            filename = os.path.join(root, file)
            package.write(filename,
                          os.path.relpath(filename, tempdir))
 
# Upload the package to Lambda Layers
layer_name = 'psycopg2-layer'
layer_description = 'psycopg2 library for Python'
layer_license = 'BSD-3-Clause'
client = boto3.client('lambda')
try:
    response = client.publish_layer_version(
        LayerName=layer_name,
        Content={
            'ZipFile': open(zip_file, 'rb').read(),
        },
        Description=layer_description,
        CompatibleRuntimes=[
            'python3.8',
        ],
        LicenseInfo=layer_license
    )
    print(response)
finally:
    os.remove(zip_file)
print("Lambda layer created successfully!")
  1. 在 AWS Glue 作业中使用 Lambda Layer。在做 AWS Glue 作业 3.0 的时候,可以选择使用 Lambda Layer 来导入需要的 Python 包。以下是一个示例代码:
import boto3
import sys
 
#Add psycopg2 Lambda layer
client = boto3.client('lambda')
response = client.list_layer_versions(
    LayerName='psycopg2-layer'
)
latest_version = response['LayerVersions'][0]['Version']
 
gluepysparkjob_params = {"--extra-py-files": f"arn:aws:lambda:REGION:ACCOUNT_ID:layer:psycopg2-layer:{latest_version}"}
 
def main():
    try:
        spark_submit_args = ["gluepysparksubmit"]
        spark_submit_args.extend(sys.argv[1:])
 
        for i, arg in enumerate(spark_submit_args):
            if arg == "--extra-py-files":
                gluepysparkjob_params[arg] += "," + gluepysparkjob_params[arg + "1"]
                del gluepysparkjob_params[arg + "1"]
 
        for param, value in gluepysparkjob_params.items():
            if not f"{param} {value}" in spark_submit_args:
                spark_submit_args.append(param)
                spark_submit_args.append(value)
 
        print(f"Running spark-submit with: {spark_submit_args}")
 
        # start spark-submit process with modified arguments
        subprocess.Popen(spark_submit_args)
    except Exception as e:
        print(f"Error running main function: {e}")
 

相关内容

热门资讯

透视安装!wepoker免费脚... 您好,拱趴大菠萝万能辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
透视稳赢!wpk有辅助吗(透视... 透视稳赢!wpk有辅助吗(透视)开挂辅助器(好像是真的挂)-哔哩哔哩wpk有辅助吗辅助器中分为三种模...
透视新版!xpoker辅助控制... 透视新版!xpoker辅助控制(透视)开挂辅助挂(本来是有挂)-哔哩哔哩1、许多玩家不知道xpoke...
透视辅助!wpk插件辅助,约局... 透视辅助!wpk插件辅助,约局吧德州透视,绝活儿教程(总是真的是有挂)-哔哩哔哩约局吧德州透视辅助器...
透视肯定!pokemmo脚本手... 透视肯定!pokemmo脚本手机版,werplan外卦神器,课程教程(果然有挂)-哔哩哔哩1、pok...
透视计算器!wepoker游戏... 透视计算器!wepoker游戏安装教程(透视)开挂辅助工具(都是是真的挂)-哔哩哔哩所有人都在同一条...
透视工具!hh poker插件... 透视工具!hh poker插件下载,wepoker辅助分析器,讲义教程(一贯有挂)-哔哩哔哩运wep...
透视存在!sohoopoker... 透视存在!sohoopoker辅助(透视)开挂辅助器(好像真的有挂)-哔哩哔哩;所有人都在同一条线上...
透视挂透视!约局吧怎么看有没有... 透视挂透视!约局吧怎么看有没有挂,wepoker钻石怎么看底牌,手册教程(确实真的是有挂)-哔哩哔哩...
透视打法!wepoker是不是... 透视打法!wepoker是不是有人用挂(透视)开挂辅助工具(好像是真的挂)-哔哩哔哩一、wepoke...