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/07/08 20:50:49 UTC

[GitHub] [airflow] josh-fell commented on a diff in pull request #23881: Generic S3 to SQL

josh-fell commented on code in PR #23881:
URL: https://github.com/apache/airflow/pull/23881#discussion_r917129087


##########
tests/providers/amazon/aws/transfers/test_s3_to_sql.py:
##########
@@ -0,0 +1,89 @@
+# 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 unittest
+from unittest.mock import patch
+
+from sqlalchemy import or_
+
+from airflow import configuration, models
+from airflow.providers.amazon.aws.transfers.s3_to_sql import S3ToSqlOperator
+from airflow.utils import db
+from airflow.utils.session import create_session
+
+
+class TestS3ToSqlTransfer(unittest.TestCase):

Review Comment:
   For net-new tests, it's preferred to use `pytest` rather than `unittest`.



##########
airflow/providers/amazon/aws/transfers/s3_to_sql.py:
##########
@@ -0,0 +1,111 @@
+# 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 typing import TYPE_CHECKING, List, Optional, Sequence, Union
+
+import pandas
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base import BaseHook
+from airflow.models import BaseOperator
+from airflow.providers.amazon.aws.hooks.s3 import S3Hook
+from airflow.providers.amazon.aws.utils.s3 import fix_int_dtypes
+
+if TYPE_CHECKING:
+    from airflow.utils.context import Context
+
+
+class S3ToSqlOperator(BaseOperator):
+    """
+    Loads a file from S3 into a SQL table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:S3ToSqlOperator`
+
+    :param s3_key: path to s3 file. (templated)
+    :param destination_table: target table on sql. (templated)
+    :param file_format: input file format. CSV, JSON or Parquet. (templated)
+    :param file_options: file reader options.
+    :param source_conn_id: source connection.
+    :param destination_conn_id: destination connection.
+    :param preoperator: sql statement or list of statements to be
+        executed prior to loading the data. (templated)
+    :param insert_args: extra params for `insert_rows` method.
+    """
+
+    template_fields: Sequence[str] = ('s3_key', 'destination_table', 'file_format', 'preoperator')
+    template_ext: Sequence[str] = (
+        '.sql',
+        '.hql',
+    )
+    template_fields_renderers = {"preoperator": "sql"}
+    ui_color = '#b0f07c'
+
+    def __init__(
+        self,
+        *,
+        s3_key: str,
+        destination_table: str,
+        file_format: str,
+        file_options: Optional[dict] = None,
+        source_conn_id: str = 'aws_default',
+        destination_conn_id: str = 'sql_default',
+        preoperator: Optional[Union[str, List[str]]] = None,
+        insert_args: Optional[dict] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+        self.s3_key = s3_key
+        self.destination_table = destination_table
+        self.file_format = file_format
+        self.file_options = file_options or {}
+        self.source_conn_id = source_conn_id
+        self.destination_conn_id = destination_conn_id
+        self.preoperator = preoperator
+        self.insert_args = insert_args or {}
+
+    def execute(self, context: "Context"):
+        source_hook = S3Hook(aws_conn_id=self.source_conn_id)
+        destination_hook = BaseHook.get_hook(self.destination_conn_id)
+
+        self.log.info("Extracting data from %s", self.source_conn_id)
+        self.log.info("Download file from: \n %s", self.s3_key)
+        file = source_hook.download_file(key=self.s3_key)
+        if self.file_format == 'csv':
+            df = pandas.read_csv(file, **self.file_options)
+        elif self.file_format == 'parquet':
+            df = pandas.read_parquet(file, **self.file_options)
+        else:

Review Comment:
   Should there be another check for JSON file format or should JSON be removed from the docstring as an acceptable file format?



-- 
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