airflow.providers.slack.transfers.sql_to_slack
¶
Module Contents¶
Classes¶
Operator implements base sql methods for SQL to Slack Transfer operators. |
|
Executes an SQL statement in a given SQL connection and sends the results to Slack. |
|
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[str, Any] | None) – The parameters to pass to the SQL query.
- class airflow.providers.slack.transfers.sql_to_slack.SqlToSlackOperator(*, sql, sql_conn_id, slack_conn_id, sql_hook_params=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) – The connection id for Slack.
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[str, Any] | None) – The parameters to pass to the SQL query
- class airflow.providers.slack.transfers.sql_to_slack.SqlToSlackApiFileOperator(*, sql, sql_conn_id, sql_hook_params=None, parameters=None, slack_conn_id=SlackHook.default_conn_name, 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[str, Any] | 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, }, )