|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +""" |
| 19 | +Example DAG using TrinoToGCSOperator. |
| 20 | +""" |
| 21 | +import os |
| 22 | +import re |
| 23 | + |
| 24 | +from airflow import models |
| 25 | +from airflow.providers.google.cloud.operators.bigquery import ( |
| 26 | + BigQueryCreateEmptyDatasetOperator, |
| 27 | + BigQueryCreateExternalTableOperator, |
| 28 | + BigQueryDeleteDatasetOperator, |
| 29 | + BigQueryExecuteQueryOperator, |
| 30 | +) |
| 31 | +from airflow.providers.google.cloud.transfers.trino_to_gcs import TrinoToGCSOperator |
| 32 | +from airflow.utils.dates import days_ago |
| 33 | + |
| 34 | +GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID", 'example-project') |
| 35 | +GCS_BUCKET = os.environ.get("GCP_TRINO_TO_GCS_BUCKET_NAME", "test-trino-to-gcs-bucket") |
| 36 | +DATASET_NAME = os.environ.get("GCP_TRINO_TO_GCS_DATASET_NAME", "test_trino_to_gcs_dataset") |
| 37 | + |
| 38 | +SOURCE_MULTIPLE_TYPES = "memory.default.test_multiple_types" |
| 39 | +SOURCE_CUSTOMER_TABLE = "tpch.sf1.customer" |
| 40 | + |
| 41 | + |
| 42 | +def safe_name(s: str) -> str: |
| 43 | + """ |
| 44 | + Remove invalid characters for filename |
| 45 | + """ |
| 46 | + return re.sub("[^0-9a-zA-Z_]+", "_", s) |
| 47 | + |
| 48 | + |
| 49 | +with models.DAG( |
| 50 | + dag_id="example_trino_to_gcs", |
| 51 | + schedule_interval=None, # Override to match your needs |
| 52 | + start_date=days_ago(1), |
| 53 | + tags=["example"], |
| 54 | +) as dag: |
| 55 | + |
| 56 | + create_dataset = BigQueryCreateEmptyDatasetOperator(task_id="create-dataset", dataset_id=DATASET_NAME) |
| 57 | + |
| 58 | + delete_dataset = BigQueryDeleteDatasetOperator( |
| 59 | + task_id="delete_dataset", dataset_id=DATASET_NAME, delete_contents=True |
| 60 | + ) |
| 61 | + |
| 62 | + # [START howto_operator_trino_to_gcs_basic] |
| 63 | + trino_to_gcs_basic = TrinoToGCSOperator( |
| 64 | + task_id="trino_to_gcs_basic", |
| 65 | + sql=f"select * from {SOURCE_MULTIPLE_TYPES}", |
| 66 | + bucket=GCS_BUCKET, |
| 67 | + filename=f"{safe_name(SOURCE_MULTIPLE_TYPES)}.{{}}.json", |
| 68 | + ) |
| 69 | + # [END howto_operator_trino_to_gcs_basic] |
| 70 | + |
| 71 | + # [START howto_operator_trino_to_gcs_multiple_types] |
| 72 | + trino_to_gcs_multiple_types = TrinoToGCSOperator( |
| 73 | + task_id="trino_to_gcs_multiple_types", |
| 74 | + sql=f"select * from {SOURCE_MULTIPLE_TYPES}", |
| 75 | + bucket=GCS_BUCKET, |
| 76 | + filename=f"{safe_name(SOURCE_MULTIPLE_TYPES)}.{{}}.json", |
| 77 | + schema_filename=f"{safe_name(SOURCE_MULTIPLE_TYPES)}-schema.json", |
| 78 | + gzip=False, |
| 79 | + ) |
| 80 | + # [END howto_operator_trino_to_gcs_multiple_types] |
| 81 | + |
| 82 | + # [START howto_operator_create_external_table_multiple_types] |
| 83 | + create_external_table_multiple_types = BigQueryCreateExternalTableOperator( |
| 84 | + task_id="create_external_table_multiple_types", |
| 85 | + bucket=GCS_BUCKET, |
| 86 | + source_objects=[f"{safe_name(SOURCE_MULTIPLE_TYPES)}.*.json"], |
| 87 | + source_format="NEWLINE_DELIMITED_JSON", |
| 88 | + destination_project_dataset_table=f"{DATASET_NAME}.{safe_name(SOURCE_MULTIPLE_TYPES)}", |
| 89 | + schema_object=f"{safe_name(SOURCE_MULTIPLE_TYPES)}-schema.json", |
| 90 | + ) |
| 91 | + # [END howto_operator_create_external_table_multiple_types] |
| 92 | + |
| 93 | + read_data_from_gcs_multiple_types = BigQueryExecuteQueryOperator( |
| 94 | + task_id="read_data_from_gcs_multiple_types", |
| 95 | + sql=f"SELECT COUNT(*) FROM `{GCP_PROJECT_ID}.{DATASET_NAME}.{safe_name(SOURCE_MULTIPLE_TYPES)}`", |
| 96 | + use_legacy_sql=False, |
| 97 | + ) |
| 98 | + |
| 99 | + # [START howto_operator_trino_to_gcs_many_chunks] |
| 100 | + trino_to_gcs_many_chunks = TrinoToGCSOperator( |
| 101 | + task_id="trino_to_gcs_many_chunks", |
| 102 | + sql=f"select * from {SOURCE_CUSTOMER_TABLE}", |
| 103 | + bucket=GCS_BUCKET, |
| 104 | + filename=f"{safe_name(SOURCE_CUSTOMER_TABLE)}.{{}}.json", |
| 105 | + schema_filename=f"{safe_name(SOURCE_CUSTOMER_TABLE)}-schema.json", |
| 106 | + approx_max_file_size_bytes=10_000_000, |
| 107 | + gzip=False, |
| 108 | + ) |
| 109 | + # [END howto_operator_trino_to_gcs_many_chunks] |
| 110 | + |
| 111 | + create_external_table_many_chunks = BigQueryCreateExternalTableOperator( |
| 112 | + task_id="create_external_table_many_chunks", |
| 113 | + bucket=GCS_BUCKET, |
| 114 | + source_objects=[f"{safe_name(SOURCE_CUSTOMER_TABLE)}.*.json"], |
| 115 | + source_format="NEWLINE_DELIMITED_JSON", |
| 116 | + destination_project_dataset_table=f"{DATASET_NAME}.{safe_name(SOURCE_CUSTOMER_TABLE)}", |
| 117 | + schema_object=f"{safe_name(SOURCE_CUSTOMER_TABLE)}-schema.json", |
| 118 | + ) |
| 119 | + |
| 120 | + # [START howto_operator_read_data_from_gcs_many_chunks] |
| 121 | + read_data_from_gcs_many_chunks = BigQueryExecuteQueryOperator( |
| 122 | + task_id="read_data_from_gcs_many_chunks", |
| 123 | + sql=f"SELECT COUNT(*) FROM `{GCP_PROJECT_ID}.{DATASET_NAME}.{safe_name(SOURCE_CUSTOMER_TABLE)}`", |
| 124 | + use_legacy_sql=False, |
| 125 | + ) |
| 126 | + # [END howto_operator_read_data_from_gcs_many_chunks] |
| 127 | + |
| 128 | + # [START howto_operator_trino_to_gcs_csv] |
| 129 | + trino_to_gcs_csv = TrinoToGCSOperator( |
| 130 | + task_id="trino_to_gcs_csv", |
| 131 | + sql=f"select * from {SOURCE_MULTIPLE_TYPES}", |
| 132 | + bucket=GCS_BUCKET, |
| 133 | + filename=f"{safe_name(SOURCE_MULTIPLE_TYPES)}.{{}}.csv", |
| 134 | + schema_filename=f"{safe_name(SOURCE_MULTIPLE_TYPES)}-schema.json", |
| 135 | + export_format="csv", |
| 136 | + ) |
| 137 | + # [END howto_operator_trino_to_gcs_csv] |
| 138 | + |
| 139 | + create_dataset >> trino_to_gcs_basic |
| 140 | + create_dataset >> trino_to_gcs_multiple_types |
| 141 | + create_dataset >> trino_to_gcs_many_chunks |
| 142 | + create_dataset >> trino_to_gcs_csv |
| 143 | + |
| 144 | + trino_to_gcs_multiple_types >> create_external_table_multiple_types >> read_data_from_gcs_multiple_types |
| 145 | + trino_to_gcs_many_chunks >> create_external_table_many_chunks >> read_data_from_gcs_many_chunks |
| 146 | + |
| 147 | + trino_to_gcs_basic >> delete_dataset |
| 148 | + trino_to_gcs_csv >> delete_dataset |
| 149 | + read_data_from_gcs_multiple_types >> delete_dataset |
| 150 | + read_data_from_gcs_many_chunks >> delete_dataset |
0 commit comments