如何从 AWS Glue Python Shell 连接到 RDS 实例?
How to Connect to RDS Instance from AWS Glue Python Shell?
我正在尝试从 AWS Glue 访问 RDS 实例,我在 EC2 实例中有一些 python 脚本 运行,我目前使用 PYODBC 进行连接,但在尝试为胶水安排作业时,我无法导入 PYODBC,因为 AWS Glue 本身不支持它,不确定驱动程序如何在 glue shell 中工作。
来自:介绍 Python Shell AWS Glue 中的工作 announcement:
Python shell jobs in AWS Glue support scripts that are compatible with Python 2.7 and come pre-loaded with libraries such as the Boto3, NumPy, SciPy, pandas, and others.
module list 不包含 pyodbc
模块,并且不能作为自定义 .egg 文件提供,因为它依赖于 libodbc.so.2
和 pyodbc.so
库。
我想你有两个选择:
- 从 Glue 的控制台为您的数据库创建一个 jdbc connection,并使用 Glue 的内部方法对其进行查询。这当然需要更改代码。
- 改为使用 Lambda 函数。您需要 pack pyodbc and the required libs along with your code in a zip file. Someone has already compiled those libs for AWS Lambda, see here。
希望对您有所帮助
对于 AWS Glue,使用 Dataframe/DynamicFrame 并指定 SQL 服务器 JDBC 驱动程序。 AWS Glue 已在其环境中包含 JDBC 用于 SQL 服务器的驱动程序,因此您无需添加任何额外的驱动程序 jar 与粘合作业。
df1=spark.read.format("jdbc").选项("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").选项("url", url_src)。选项("dbtable", dbtable_src).选项("user", userID_src).选项("password", password_src).load()
如果您使用的是 SQL 而不是 table:
df1=spark.read.format("jdbc").选项("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").选项("url", url_src)。 option("dbtable", (你的 select 语句") A).option("user" , userID_src).option("password", password_src).load()
作为替代解决方案,您还可以在 AWS Glue
中的 python 脚本 运行 中为 SQL 服务器使用 jtds 驱动程序
我能够使用 python 库 psycopg2,即使它不是用纯 python 编写的,也没有预加载 aws glue python shell环境。这与 aws 胶水文档相反。因此,您可能能够以类似的方式使用与 odbc 相关的 python 库。我为 psycopg2 库创建了 .egg 文件,并在 glue python shell 环境中成功使用了它。以下是胶水 python shell 的日志,如果您的脚本中有 import psycopg2
并且胶水作业引用相关的 psycopg2 .egg 文件。
Creating /glue/lib/installation/site.py
Processing psycopg2-2.8.3-py2.7.egg
Copying psycopg2-2.8.3-py2.7.egg to /glue/lib/installation
Adding psycopg2 2.8.3 to easy-install.pth file
Installed /glue/lib/installation/psycopg2-2.8.3-py2.7.egg
Processing dependencies for psycopg2==2.8.3
Searching for psycopg2==2.8.3
Reading https://pypi.org/simple/psycopg2/
Downloading https://files.pythonhosted.org/packages/5c/1c/6997288da181277a0c29bc39a5f9143ff20b8c99f2a7d059cfb55163e165/psycopg2-2.8.3.tar.gz#sha256=897a6e838319b4bf648a574afb6cabcb17d0488f8c7195100d48d872419f4457
Best match: psycopg2 2.8.3
Processing psycopg2-2.8.3.tar.gz
Writing /tmp/easy_install-dml23ld7/psycopg2-2.8.3/setup.cfg
Running psycopg2-2.8.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-dml23ld7/psycopg2-2.8.3/egg-dist-tmp-9qwen3l_
creating /glue/lib/installation/psycopg2-2.8.3-py3.6-linux-x86_64.egg
Extracting psycopg2-2.8.3-py3.6-linux-x86_64.egg to /glue/lib/installation
Removing psycopg2 2.8.3 from easy-install.pth file
Adding psycopg2 2.8.3 to easy-install.pth file
Installed /glue/lib/installation/psycopg2-2.8.3-py3.6-linux-x86_64.egg
Finished processing dependencies for psycopg2==2.8.3
这些是我用来从胶水连接到 RDS 的步骤 python shell 作业:
- 将你的依赖包打包成一个egg文件(这些包必须是纯的python如果我没记错的话)。放在S3中。
- 将您的作业设置为在作业配置> Python 库路径下引用该 egg 文件
- 验证您的作业是否可以导入 package/module
- 创建到您的 RDS 的粘合连接(位于数据库 > 表、连接中),测试连接以确保它可以访问您的 RDS
- 现在在你的工作中,你必须将它设置为reference/use这个连接。它在您配置作业或编辑作业时需要连接。
完成这些步骤并验证后,您应该能够连接。在我的示例中,我使用了 pymysql。
如果有人需要使用 python shell 与 sqlalchemy 的 postgres 连接,可以通过引用 sqlalchemy, scramp, pg8000
wheel 文件,重要的是通过消除 pg8000 重建 wheel对 setup.py
.
的 scramp 依赖
我需要做类似的事情,最终在 Scala 中创建了另一个 Glue 作业,同时使用 Python 处理其他所有内容。我知道它可能不适合所有人,但想提一下
我正在尝试从 AWS Glue 访问 RDS 实例,我在 EC2 实例中有一些 python 脚本 运行,我目前使用 PYODBC 进行连接,但在尝试为胶水安排作业时,我无法导入 PYODBC,因为 AWS Glue 本身不支持它,不确定驱动程序如何在 glue shell 中工作。
来自:介绍 Python Shell AWS Glue 中的工作 announcement:
Python shell jobs in AWS Glue support scripts that are compatible with Python 2.7 and come pre-loaded with libraries such as the Boto3, NumPy, SciPy, pandas, and others.
module list 不包含 pyodbc
模块,并且不能作为自定义 .egg 文件提供,因为它依赖于 libodbc.so.2
和 pyodbc.so
库。
我想你有两个选择:
- 从 Glue 的控制台为您的数据库创建一个 jdbc connection,并使用 Glue 的内部方法对其进行查询。这当然需要更改代码。
- 改为使用 Lambda 函数。您需要 pack pyodbc and the required libs along with your code in a zip file. Someone has already compiled those libs for AWS Lambda, see here。
希望对您有所帮助
对于 AWS Glue,使用 Dataframe/DynamicFrame 并指定 SQL 服务器 JDBC 驱动程序。 AWS Glue 已在其环境中包含 JDBC 用于 SQL 服务器的驱动程序,因此您无需添加任何额外的驱动程序 jar 与粘合作业。
df1=spark.read.format("jdbc").选项("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").选项("url", url_src)。选项("dbtable", dbtable_src).选项("user", userID_src).选项("password", password_src).load()
如果您使用的是 SQL 而不是 table:
df1=spark.read.format("jdbc").选项("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").选项("url", url_src)。 option("dbtable", (你的 select 语句") A).option("user" , userID_src).option("password", password_src).load()
作为替代解决方案,您还可以在 AWS Glue
中的 python 脚本 运行 中为 SQL 服务器使用 jtds 驱动程序我能够使用 python 库 psycopg2,即使它不是用纯 python 编写的,也没有预加载 aws glue python shell环境。这与 aws 胶水文档相反。因此,您可能能够以类似的方式使用与 odbc 相关的 python 库。我为 psycopg2 库创建了 .egg 文件,并在 glue python shell 环境中成功使用了它。以下是胶水 python shell 的日志,如果您的脚本中有 import psycopg2
并且胶水作业引用相关的 psycopg2 .egg 文件。
Creating /glue/lib/installation/site.py
Processing psycopg2-2.8.3-py2.7.egg
Copying psycopg2-2.8.3-py2.7.egg to /glue/lib/installation
Adding psycopg2 2.8.3 to easy-install.pth file
Installed /glue/lib/installation/psycopg2-2.8.3-py2.7.egg
Processing dependencies for psycopg2==2.8.3
Searching for psycopg2==2.8.3
Reading https://pypi.org/simple/psycopg2/
Downloading https://files.pythonhosted.org/packages/5c/1c/6997288da181277a0c29bc39a5f9143ff20b8c99f2a7d059cfb55163e165/psycopg2-2.8.3.tar.gz#sha256=897a6e838319b4bf648a574afb6cabcb17d0488f8c7195100d48d872419f4457
Best match: psycopg2 2.8.3
Processing psycopg2-2.8.3.tar.gz
Writing /tmp/easy_install-dml23ld7/psycopg2-2.8.3/setup.cfg
Running psycopg2-2.8.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-dml23ld7/psycopg2-2.8.3/egg-dist-tmp-9qwen3l_
creating /glue/lib/installation/psycopg2-2.8.3-py3.6-linux-x86_64.egg
Extracting psycopg2-2.8.3-py3.6-linux-x86_64.egg to /glue/lib/installation
Removing psycopg2 2.8.3 from easy-install.pth file
Adding psycopg2 2.8.3 to easy-install.pth file
Installed /glue/lib/installation/psycopg2-2.8.3-py3.6-linux-x86_64.egg
Finished processing dependencies for psycopg2==2.8.3
这些是我用来从胶水连接到 RDS 的步骤 python shell 作业:
- 将你的依赖包打包成一个egg文件(这些包必须是纯的python如果我没记错的话)。放在S3中。
- 将您的作业设置为在作业配置> Python 库路径下引用该 egg 文件
- 验证您的作业是否可以导入 package/module
- 创建到您的 RDS 的粘合连接(位于数据库 > 表、连接中),测试连接以确保它可以访问您的 RDS
- 现在在你的工作中,你必须将它设置为reference/use这个连接。它在您配置作业或编辑作业时需要连接。
完成这些步骤并验证后,您应该能够连接。在我的示例中,我使用了 pymysql。
如果有人需要使用 python shell 与 sqlalchemy 的 postgres 连接,可以通过引用 sqlalchemy, scramp, pg8000
wheel 文件,重要的是通过消除 pg8000 重建 wheel对 setup.py
.
我需要做类似的事情,最终在 Scala 中创建了另一个 Glue 作业,同时使用 Python 处理其他所有内容。我知道它可能不适合所有人,但想提一下