Source code for tests.system.google.cloud.dataflow.example_dataflow_sql
## 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."""Example Airflow DAG for Google Cloud Dataflow service"""from__future__importannotationsimportosfromdatetimeimportdatetimefromairflow.models.dagimportDAGfromairflow.providers.google.cloud.operators.bigqueryimport(BigQueryCreateEmptyDatasetOperator,BigQueryCreateEmptyTableOperator,BigQueryDeleteDatasetOperator,BigQueryDeleteTableOperator,BigQueryInsertJobOperator,)fromairflow.providers.google.cloud.operators.dataflowimportDataflowStartSqlJobOperatorfromairflow.utils.trigger_ruleimportTriggerRulefromproviders.tests.system.googleimportDEFAULT_GCP_SYSTEM_TEST_PROJECT_ID
create_bq_table=BigQueryCreateEmptyTableOperator(task_id="create_bq_table",dataset_id=BQ_SQL_DATASET,table_id=BQ_SQL_TABLE_INPUT,schema_fields=[{"name":"emp_name","type":"STRING","mode":"REQUIRED"},{"name":"salary","type":"INTEGER","mode":"NULLABLE"},],)insert_query_job=BigQueryInsertJobOperator(task_id="insert_query_job",configuration={"query":{"query":INSERT_ROWS_QUERY,"useLegacySql":False,"priority":"BATCH",}},location=LOCATION,)# [START howto_operator_start_sql_job]start_sql=DataflowStartSqlJobOperator(task_id="start_sql_query",job_name=DATAFLOW_SQL_JOB_NAME,query=f""" SELECT emp_name as employee, salary as employee_salary FROM bigquery.table.`{PROJECT_ID}`.`{BQ_SQL_DATASET}`.`{BQ_SQL_TABLE_INPUT}` WHERE salary >= 1000; """,options={"bigquery-project":PROJECT_ID,"bigquery-dataset":BQ_SQL_DATASET,"bigquery-table":BQ_SQL_TABLE_OUTPUT,},location=LOCATION,do_xcom_push=True,)# [END howto_operator_start_sql_job]delete_bq_table=BigQueryDeleteTableOperator(task_id="delete_bq_table",deletion_dataset_table=f"{PROJECT_ID}.{BQ_SQL_DATASET}.{BQ_SQL_TABLE_INPUT}",trigger_rule=TriggerRule.ALL_DONE,)delete_bq_dataset=BigQueryDeleteDatasetOperator(task_id="delete_bq_dataset",dataset_id=BQ_SQL_DATASET,delete_contents=True,trigger_rule=TriggerRule.ALL_DONE,)(# TEST SETUPcreate_bq_dataset>>create_bq_table>>insert_query_job# TEST BODY>>start_sql# TEST TEARDOWN>>delete_bq_table>>delete_bq_dataset)fromdev.tests_common.test_utils.watcherimportwatcher# This test needs watcher in order to properly mark success/failure# when "tearDown" task with trigger rule is part of the DAGlist(dag.tasks)>>watcher()fromdev.tests_common.test_utils.system_testsimportget_test_run# noqa: E402# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)