Python - Redshift 中的动态变量 SQL 查询
Python - Dynamic variable in Redshift SQL Query
我正在 python 环境中开发,我想使用 psycopg2
调用 sql 查询
假设我在 .sql 文件中有以下 UNLOAD 命令:
UNLOAD
(
'
Some SQL Query
'
)
TO 's3://%PATH%'
...
在 sql 文件中,%PATH%
应该明确如:'folder1/folder3/file_name'
。
但我希望 python 程序在运行时设置此 %PATH%
。这意味着 .sql 文件包含类似 %PATH%
的内容,并且仅在运行时设置。
知道怎么做吗?
您将无法在运行时动态设置 UNLOAD 路径,但是您可以将 SQL 语句放在类似 shell/python 的脚本中,您可以在其中使用路径创建变量你想要然后将它们传递到查询中。
This 如果您决定使用 python 脚本,AWS 的 UNLOAD 实用程序将帮助您入门。
以这种方式实施它会给您带来困难。
最好的方法是将文件转储到静态位置:
UNLOAD
(
'
Some SQL Query
'
)
TO 's3://path/to/static/s3_bucket'
...
然后使用(通过 shellscript/或为任何其他脚本选择合适的命令)
aws s3 mv $source $destination
在这里,您可以为 $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...)
您只需在 SQL 文件中指定一个 replacement field,然后使用格式化命令。
像这样创建你的文件
UNLOAD ('Some SQL Query')
TO 's3://{bucket}/{key}'
并在 python 中使用此文件,如
template = open('file1.sql', 'r').read()
query = template.format(bucket='mybucket', key='folder/file.csv')
我正在 python 环境中开发,我想使用 psycopg2
假设我在 .sql 文件中有以下 UNLOAD 命令:
UNLOAD
(
'
Some SQL Query
'
)
TO 's3://%PATH%'
...
在 sql 文件中,%PATH%
应该明确如:'folder1/folder3/file_name'
。
但我希望 python 程序在运行时设置此 %PATH%
。这意味着 .sql 文件包含类似 %PATH%
的内容,并且仅在运行时设置。
知道怎么做吗?
您将无法在运行时动态设置 UNLOAD 路径,但是您可以将 SQL 语句放在类似 shell/python 的脚本中,您可以在其中使用路径创建变量你想要然后将它们传递到查询中。
This 如果您决定使用 python 脚本,AWS 的 UNLOAD 实用程序将帮助您入门。
以这种方式实施它会给您带来困难。
最好的方法是将文件转储到静态位置:
UNLOAD
(
'
Some SQL Query
'
)
TO 's3://path/to/static/s3_bucket'
...
然后使用(通过 shellscript/或为任何其他脚本选择合适的命令)
aws s3 mv $source $destination
在这里,您可以为 $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...)
您只需在 SQL 文件中指定一个 replacement field,然后使用格式化命令。
像这样创建你的文件
UNLOAD ('Some SQL Query')
TO 's3://{bucket}/{key}'
并在 python 中使用此文件,如
template = open('file1.sql', 'r').read()
query = template.format(bucket='mybucket', key='folder/file.csv')