|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +""" |
| 19 | +Example Airflow DAG that shows how to use SalesforceToGcsOperator. |
| 20 | +""" |
| 21 | +import os |
| 22 | + |
| 23 | +from airflow import models |
| 24 | +from airflow.providers.google.cloud.operators.bigquery import ( |
| 25 | + BigQueryCreateEmptyDatasetOperator, |
| 26 | + BigQueryCreateEmptyTableOperator, |
| 27 | + BigQueryDeleteDatasetOperator, |
| 28 | + BigQueryExecuteQueryOperator, |
| 29 | +) |
| 30 | +from airflow.providers.google.cloud.operators.gcs import GCSCreateBucketOperator, GCSDeleteBucketOperator |
| 31 | +from airflow.providers.google.cloud.transfers.gcs_to_bigquery import GCSToBigQueryOperator |
| 32 | +from airflow.providers.google.cloud.transfers.salesforce_to_gcs import SalesforceToGcsOperator |
| 33 | +from airflow.utils.dates import days_ago |
| 34 | + |
| 35 | +GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID", "example-project") |
| 36 | +GCS_BUCKET = os.environ.get("GCS_BUCKET", "airflow-salesforce-bucket") |
| 37 | +DATASET_NAME = os.environ.get("SALESFORCE_DATASET_NAME", "salesforce_test_dataset") |
| 38 | +TABLE_NAME = os.environ.get("SALESFORCE_TABLE_NAME", "salesforce_test_datatable") |
| 39 | +GCS_OBJ_PATH = os.environ.get("GCS_OBJ_PATH", "results.csv") |
| 40 | +QUERY = "SELECT Id, Name, Company, Phone, Email, CreatedDate, LastModifiedDate, IsDeleted FROM Lead" |
| 41 | +GCS_CONN_ID = os.environ.get("GCS_CONN_ID", "google_cloud_default") |
| 42 | +SALESFORCE_CONN_ID = os.environ.get("SALESFORCE_CONN_ID", "salesforce_default") |
| 43 | + |
| 44 | + |
| 45 | +with models.DAG( |
| 46 | + "example_salesforce_to_gcs", |
| 47 | + schedule_interval=None, # Override to match your needs |
| 48 | + start_date=days_ago(1), |
| 49 | +) as dag: |
| 50 | + create_bucket = GCSCreateBucketOperator( |
| 51 | + task_id="create_bucket", |
| 52 | + bucket_name=GCS_BUCKET, |
| 53 | + project_id=GCP_PROJECT_ID, |
| 54 | + gcp_conn_id=GCS_CONN_ID, |
| 55 | + ) |
| 56 | + |
| 57 | + # [START howto_operator_salesforce_to_gcs] |
| 58 | + gcs_upload_task = SalesforceToGcsOperator( |
| 59 | + query=QUERY, |
| 60 | + include_deleted=True, |
| 61 | + bucket_name=GCS_BUCKET, |
| 62 | + object_name=GCS_OBJ_PATH, |
| 63 | + salesforce_conn_id=SALESFORCE_CONN_ID, |
| 64 | + export_format='csv', |
| 65 | + coerce_to_timestamp=False, |
| 66 | + record_time_added=False, |
| 67 | + gcp_conn_id=GCS_CONN_ID, |
| 68 | + task_id="upload_to_gcs", |
| 69 | + dag=dag, |
| 70 | + ) |
| 71 | + # [END howto_operator_salesforce_to_gcs] |
| 72 | + |
| 73 | + create_dataset = BigQueryCreateEmptyDatasetOperator( |
| 74 | + task_id="create_dataset", dataset_id=DATASET_NAME, project_id=GCP_PROJECT_ID, gcp_conn_id=GCS_CONN_ID |
| 75 | + ) |
| 76 | + |
| 77 | + create_table = BigQueryCreateEmptyTableOperator( |
| 78 | + task_id="create_table", |
| 79 | + dataset_id=DATASET_NAME, |
| 80 | + table_id=TABLE_NAME, |
| 81 | + schema_fields=[ |
| 82 | + {'name': 'id', 'type': 'STRING', 'mode': 'NULLABLE'}, |
| 83 | + {'name': 'name', 'type': 'STRING', 'mode': 'NULLABLE'}, |
| 84 | + {'name': 'company', 'type': 'STRING', 'mode': 'NULLABLE'}, |
| 85 | + {'name': 'phone', 'type': 'STRING', 'mode': 'NULLABLE'}, |
| 86 | + {'name': 'email', 'type': 'STRING', 'mode': 'NULLABLE'}, |
| 87 | + {'name': 'createddate', 'type': 'STRING', 'mode': 'NULLABLE'}, |
| 88 | + {'name': 'lastmodifieddate', 'type': 'STRING', 'mode': 'NULLABLE'}, |
| 89 | + {'name': 'isdeleted', 'type': 'BOOL', 'mode': 'NULLABLE'}, |
| 90 | + ], |
| 91 | + ) |
| 92 | + |
| 93 | + load_csv = GCSToBigQueryOperator( |
| 94 | + task_id='gcs_to_bq', |
| 95 | + bucket=GCS_BUCKET, |
| 96 | + source_objects=[GCS_OBJ_PATH], |
| 97 | + destination_project_dataset_table=f"{DATASET_NAME}.{TABLE_NAME}", |
| 98 | + write_disposition='WRITE_TRUNCATE', |
| 99 | + ) |
| 100 | + |
| 101 | + read_data_from_gcs = BigQueryExecuteQueryOperator( |
| 102 | + task_id="read_data_from_gcs", |
| 103 | + sql=f"SELECT COUNT(*) FROM `{GCP_PROJECT_ID}.{DATASET_NAME}.{TABLE_NAME}`", |
| 104 | + use_legacy_sql=False, |
| 105 | + ) |
| 106 | + |
| 107 | + delete_bucket = GCSDeleteBucketOperator( |
| 108 | + task_id="delete_bucket", |
| 109 | + bucket_name=GCS_BUCKET, |
| 110 | + ) |
| 111 | + |
| 112 | + delete_dataset = BigQueryDeleteDatasetOperator( |
| 113 | + task_id="delete_dataset", |
| 114 | + project_id=GCP_PROJECT_ID, |
| 115 | + dataset_id=DATASET_NAME, |
| 116 | + delete_contents=True, |
| 117 | + ) |
| 118 | + |
| 119 | + create_bucket >> gcs_upload_task >> load_csv |
| 120 | + create_dataset >> create_table >> load_csv |
| 121 | + load_csv >> read_data_from_gcs |
| 122 | + read_data_from_gcs >> delete_bucket |
| 123 | + read_data_from_gcs >> delete_dataset |
0 commit comments