gpt4 book ai didi

python - AWS Python Shell - 如何使用 Glue 目录连接

转载 作者:行者123 更新时间:2023-12-02 16:53:33 25 4
gpt4 key购买 nike

我在 Glue 中定义了一个 JDBC 连接,我能够在 Glue Spark 作业中成功使用它。我将如何在 Glue Python Shell 作业中使用相同的连接?我找不到任何模板如何执行此操作,尽管我已经看到了可能的引用。

另一种方法是如何在我需要包含外部库的 Python Shell 中定义 JDBC 连接?我读过的 pyodbc 由于依赖关系不可用。

最佳答案

这是代码。确保在您的 python shell 作业 cloudformation 模板中添加了相同的连接。我们正在使用 pg8000 库。

def get_connection(self, conn_name):

client = boto3.client('glue', region_name=self.region_id)
response = client.get_connection(Name=conn_name)
print response

connection_properties = response['Connection']['ConnectionProperties']
URL = connection_properties['JDBC_CONNECTION_URL']
url_list = URL.split("/")

host = "{}".format(url_list[-2][:-5])
port = url_list[-2][-4:]
database = "{}".format(url_list[-1])
user = "{}".format(connection_properties['USERNAME'])
pwd = "{}".format(connection_properties['PASSWORD'])

# print "user:{}".format(user)
# print "pwd:{}".format(pwd)
# print "host:{}".format(host)
# print "port:{}".format(port)
# print "database:{}".format(database)

rs_conn = dbapi.connect(database=database, host=host, port=5439, \
user=user, password=pwd, ssl=True)
cur = rs_conn.cursor()
cur.execute("set statement_timeout = 1200000")
rs_conn.commit()
cur.close()
return rs_conn

关于python - AWS Python Shell - 如何使用 Glue 目录连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57439002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com