You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/01/11 13:10:54 UTC

[GitHub] [airflow] ashb commented on a change in pull request #20807: Create a generic operator SqlToS3Operator and deprecate the MySqlToS3Operator.

ashb commented on a change in pull request #20807:
URL: https://github.com/apache/airflow/pull/20807#discussion_r782129020



##########
File path: airflow/providers/amazon/aws/transfers/sql_to_s3.py
##########
@@ -0,0 +1,177 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import os
+from collections import namedtuple
+from enum import Enum
+from tempfile import NamedTemporaryFile
+from typing import TYPE_CHECKING, Iterable, Mapping, Optional, Sequence, Union
+
+import numpy as np
+import pandas as pd
+from typing_extensions import Literal
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base import BaseHook
+from airflow.hooks.dbapi import DbApiHook
+from airflow.models import BaseOperator
+from airflow.providers.amazon.aws.hooks.s3 import S3Hook
+
+if TYPE_CHECKING:
+    from airflow.utils.context import Context
+
+
+FILE_FORMAT = Enum(
+    "FILE_FORMAT",
+    "CSV, PARQUET",
+)
+
+FileOptions = namedtuple('FileOptions', ['mode', 'suffix'])
+
+FILE_OPTIONS_MAP = {
+    FILE_FORMAT.CSV: FileOptions('r+', '.csv'),
+    FILE_FORMAT.PARQUET: FileOptions('rb+', '.parquet'),
+}
+
+
+class SqlToS3Operator(BaseOperator):
+    """
+    Saves data from an specific SQL query into a file in S3.
+
+    :param query: the sql query to be executed. If you want to execute a file, place the absolute path of it,
+        ending with .sql extension. (templated)
+    :type query: str
+    :param s3_bucket: bucket where the data will be stored. (templated)
+    :type s3_bucket: str
+    :param s3_key: desired key for the file. It includes the name of the file. (templated)
+    :type s3_key: str
+    :param replace: whether or not to replace the file in S3 if it previously existed
+    :type replace: bool
+    :param sql_conn_id: reference to a specific database.
+    :type sql_conn_id: str
+    :param parameters: (optional) the parameters to render the SQL query with.
+    :type parameters: dict or iterable
+    :param aws_conn_id: reference to a specific S3 connection
+    :type aws_conn_id: str
+    :param verify: Whether or not to verify SSL certificates for S3 connection.
+        By default SSL certificates are verified.
+        You can provide the following values:
+
+        - ``False``: do not validate SSL certificates. SSL will still be used
+                (unless use_ssl is False), but SSL certificates will not be verified.
+        - ``path/to/cert/bundle.pem``: A filename of the CA cert bundle to uses.
+                You can specify this argument if you want to use a different
+                CA cert bundle than the one used by botocore.
+    :type verify: bool or str
+    :param file_format: the destination file format, only string 'csv' or 'parquet' is accepted.
+    :type file_format: str
+    :param pd_kwargs: arguments to include in ``DataFrame.to_parquet()`` or
+        ``DataFrame.to_csv()``.
+    :type pd_kwargs: dict
+    """
+
+    template_fields: Sequence[str] = (
+        's3_bucket',
+        's3_key',
+        'query',
+    )
+    template_ext: Sequence[str] = ('.sql',)
+    template_fields_renderers = {
+        "query": "sql",
+        "pd_csv_kwargs": "json",
+        "pd_kwargs": "json",
+    }
+
+    def __init__(
+        self,
+        *,
+        query: str,
+        s3_bucket: str,
+        s3_key: str,
+        sql_conn_id: str,
+        parameters: Optional[Union[Mapping, Iterable]] = None,
+        replace: bool = False,
+        aws_conn_id: str = 'aws_default',
+        verify: Optional[Union[bool, str]] = None,
+        file_format: Literal['csv', 'parquet'] = 'csv',
+        pd_kwargs: Optional[dict] = None,

Review comment:
       This isn't in the doc string.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org