Source code for tests.system.providers.ydb.example_ydb
# 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__future__importannotationsimportdatetimeimportosimportydbfromairflowimportDAGfromairflow.decoratorsimporttaskfromairflow.providers.ydb.hooks.ydbimportYDBHookfromairflow.providers.ydb.operators.ydbimportYDBExecuteQueryOperator# [START ydb_operator_howto_guide]# create_pet_table, populate_pet_table, get_all_pets, and get_birth_date are examples of tasks created by# instantiating the YDB Operator
[docs]create_pet_table=YDBExecuteQueryOperator(task_id="create_pet_table",sql=""" CREATE TABLE pet ( pet_id INT, name TEXT NOT NULL, pet_type TEXT NOT NULL, birth_date TEXT NOT NULL, owner TEXT NOT NULL, PRIMARY KEY (pet_id) ); """,is_ddl=True,# must be specified for DDL queries)
# [END ydb_operator_howto_guide_create_pet_table]# [START ydb_operator_howto_guide_populate_pet_table]populate_pet_table=YDBExecuteQueryOperator(task_id="populate_pet_table",sql=""" UPSERT INTO pet (pet_id, name, pet_type, birth_date, owner) VALUES (1, 'Max', 'Dog', '2018-07-05', 'Jane'); UPSERT INTO pet (pet_id, name, pet_type, birth_date, owner) VALUES (2, 'Susie', 'Cat', '2019-05-01', 'Phil'); """,)# [END ydb_operator_howto_guide_populate_pet_table]# [START ydb_operator_howto_guide_get_all_pets]get_all_pets=YDBExecuteQueryOperator(task_id="get_all_pets",sql="SELECT * FROM pet;")# [END ydb_operator_howto_guide_get_all_pets]# [START ydb_operator_howto_guide_get_birth_date]get_birth_date=YDBExecuteQueryOperator(task_id="get_birth_date",sql="SELECT * FROM pet WHERE birth_date BETWEEN '{{params.begin_date}}' AND '{{params.end_date}}'",params={"begin_date":"2020-01-01","end_date":"2020-12-31"},)# [END ydb_operator_howto_guide_get_birth_date](create_pet_table>>populate_pet_table>>populate_pet_table_via_bulk_upsert()>>get_all_pets>>get_birth_date)# [END ydb_operator_howto_guide]fromtests.system.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()fromtests.system.utilsimportget_test_run# noqa: E402# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)