airflow.providers.slack.transfers.sql_to_slack

Module Contents

Classes

BaseSqlToSlackOperator

Operator implements base sql methods for SQL to Slack Transfer operators.

SqlToSlackOperator

Executes an SQL statement in a given SQL connection and sends the results to Slack. The results of the

SqlToSlackApiFileOperator

Executes an SQL statement in a given SQL connection and sends the results to Slack API as file.

class airflow.providers.slack.transfers.sql_to_slack.BaseSqlToSlackOperator(*, sql, sql_conn_id, sql_hook_params=None, parameters=None, **kwargs)[source]

Bases: airflow.models.BaseOperator

Operator implements base sql methods for SQL to Slack Transfer operators.

Parameters
  • sql (str) – The SQL query to be executed

  • sql_conn_id (str) – reference to a specific DB-API Connection.

  • sql_hook_params (dict | None) – Extra config params to be passed to the underlying hook. Should match the desired hook constructor params.

  • parameters (Iterable | Mapping | None) – The parameters to pass to the SQL query.

class airflow.providers.slack.transfers.sql_to_slack.SqlToSlackOperator(*, sql, sql_conn_id, sql_hook_params=None, slack_conn_id=None, slack_webhook_token=None, slack_channel=None, slack_message, results_df_name='results_df', parameters=None, **kwargs)[source]

Bases: BaseSqlToSlackOperator

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

Parameters
  • sql (str) – The SQL query to be executed (templated)

  • slack_message (str) – The 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_id (str) – reference to a specific database.

  • sql_hook_params (dict | None) – Extra config params to be passed to the underlying hook. Should match the desired hook constructor params.

  • slack_conn_id (str | None) – The connection id for Slack.

  • slack_webhook_token (str | None) – The 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_channel (str | None) – The channel to send message. Override default from Slack connection.

  • results_df_name (str) – The name of the JINJA template’s dataframe variable, default is ‘results_df’

  • parameters (Iterable | Mapping | None) – The parameters to pass to the SQL query

template_fields: Sequence[str] = ('sql', 'slack_message')[source]
template_ext: Sequence[str] = ('.sql', '.jinja', '.j2')[source]
template_fields_renderers[source]
times_rendered = 0[source]
render_template_fields(context, jinja_env=None)[source]

Template all attributes listed in self.template_fields.

This mutates the attributes in-place and is irreversible.

Parameters
  • context – Context dict with values to apply on content.

  • jinja_env – Jinja’s environment to use for rendering.

execute(context)[source]

This is the main method to derive when creating an operator. Context is the same dictionary used as when rendering jinja templates.

Refer to get_template_context for more context.

class airflow.providers.slack.transfers.sql_to_slack.SqlToSlackApiFileOperator(*, sql, sql_conn_id, sql_hook_params=None, parameters=None, slack_conn_id, slack_filename, slack_channels=None, slack_initial_comment=None, slack_title=None, df_kwargs=None, **kwargs)[source]

Bases: BaseSqlToSlackOperator

Executes an SQL statement in a given SQL connection and sends the results to Slack API as file.

Parameters
  • sql (str) – The SQL query to be executed

  • sql_conn_id (str) – reference to a specific DB-API Connection.

  • slack_conn_id (str) – Slack API Connection.

  • slack_filename (str) – Filename for display in slack. Should contain supported extension which referenced to SUPPORTED_FILE_FORMATS. It is also possible to set compression in extension: filename.csv.gzip, filename.json.zip, etc.

  • sql_hook_params (dict | None) – Extra config params to be passed to the underlying hook. Should match the desired hook constructor params.

  • parameters (Iterable | Mapping | None) – The parameters to pass to the SQL query.

  • slack_channels (str | Sequence[str] | None) – Comma-separated list of channel names or IDs where the file will be shared. If omitting this parameter, then file will send to workspace.

  • slack_initial_comment (str | None) – The message text introducing the file in specified slack_channels.

  • slack_title (str | None) – Title of file.

  • df_kwargs (dict | None) – Keyword arguments forwarded to pandas.DataFrame.to_{format}() method.

Example:
SqlToSlackApiFileOperator(
    task_id="sql_to_slack",
    sql="SELECT 1 a, 2 b, 3 c",
    sql_conn_id="sql-connection",
    slack_conn_id="slack-api-connection",
    slack_filename="awesome.json.gz",
    slack_channels="#random,#general",
    slack_initial_comment="Awesome load to compressed multiline JSON.",
    df_kwargs={
        "orient": "records",
        "lines": True,
    },
)
template_fields: Sequence[str] = ('sql', 'slack_channels', 'slack_filename', 'slack_initial_comment', 'slack_title')[source]
template_ext: Sequence[str] = ('.sql', '.jinja', '.j2')[source]
template_fields_renderers[source]
SUPPORTED_FILE_FORMATS: Sequence[str] = ('csv', 'json', 'html')[source]
execute(context)[source]

This is the main method to derive when creating an operator. Context is the same dictionary used as when rendering jinja templates.

Refer to get_template_context for more context.

Was this entry helpful?