|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START functions_cloudevent_storage] |
| 16 | +import functions_framework |
| 17 | +import os |
| 18 | + |
| 19 | + |
| 20 | +# Triggered by a change in a storage bucket |
| 21 | +@functions_framework.cloud_event |
| 22 | +def bq_sp_transform(cloud_event): |
| 23 | + |
| 24 | + gcs_export_bq() |
| 25 | + |
| 26 | + gcs_copy() |
| 27 | + |
| 28 | + bq_one_time_sp() |
| 29 | + |
| 30 | + |
| 31 | +def bq_one_time_sp(): |
| 32 | + |
| 33 | + PROJECT_ID = os.environ.get("PROJECT_ID") |
| 34 | + |
| 35 | + from google.cloud import bigquery |
| 36 | + |
| 37 | + client = bigquery.Client() |
| 38 | + |
| 39 | + query_string = f""" |
| 40 | + CALL `{PROJECT_ID}.ds_edw.sp_provision_lookup_tables`(); |
| 41 | + CALL `{PROJECT_ID}.ds_edw.sp_lookerstudio_report`(); |
| 42 | + CALL `{PROJECT_ID}.ds_edw.sp_bigqueryml_model`(); |
| 43 | + """ |
| 44 | + query_job = client.query(query_string) |
| 45 | + |
| 46 | + query_job.result() |
| 47 | + |
| 48 | + |
| 49 | +def gcs_export_bq(): |
| 50 | + |
| 51 | + from google.cloud import bigquery |
| 52 | + client = bigquery.Client() |
| 53 | + EXPORT_BUCKET_ID = os.environ.get("EXPORT_BUCKET_ID") |
| 54 | + |
| 55 | + destination_uri = "gs://{}/{}".format(EXPORT_BUCKET_ID, "taxi-*.Parquet") |
| 56 | + job_config = bigquery.job.ExtractJobConfig() |
| 57 | + job_config.compression = bigquery.Compression.GZIP |
| 58 | + job_config.destination_format = "PARQUET" |
| 59 | + |
| 60 | + extract_job = client.extract_table( |
| 61 | + 'bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2022', |
| 62 | + destination_uri, |
| 63 | + # Location must match that of the source table. |
| 64 | + location="US", |
| 65 | + job_config=job_config, |
| 66 | + ) # API request |
| 67 | + extract_job.result() # Waits for job to complete. |
| 68 | + |
| 69 | + |
| 70 | +def gcs_copy(): |
| 71 | + |
| 72 | + EXPORT_BUCKET_ID = os.environ.get("EXPORT_BUCKET_ID") |
| 73 | + BUCKET_ID = os.environ.get("BUCKET_ID") |
| 74 | + |
| 75 | + from google.cloud import storage |
| 76 | + |
| 77 | + storage_client = storage.Client() |
| 78 | + |
| 79 | + source_bucket = storage_client.bucket(EXPORT_BUCKET_ID) |
| 80 | + destination_bucket = storage_client.bucket(BUCKET_ID) |
| 81 | + |
| 82 | + blobs = storage_client.list_blobs(EXPORT_BUCKET_ID) |
| 83 | + |
| 84 | + blob_list = [] |
| 85 | + # Note: The call returns a response only when the iterator is consumed. |
| 86 | + for blob in blobs: |
| 87 | + blob_list.append(blob.name) |
| 88 | + print(blob.name) |
| 89 | + |
| 90 | + for blob in blob_list: |
| 91 | + source_blob = source_bucket.blob(blob) |
| 92 | + |
| 93 | + source_bucket.copy_blob( |
| 94 | + source_blob, destination_bucket, blob, |
| 95 | + ) |
0 commit comments