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/06/01 15:50:12 UTC

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

vincbeck commented on code in PR #23881:
URL: https://github.com/apache/airflow/pull/23881#discussion_r886967712


##########
airflow/operators/s3_to_sql.py:
##########
@@ -0,0 +1,117 @@
+#
+# 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.
+
+from typing import List, Optional, Sequence, Union
+
+import numpy
+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.utils.context import Context
+
+
+class S3ToSqlOperator(BaseOperator):
+    """
+    Moves data from s3 to sql.
+
+    :param source_path: path to s3 file
+    :param destination_table: target table on sql
+    :param file_format: input file format. CSV, JSON or Parquet
+    :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] = ('source_key', 'destination_table', 'file_format', 'preoperator')
+    template_ext: Sequence[str] = (
+        '.sql',
+        '.hql',
+    )
+    template_fields_renderers = {"preoperator": "sql"}
+    ui_color = '#b0f07c'
+
+    def __init__(
+        self,
+        *,
+        source_path: 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.source_key = source_path
+        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 {}
+
+    @staticmethod
+    def _fix_int_dtypes(df: pandas.DataFrame) -> None:

Review Comment:
   I would do it otherwise:
   - I would create a file `airflow/providers/amazon/aws/utils/s3.py`
   - I would move this function into this file
   - I would drop the `_` from the function name
   - I would use this function in `SqlToS3Operator ` and `S3ToSqlOperator `



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