|
19 | 19 | Example Airflow DAG that shows how to use DisplayVideo.
|
20 | 20 | """
|
21 | 21 | import os
|
| 22 | +from typing import Dict |
22 | 23 |
|
23 | 24 | from airflow import models
|
| 25 | +from airflow.providers.google.cloud.operators.gcs_to_bigquery import GCSToBigQueryOperator |
24 | 26 | from airflow.providers.google.marketing_platform.operators.display_video import (
|
25 |
| - GoogleDisplayVideo360CreateReportOperator, GoogleDisplayVideo360DeleteReportOperator, |
26 |
| - GoogleDisplayVideo360DownloadLineItemsOperator, GoogleDisplayVideo360DownloadReportOperator, |
27 |
| - GoogleDisplayVideo360RunReportOperator, GoogleDisplayVideo360UploadLineItemsOperator, |
| 27 | + GoogleDisplayVideo360CreateReportOperator, GoogleDisplayVideo360CreateSDFDownloadTaskOperator, |
| 28 | + GoogleDisplayVideo360DeleteReportOperator, GoogleDisplayVideo360DownloadLineItemsOperator, |
| 29 | + GoogleDisplayVideo360DownloadReportOperator, GoogleDisplayVideo360RunReportOperator, |
| 30 | + GoogleDisplayVideo360SDFtoGCSOperator, GoogleDisplayVideo360UploadLineItemsOperator, |
28 | 31 | )
|
29 | 32 | from airflow.providers.google.marketing_platform.sensors.display_video import (
|
30 |
| - GoogleDisplayVideo360ReportSensor, |
| 33 | + GoogleDisplayVideo360GetSDFDownloadOperationSensor, GoogleDisplayVideo360ReportSensor, |
31 | 34 | )
|
32 | 35 | from airflow.utils import dates
|
33 | 36 |
|
34 | 37 | # [START howto_display_video_env_variables]
|
35 | 38 | BUCKET = os.environ.get("GMP_DISPLAY_VIDEO_BUCKET", "gs://test-display-video-bucket")
|
36 | 39 | ADVERTISER_ID = os.environ.get("GMP_ADVERTISER_ID", 1234567)
|
37 | 40 | OBJECT_NAME = os.environ.get("GMP_OBJECT_NAME", "files/report.csv")
|
| 41 | +PATH_TO_UPLOAD_FILE = os.environ.get( |
| 42 | + "GCP_GCS_PATH_TO_UPLOAD_FILE", "test-gcs-example.txt" |
| 43 | +) |
| 44 | +PATH_TO_SAVED_FILE = os.environ.get( |
| 45 | + "GCP_GCS_PATH_TO_SAVED_FILE", "test-gcs-example-download.txt" |
| 46 | +) |
| 47 | +BUCKET_FILE_LOCATION = PATH_TO_UPLOAD_FILE.rpartition("/")[-1] |
| 48 | +SDF_VERSION = os.environ.get("GMP_SDF_VERSION", "SDF_VERSION_5_1") |
| 49 | +BQ_DATA_SET = os.environ.get("GMP_BQ_DATA_SET", "airflow_test") |
38 | 50 |
|
39 | 51 | REPORT = {
|
40 | 52 | "kind": "doubleclickbidmanager#query",
|
|
55 | 67 | }
|
56 | 68 |
|
57 | 69 | PARAMS = {"dataRange": "LAST_14_DAYS", "timezoneCode": "America/New_York"}
|
| 70 | + |
| 71 | +BODY_REQUEST: Dict = { |
| 72 | + "version": SDF_VERSION, |
| 73 | + "advertiserId": ADVERTISER_ID, |
| 74 | + "inventorySourceFilter": {"inventorySourceIds": []}, |
| 75 | +} |
58 | 76 | # [END howto_display_video_env_variables]
|
59 | 77 |
|
60 | 78 | # download_line_items variables
|
61 |
| -REQUEST_BODY = { |
62 |
| - "filterType": ADVERTISER_ID, |
63 |
| - "format": "CSV", |
64 |
| - "fileSpec": "EWF" |
65 |
| -} |
| 79 | +REQUEST_BODY = {"filterType": ADVERTISER_ID, "format": "CSV", "fileSpec": "EWF"} |
66 | 80 |
|
67 | 81 | default_args = {"start_date": dates.days_ago(1)}
|
68 | 82 |
|
|
119 | 133 | upload_line_items = GoogleDisplayVideo360UploadLineItemsOperator(
|
120 | 134 | task_id="upload_line_items",
|
121 | 135 | bucket_name=BUCKET,
|
122 |
| - object_name=OBJECT_NAME, |
| 136 | + object_name=BUCKET_FILE_LOCATION, |
123 | 137 | )
|
124 | 138 | # [END howto_google_display_video_upload_line_items_operator]
|
| 139 | + |
| 140 | + # [START howto_google_display_video_create_sdf_download_task_operator] |
| 141 | + create_sdf_download_task = GoogleDisplayVideo360CreateSDFDownloadTaskOperator( |
| 142 | + task_id="create_sdf_download_task", body_request=BODY_REQUEST |
| 143 | + ) |
| 144 | + operation_name = '{{ task_instance.xcom_pull("create_sdf_download_task")["name"] }}' |
| 145 | + # [END howto_google_display_video_create_sdf_download_task_operator] |
| 146 | + |
| 147 | + # [START howto_google_display_video_wait_for_operation_sensor] |
| 148 | + wait_for_operation = GoogleDisplayVideo360GetSDFDownloadOperationSensor( |
| 149 | + task_id="wait_for_operation", operation_name=operation_name, |
| 150 | + ) |
| 151 | + # [END howto_google_display_video_wait_for_operation_sensor] |
| 152 | + |
| 153 | + # [START howto_google_display_video_save_sdf_in_gcs_operator] |
| 154 | + save_sdf_in_gcs = GoogleDisplayVideo360SDFtoGCSOperator( |
| 155 | + task_id="save_sdf_in_gcs", |
| 156 | + operation_name=operation_name, |
| 157 | + bucket_name=BUCKET, |
| 158 | + object_name=BUCKET_FILE_LOCATION, |
| 159 | + gzip=False, |
| 160 | + ) |
| 161 | + # [END howto_google_display_video_save_sdf_in_gcs_operator] |
| 162 | + |
| 163 | + # [START howto_google_display_video_gcs_to_big_query_operator] |
| 164 | + upload_sdf_to_big_query = GCSToBigQueryOperator( |
| 165 | + task_id="upload_sdf_to_big_query", |
| 166 | + bucket=BUCKET, |
| 167 | + source_objects=['{{ task_instance.xcom_pull("upload_sdf_to_bigquery")}}'], |
| 168 | + destination_project_dataset_table=f"{BQ_DATA_SET}.gcs_to_bq_table", |
| 169 | + schema_fields=[ |
| 170 | + {"name": "name", "type": "STRING", "mode": "NULLABLE"}, |
| 171 | + {"name": "post_abbr", "type": "STRING", "mode": "NULLABLE"}, |
| 172 | + ], |
| 173 | + write_disposition="WRITE_TRUNCATE", |
| 174 | + dag=dag, |
| 175 | + ) |
| 176 | + # [END howto_google_display_video_gcs_to_big_query_operator] |
| 177 | + |
125 | 178 | create_report >> run_report >> wait_for_report >> get_report >> delete_report
|
| 179 | + create_sdf_download_task >> wait_for_operation >> save_sdf_in_gcs >> upload_sdf_to_big_query |
0 commit comments