Python - Dynamic variable in Redshift SQL Query

蓝咒 提交于 2019-12-06 15:08:12

You simply specify a replacement field in your SQL file, and the use a format command.

Create your file like this

UNLOAD ('Some SQL Query')
TO 's3://{bucket}/{key}'

And use this file in python like

template = open('file1.sql', 'r').read()
query = template.format(bucket='mybucket', key='folder/file.csv')

Implementing it this way will give you a tough time.

The best way to do is to dump the file at a static location:

UNLOAD 
(
'
Some SQL Query
'
)
TO 's3://path/to/static/s3_bucket'
...

and then use (via a shellscript / or opt for a suitable command for any other script)

aws s3 mv $source $destination

Here, you may pass any value for $destination which can be easily populated during run-time.

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...)

You would not be able to set up the UNLOAD path dynamically at runtime, however you could put your SQL statement in a something like a shell/python script where you can create variables with the path you'd like and then pass them into the query.

This UNLOAD utility by AWS will get you started if you decide to go with a python script.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!