SqlToSlackOperator

Slack

Executes an SQL statement in a given SQL connection and sends the results to Slack. The results of the query are rendered into the ‘slack_message’ parameter as a Pandas dataframe using a JINJA variable called ‘{{ results_df }}’. The ‘results_df’ variable name can be changed by specifying a different ‘results_df_name’ parameter. The Tabulate library is added to the JINJA environment as a filter to allow the dataframe to be rendered nicely. For example, set ‘slack_message’ to {{ results_df | tabulate(tablefmt=”pretty”, headers=”keys”) }} to send the results to Slack as an ascii rendered table.

View on GitHub

Last Updated: Nov. 16, 2022

Access Instructions

Install the Slack provider package into your Airflow environment.

Import the module into your DAG file and instantiate it with your desired params.

Parameters

sqlRequiredThe SQL query to be executed (templated)
slack_messageRequiredThe templated Slack message to send with the data returned from the SQL connection. You can use the default JINJA variable {{ results_df }} to access the pandas dataframe containing the SQL results
sql_conn_idRequiredreference to a specific database.
sql_hook_paramsExtra config params to be passed to the underlying hook. Should match the desired hook constructor params.
slack_conn_idThe connection id for Slack.
slack_webhook_tokenThe token to use to authenticate to Slack. If this is not provided, the ‘slack_conn_id’ attribute needs to be specified in the ‘password’ field.
slack_channelThe channel to send message. Override default from Slack connection.
results_df_nameThe name of the JINJA template’s dataframe variable, default is ‘results_df’
parametersThe parameters to pass to the SQL query

Documentation

Executes an SQL statement in a given SQL connection and sends the results to Slack. The results of the query are rendered into the ‘slack_message’ parameter as a Pandas dataframe using a JINJA variable called ‘{{ results_df }}’. The ‘results_df’ variable name can be changed by specifying a different ‘results_df_name’ parameter. The Tabulate library is added to the JINJA environment as a filter to allow the dataframe to be rendered nicely. For example, set ‘slack_message’ to {{ results_df | tabulate(tablefmt=”pretty”, headers=”keys”) }} to send the results to Slack as an ascii rendered table.

See also

For more information on how to use this operator, take a look at the guide: SqlToSlackOperator

Was this page helpful?