|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | + |
| 19 | +import datetime |
| 20 | +import numbers |
| 21 | +from contextlib import closing |
| 22 | +from typing import Any, Iterable, Mapping, Optional, Sequence, Union |
| 23 | + |
| 24 | +from airflow.operators.sql import BaseSQLOperator |
| 25 | +from airflow.providers.google.suite.hooks.sheets import GSheetsHook |
| 26 | + |
| 27 | + |
| 28 | +class SQLToGoogleSheetsOperator(BaseSQLOperator): |
| 29 | + """ |
| 30 | + Copy data from SQL results to provided Google Spreadsheet. |
| 31 | +
|
| 32 | + :param sql: The SQL to execute. |
| 33 | + :type sql: str |
| 34 | + :param spreadsheet_id: The Google Sheet ID to interact with. |
| 35 | + :type spreadsheet_id: str |
| 36 | + :param conn_id: the connection ID used to connect to the database. |
| 37 | + :type sql_conn_id: str |
| 38 | + :param parameters: The parameters to render the SQL query with. |
| 39 | + :type parameters: dict or iterable |
| 40 | + :param database: name of database which overwrite the defined one in connection |
| 41 | + :type database: str |
| 42 | + :param spreadsheet_range: The A1 notation of the values to retrieve. |
| 43 | + :type spreadsheet_range: str |
| 44 | + :param gcp_conn_id: The connection ID to use when fetching connection info. |
| 45 | + :type gcp_conn_id: str |
| 46 | + :param delegate_to: The account to impersonate using domain-wide delegation of authority, |
| 47 | + if any. For this to work, the service account making the request must have |
| 48 | + domain-wide delegation enabled. |
| 49 | + :type delegate_to: str |
| 50 | + :param impersonation_chain: Optional service account to impersonate using short-term |
| 51 | + credentials, or chained list of accounts required to get the access_token |
| 52 | + of the last account in the list, which will be impersonated in the request. |
| 53 | + If set as a string, the account must grant the originating account |
| 54 | + the Service Account Token Creator IAM role. |
| 55 | + If set as a sequence, the identities from the list must grant |
| 56 | + Service Account Token Creator IAM role to the directly preceding identity, with first |
| 57 | + account from the list granting this role to the originating account (templated). |
| 58 | + :type impersonation_chain: Union[str, Sequence[str]] |
| 59 | + """ |
| 60 | + |
| 61 | + template_fields = ( |
| 62 | + "sql", |
| 63 | + "spreadsheet_id", |
| 64 | + "spreadsheet_range", |
| 65 | + "impersonation_chain", |
| 66 | + ) |
| 67 | + |
| 68 | + template_fields_renderers = {"sql": "sql"} |
| 69 | + template_ext = (".sql",) |
| 70 | + |
| 71 | + ui_color = "#a0e08c" |
| 72 | + |
| 73 | + def __init__( |
| 74 | + self, |
| 75 | + *, |
| 76 | + sql: str, |
| 77 | + spreadsheet_id: str, |
| 78 | + sql_conn_id: str, |
| 79 | + parameters: Optional[Union[Mapping, Iterable]] = None, |
| 80 | + database: str = None, |
| 81 | + spreadsheet_range: str = "Sheet1", |
| 82 | + gcp_conn_id: str = "google_cloud_default", |
| 83 | + delegate_to: Optional[str] = None, |
| 84 | + impersonation_chain: Optional[Union[str, Sequence[str]]] = None, |
| 85 | + **kwargs, |
| 86 | + ) -> None: |
| 87 | + super().__init__(**kwargs) |
| 88 | + |
| 89 | + self.sql = sql |
| 90 | + self.conn_id = sql_conn_id |
| 91 | + self.database = database |
| 92 | + self.parameters = parameters |
| 93 | + self.gcp_conn_id = gcp_conn_id |
| 94 | + self.spreadsheet_id = spreadsheet_id |
| 95 | + self.spreadsheet_range = spreadsheet_range |
| 96 | + self.delegate_to = delegate_to |
| 97 | + self.impersonation_chain = impersonation_chain |
| 98 | + |
| 99 | + def _data_prep(self, data): |
| 100 | + for row in data: |
| 101 | + item_list = [] |
| 102 | + for item in row: |
| 103 | + if isinstance(item, (datetime.date, datetime.datetime)): |
| 104 | + item = item.isoformat() |
| 105 | + elif isinstance(item, int): # To exclude int from the number check. |
| 106 | + pass |
| 107 | + elif isinstance(item, numbers.Number): |
| 108 | + item = float(item) |
| 109 | + item_list.append(item) |
| 110 | + yield item_list |
| 111 | + |
| 112 | + def _get_data(self): |
| 113 | + hook = self.get_db_hook() |
| 114 | + with closing(hook.get_conn()) as conn, closing(conn.cursor()) as cur: |
| 115 | + self.log.info("Executing query") |
| 116 | + cur.execute(self.sql, self.parameters or ()) |
| 117 | + |
| 118 | + yield [field[0] for field in cur.description] |
| 119 | + yield from self._data_prep(cur.fetchall()) |
| 120 | + |
| 121 | + def execute(self, context: Any) -> None: |
| 122 | + self.log.info("Getting data") |
| 123 | + values = list(self._get_data()) |
| 124 | + |
| 125 | + self.log.info("Connecting to Google") |
| 126 | + sheet_hook = GSheetsHook( |
| 127 | + gcp_conn_id=self.gcp_conn_id, |
| 128 | + delegate_to=self.delegate_to, |
| 129 | + impersonation_chain=self.impersonation_chain, |
| 130 | + ) |
| 131 | + |
| 132 | + self.log.info(f"Uploading data to https://docs.google.com/spreadsheets/d/{self.spreadsheet_id}") |
| 133 | + |
| 134 | + sheet_hook.update_values( |
| 135 | + spreadsheet_id=self.spreadsheet_id, |
| 136 | + range_=self.spreadsheet_range, |
| 137 | + values=values, |
| 138 | + ) |
0 commit comments