ephraimbuddy commented on a change in pull request #19137: URL: https://github.com/apache/airflow/pull/19137#discussion_r734277642
########## File path: airflow/providers/amazon/aws/example_dags/example_redshift_data_execute_sql.py ########## @@ -0,0 +1,118 @@ +# 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 datetime import datetime, timedelta +from os import getenv +from time import sleep + +from airflow import DAG +from airflow.decorators import task +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.redshift import RedshiftDataHook + +# [START howto_operator_redshift_data_env_variables] +REDSHIFT_CLUSTER_IDENTIFIER = getenv("REDSHIFT_CLUSTER_IDENTIFIER", "test-cluster") +REDSHIFT_DATABASE = getenv("REDSHIFT_DATABASE", "test-database") +REDSHIFT_DATABASE_USER = getenv("REDSHIFT_DATABASE_USER", "awsuser") +# [END howto_operator_redshift_data_env_variables] + +REDSHIFT_QUERY = """ +SELECT table_schema, + table_name +FROM information_schema.tables +WHERE table_schema NOT IN ('information_schema', 'pg_catalog') + AND table_type = 'BASE TABLE' +ORDER BY table_schema, + table_name; + """ +POLL_INTERVAL = 10 +TIMEOUT = 600 + + +@task(task_id="execute_query") +def execute_query_fn(): + """This is a python callback that executes a Redshift query""" + hook = RedshiftDataHook() + + resp = hook.execute_statement( + cluster_identifier=REDSHIFT_CLUSTER_IDENTIFIER, + database=REDSHIFT_DATABASE, + db_user=REDSHIFT_DATABASE_USER, + sql=REDSHIFT_QUERY, + ) + return resp + + +@task(task_id="wait_for_results") +def wait_for_results_fn(id): + """This is a python callback that executes a Redshift query""" Review comment: ```suggestion """This is a python decorator task that executes a Redshift query""" ``` ########## File path: airflow/providers/amazon/aws/example_dags/example_redshift_data_execute_sql.py ########## @@ -0,0 +1,118 @@ +# 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 datetime import datetime, timedelta +from os import getenv +from time import sleep + +from airflow import DAG +from airflow.decorators import task +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.redshift import RedshiftDataHook + +# [START howto_operator_redshift_data_env_variables] +REDSHIFT_CLUSTER_IDENTIFIER = getenv("REDSHIFT_CLUSTER_IDENTIFIER", "test-cluster") +REDSHIFT_DATABASE = getenv("REDSHIFT_DATABASE", "test-database") +REDSHIFT_DATABASE_USER = getenv("REDSHIFT_DATABASE_USER", "awsuser") +# [END howto_operator_redshift_data_env_variables] + +REDSHIFT_QUERY = """ +SELECT table_schema, + table_name +FROM information_schema.tables +WHERE table_schema NOT IN ('information_schema', 'pg_catalog') + AND table_type = 'BASE TABLE' +ORDER BY table_schema, + table_name; + """ +POLL_INTERVAL = 10 +TIMEOUT = 600 + + +@task(task_id="execute_query") +def execute_query_fn(): + """This is a python callback that executes a Redshift query""" + hook = RedshiftDataHook() + + resp = hook.execute_statement( + cluster_identifier=REDSHIFT_CLUSTER_IDENTIFIER, + database=REDSHIFT_DATABASE, + db_user=REDSHIFT_DATABASE_USER, + sql=REDSHIFT_QUERY, + ) + return resp + + +@task(task_id="wait_for_results") +def wait_for_results_fn(id): + """This is a python callback that executes a Redshift query""" + hook = RedshiftDataHook() + + elapsed = 0 + while elapsed < TIMEOUT: + print("Polling", id) + status = hook.describe_statement( + id=id, + ) + print(status) + if status == 'FINISHED': + return status + elif status == 'FAILED': + raise ValueError(f"RedshiftDataHook.describe_statement {status}") + elif status == 'ABORTED': + raise ValueError(f"Query {status}") + else: + print(f"Query {status}") + sleep(POLL_INTERVAL) + elapsed = elapsed + POLL_INTERVAL + + raise AirflowException("Timeout. The operation could not be completed within the allotted time.") + + +@task(task_id="output_results") +def output_results_fn(id): + """This is a python callback that returns a Redshift query""" + hook = RedshiftDataHook() + + resp = hook.get_statement_result( + id=id, + ) + print(resp) + return resp + + +with DAG( + dag_id='example_redshift_data', + schedule_interval=None, + start_date=datetime(2021, 1, 1), + dagrun_timeout=timedelta(minutes=60), + tags=['example'], + catchup=False, +) as dag: + # [START howto_redshift_data] + + # Using a task-decorated function to request the list of tables in a Redshift cluster + redshift_query = execute_query_fn() + + # Using a task-decorated function to wait for the Redshift query above Review comment: Since you are using the Taskflow API, I suggest you also use `@dag` decorator ########## File path: airflow/providers/amazon/aws/example_dags/example_redshift_data_execute_sql.py ########## @@ -0,0 +1,118 @@ +# 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 datetime import datetime, timedelta +from os import getenv +from time import sleep + +from airflow import DAG +from airflow.decorators import task +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.redshift import RedshiftDataHook + +# [START howto_operator_redshift_data_env_variables] +REDSHIFT_CLUSTER_IDENTIFIER = getenv("REDSHIFT_CLUSTER_IDENTIFIER", "test-cluster") +REDSHIFT_DATABASE = getenv("REDSHIFT_DATABASE", "test-database") +REDSHIFT_DATABASE_USER = getenv("REDSHIFT_DATABASE_USER", "awsuser") +# [END howto_operator_redshift_data_env_variables] + +REDSHIFT_QUERY = """ +SELECT table_schema, + table_name +FROM information_schema.tables +WHERE table_schema NOT IN ('information_schema', 'pg_catalog') + AND table_type = 'BASE TABLE' +ORDER BY table_schema, + table_name; + """ +POLL_INTERVAL = 10 +TIMEOUT = 600 + + +@task(task_id="execute_query") +def execute_query_fn(): + """This is a python callback that executes a Redshift query""" Review comment: ```suggestion """This is a python decorator task that executes a Redshift query""" ``` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
