feluelle commented on a change in pull request #6469: [AIRFLOW-5816] S3 to 
snowflake operator
URL: https://github.com/apache/airflow/pull/6469#discussion_r348076635
 
 

 ##########
 File path: airflow/contrib/operators/s3_to_snowflake_operator.py
 ##########
 @@ -0,0 +1,110 @@
+# -*- coding: utf-8 -*-
+#
+# 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.
+
+"""
+This module contains AWS S3 to Snowflake operator.
+"""
+
+from airflow.contrib.hooks.snowflake_hook import SnowflakeHook
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+
+
+class S3ToSnowflakeTransfer(BaseOperator):
+    """
+    Executes an COPY command to load files from s3 to Snowflake
+
+    :param s3_keys: reference to a list of S3 keys
+    :type s3_keys: list
+    :param table: reference to a specific table in snowflake database
+    :type table: str
+    :param s3_bucket: reference to a specific S3 bucket
+    :type s3_bucket: str
+    :param file_format: reference to a specific file format
+    :type file_format: str
+    :param schema: reference to a specific schema in snowflake database
+    :type schema: str
+    :param columns_array: reference to a specific columns array in snowflake 
database
+    :type schema: list
+    :param snowflake_conn_id: reference to a specific snowflake database
+    :type snowflake_conn_id: str
+    """
+
+    @apply_defaults
+    def __init__(self,
+                 s3_keys,
+                 table,
+                 stage,
+                 file_format,
+                 schema,
+                 columns_array=None,
+                 autocommit=True,
+                 snowflake_conn_id='snowflake_default',
+                 *args, **kwargs):
+        super(S3ToSnowflakeTransfer, self).__init__(*args, **kwargs)
+        self.s3_keys = s3_keys
+        self.table = table
+        self.stage = stage
+        self.file_format = file_format
+        self.schema = schema
+        self.columns_array = columns_array
+        self.autocommit = autocommit
+        self.snowflake_conn_id = snowflake_conn_id
+
+    def execute(self, context):
+        snowflake_hook = 
SnowflakeHook(snowflake_conn_id=self.snowflake_conn_id)
+
+        # Snowflake won't accept list of files it has to be tuple only.
+        # but in python tuple([1]) = (1,) => which is invalid for snowflake
+        files = str(self.s3_keys)
+        files = files.replace('[', '(')
+        files = files.replace(']', ')')
+
+        # we can extend this based on stage
+        base_sql = """
+                    FROM @{stage}/
+                    files={files}
+                    file_format={file_format}
+                """.format(
+            stage=self.stage,
+            files=files,
+            file_format=self.file_format
+        )
+
+        if self.columns_array:
+            copy_query = """
+                COPY INTO {schema}.{table}({columns}) {base_sql}
+            """.format(
+                schema=self.schema,
+                table=self.table,
+                columns=",".join(self.columns_array),
+                base_sql=base_sql
+            )
+        else:
+            copy_query = """
+                    COPY INTO {schema}.{table} {base_sql}
 
 Review comment:
   ```suggestion
                   COPY INTO {schema}.{table} {base_sql}
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to