How to send parameters to redshift queries

930 views
Skip to first unread message

Ramnath Medikonda

unread,
Apr 14, 2016, 7:18:18 PM4/14/16
to Luigi
Has anyone got a chance to pass parameters to a SQL query when implementing RedshiftQuery class in luigi?

I have this query which needs to read parameters:


delete from "dev"."device_engagement_staging" where batch_id = %(batch_id)s; where batch_id will have to be read from command line(luigi parameters).

Any suggestion would be appreciated.

ch...@thekensta.com

unread,
May 20, 2016, 11:46:45 AM5/20/16
to Luigi

In your class, try something like this:


class MyRedShiftTask(RedshiftQuery):

batch_id = luigi.IntParameter()

@property
def query(self):
template = "delete from dev.device_engagement_staging where batch_id = {batch_id}"
sql = template.format(batch_id=self.batch_id)
return sql

Or for more sophisticated formatting you can try jinja2 templates.
you can also put in a schema and and table as params then read from config, e.g.

schema = luigi.Parameter(default="dev")
table = luigi.Parameter(default="device_engagement_staging")
...
template = "delete from {schema}.{table} where batch_id = {batch_id}"


and then in the production config do:

[MyRedShiftTask]
schema=production
table=device_engagement

Or at least, this is what is working for me :-)

Reply all
Reply to author
Forward
0 new messages