无法使用redshiftconnector使sql可靠执行

2024-09-24 02:22:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用红移连接器创建了一个连接,我发现了一些可能的问题。我所要做的就是在我的库中设置一个(可靠的)函数,该函数将创建游标并在启用自动提交的情况下执行传递的sql语句,我们需要至少支持delete fromselect from

连接设置:

conn = redshift_connector.connect(
    host=db_host,
    port=5439,
    database=db_name,
    user=db_user,
    password=db_pass
)

请注意,在我的库中,我们使用@property装饰器,如下所示:

@property
def conn(self):
    conn = redshift_connector.connect(
    ...
    )

然后,当我尝试按照这里的说明https://pypi.org/project/redshift-connector/运行sql语句时,我遇到了各种问题。事实上,我所看到的唯一一件作品是select

比如说,

cursor = conn.cursor() 
res = cursor.execute("delete from schema.table where col = "val" 
res.close()

不会导致任何内容被删除

如果我这样做了

conn.autocommit = True
cursor = conn.cursor() 
res = cursor.execute("delete from schema.table where col = "val" 
res.close()

我在Redshift中创建了3个锁,ShareRowExclusiveLock-grated:True,AccessShareLock-grated:True,ShareRowExclusiveLock-grated:False,execute语句将挂起,直到我删除这些锁为止。删除锁后,取决于是否终止正确的锁,execute语句将成功完成或出错

终止所有锁不会导致在后续执行中删除任何内容

我目前的理论是红移连接器不能正确或有效地创建和移除锁

作为补充说明,我发现我无法在sqlalchemy redshift中交换,最新版本的awswrangler似乎出于未知的原因期待使用redshift连接器(不确定为什么有人认为重新发明轮子会很有趣)。使用sqlalchemy postgresql+psycopg2或redshift+psycopg2会导致与自动提交相关的AttributeError。awswrangler似乎也不支持或使用sqlalchemy的execution_options(autocommit=True)

我还要补充一点,pypi页面显示有一个redshift_连接器标记,没有

如果有人知道如何让sqlalchemy与awswrangler一起工作,我将非常感谢您的帮助


Tags: 函数fromtrueredshiftexecutedbconnectorsqlalchemy