Python - Redshift SQL 查询中的动态变量

标签 python amazon-s3 amazon-redshift psycopg2

我正在 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/

相关文章:

postgresql - Redshift 错误: relation "Temp table" does not exist

python - 如何制作非数值列的散点图?

对标准输出的每一行进行正则表达式并推送到 shell/bash 中的数组

c++ - 使用 libcurl 调用 Amazon s3 RESTful 服务

google-app-engine - App Engine 作为 Web 服务,S3 作为存储

amazon-redshift - Redshift 中的季度日

ssis - "[Amazon][ODBC] (11310) SQL C type not supported."通过 Redshift 将 "Execute SQL Task"与单行结果集一起使用时出错

python - 遍历 Python 目录中的无限文件

python - 使用 Flask 运行时启动功能

python - 编写一个函数,该函数接受一个列表作为参数,并返回一个复制列表中元素的 LIST