我正在 python 环境中进行开发,我想使用 psycopg2
调用 sql 查询
假设我在 .sql 文件中有以下 UNLOAD 命令:
UNLOAD
(
'
Some SQL Query
'
)
TO 's3://%PATH%'
...
在 sql 文件中,%PATH%
应明确显示,例如:'folder1/folder3/file_name'
。
但我希望 python 程序在运行时设置这个 %PATH%
。这意味着 .sql 文件包含类似 %PATH%
的内容,并且仅在运行时设置。
知道如何做吗?
最佳答案
以这种方式实现它会给你带来困难。
最好的方法是将文件转储到静态位置:
UNLOAD
(
'
Some SQL Query
'
)
TO 's3://path/to/static/s3_bucket'
...
然后使用(通过 shellscript/或为任何其他脚本选择合适的命令)
aws s3 mv $源$目的地
在这里,您可以为 $destination
传递任何值,该值可以在运行时轻松填充。
In short, you've dumped the file in s3 at a fixed location (using UNLOAD) and moved it to the location of your choice or a location populated at run time (using aws s3 mv...)
关于Python - Redshift SQL 查询中的动态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48092223/