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 2021/12/28 14:00:00 UTC

[GitHub] [airflow] aa3pankaj commented on a change in pull request #20459: Adding snowflake_to_s3 transfer operator, Updates in s3_to_snowflake

aa3pankaj commented on a change in pull request #20459:
URL: https://github.com/apache/airflow/pull/20459#discussion_r775921457



##########
File path: airflow/providers/snowflake/transfers/snowflake_to_s3.py
##########
@@ -0,0 +1,153 @@
+#
+# 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 Snowflake to AWS S3 operator."""
+from typing import Any, List, Optional
+
+from airflow.models import BaseOperator
+from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook
+
+
+class SnowflakeToS3Operator(BaseOperator):
+    """
+    Executes COPY command to unload data from Snowflake to s3
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:SnowflakeToS3Operator`
+
+    :param stage: reference to a specific snowflake stage. If the stage's schema is not the same as the
+        table one, it must be specified
+    :type stage: str
+    :param prefix: cloud storage location specified to limit the set of files to load
+    :type prefix: str
+    :param file_format: reference to a specific file format
+    :type file_format: str
+    :param on_error: action to be taken in case of any error, possible options are { CONTINUE | SKIP_FILE |
+        SKIP_FILE_<num> | SKIP_FILE_<num>% | ABORT_STATEMENT }
+    :type on_error: str
+    :param header: whether header (column names) needed or not in the unloaded data
+    :type header: bool
+    :param overwrite: whether unloaded data should be overwritten or not in case of same prefix
+    :type overwrite: bool
+    :param single: whether unloaded data should be in a single file or multiple files,
+        default snowflake behaviour is multiple
+    :type single: bool
+    :param unload_sql: sql that would be used for unloading data
+    :type unload_sql: str
+    :param warehouse: name of warehouse (will overwrite any warehouse
+        defined in the connection's extra JSON)
+    :type warehouse: str
+    :param database: reference to a specific database in Snowflake connection
+    :type database: str
+    :param snowflake_conn_id: Reference to
+        :ref:`Snowflake connection id<howto/connection:snowflake>`
+    :type snowflake_conn_id: str
+    :param role: name of role (will overwrite any role defined in
+        connection's extra JSON)
+    :type role: str
+    :param authenticator: authenticator for Snowflake.
+        'snowflake' (default) to use the internal Snowflake authenticator
+        'externalbrowser' to authenticate using your web browser and
+        Okta, ADFS or any other SAML 2.0-compliant identify provider
+        (IdP) that has been defined for your account
+        'https://<your_okta_account_name>.okta.com' to authenticate
+        through native Okta.
+    :type authenticator: str
+    :param session_parameters: You can set session-level parameters at
+        the time you connect to Snowflake
+    :type session_parameters: dict
+    """
+
+    def __init__(
+        self,
+        *,
+        stage: str,
+        prefix: Optional[str] = None,
+        file_format: str,
+        on_error: Optional[str] = None,
+        unload_sql: str,
+        header: Optional[bool] = False,
+        single: Optional[bool] = False,
+        overwrite: Optional[bool] = False,
+        schema: Optional[str] = None,
+        warehouse: Optional[str] = None,
+        database: Optional[str] = None,
+        autocommit: bool = True,
+        snowflake_conn_id: str = 'snowflake_default',
+        role: Optional[str] = None,
+        authenticator: Optional[str] = None,
+        session_parameters: Optional[dict] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+        self.warehouse = warehouse
+        self.database = database
+        self.stage = stage
+        self.prefix = prefix
+        self.file_format = file_format
+        self.on_error = on_error
+        self.unload_sql = unload_sql
+        self.header = header
+        self.single = single
+        self.overwrite = overwrite
+        self.schema = schema
+        self.autocommit = autocommit
+        self.snowflake_conn_id = snowflake_conn_id
+        self.role = role
+        self.authenticator = authenticator
+        self.session_parameters = session_parameters
+        self.query_ids: List[str] = []
+
+    def execute(self, context: Any) -> None:
+        snowflake_hook = SnowflakeHook(
+            snowflake_conn_id=self.snowflake_conn_id,
+            warehouse=self.warehouse,
+            database=self.database,
+            role=self.role,
+            schema=self.schema,
+            authenticator=self.authenticator,
+            session_parameters=self.session_parameters,
+        )
+
+        sql_parts = [
+            f"COPY INTO @{self.stage}/{self.prefix or ''}",

Review comment:
       Here we expect user to pass valid identifier, anyway do you expect SnowflakeToS3Operator to validate stage name using some regex?




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