-
Notifications
You must be signed in to change notification settings - Fork 130
Add import aggegation helper #1829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed 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. | ||
|
|
||
| import functions_framework | ||
| from google.cloud import bigquery | ||
| import logging | ||
| from flask import jsonify | ||
| import os | ||
|
|
||
| # Initialize BigQuery Client | ||
| try: | ||
| bq_client = bigquery.Client() | ||
| except Exception as e: | ||
| logging.warning(f"Failed to initialize BigQuery client: {e}") | ||
| bq_client = None | ||
|
|
||
| BQ_DATASET_ID = os.environ.get('BQ_DATASET_ID') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| SPANNER_PROJECT_ID = os.environ.get('SPANNER_PROJECT_ID') | ||
| SPANNER_INSTANCE_ID = os.environ.get('SPANNER_INSTANCE_ID') | ||
| SPANNER_DATABASE_ID = os.environ.get('SPANNER_DATABASE_ID') | ||
| GCS_BUCKET_ID = os.environ.get('GCS_BUCKET_ID') | ||
|
|
||
|
|
||
| @functions_framework.http | ||
| def aggregation_helper(request): | ||
| """ | ||
| HTTP Cloud Function that takes importName and runs a BQ query. | ||
| """ | ||
| if not bq_client: | ||
| return ('BigQuery client not initialized', 500) | ||
|
|
||
| request_json = request.get_json(silent=True) | ||
| if not request_json: | ||
| return ('Request is not a valid JSON', 400) | ||
|
|
||
| import_list = request_json.get('importList') | ||
| if not import_list: | ||
| return ("'importList' parameter is missing", 400) | ||
|
|
||
| logging.info(f"Received request for importList: {import_list}") | ||
|
|
||
| results = [] | ||
|
|
||
| try: | ||
| for import_item in import_list: | ||
| import_name = import_item.get('importName') | ||
| if not import_name: | ||
| logging.warning( | ||
| f"Skipping item without importName: {import_item}") | ||
| continue | ||
|
|
||
| query = None | ||
| # Define specific queries based on importName | ||
| if "india_census" in import_name: | ||
| # Placeholder for India Census specific logic | ||
| query = """ | ||
| SELECT @import_name as import_name, CURRENT_TIMESTAMP() as execution_time | ||
| """ | ||
| elif "us_census" in import_name: | ||
| # Placeholder for US Census specific logic | ||
| query = """ | ||
| SELECT @import_name as import_name, CURRENT_TIMESTAMP() as execution_time | ||
| """ | ||
| else: | ||
| logging.info( | ||
| f"No specific aggregation logic for import: {import_name}") | ||
| continue | ||
|
|
||
| if query: | ||
| job_config = bigquery.QueryJobConfig(query_parameters=[ | ||
| bigquery.ScalarQueryParameter("import_name", "STRING", | ||
| import_name), | ||
| ]) | ||
| query_job = bq_client.query(query, job_config=job_config) | ||
| query_results = query_job.result() | ||
| for row in query_results: | ||
| results.append(dict(row)) | ||
|
|
||
| return jsonify({"status": "success"}), 200 | ||
|
|
||
| except Exception as e: | ||
| logging.error(f"Aggregation failed: {e}") | ||
| return (f"Aggregation failed: {str(e)}", 500) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| functions-framework==3.* | ||
| google-cloud-bigquery | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ substitutions: | |
| _GCS_BUCKET_ID: 'datcom-prod-imports' | ||
| _LOCATION: 'us-central1' | ||
| _GCS_MOUNT_BUCKET: 'datcom-volume-mount' | ||
| _BQ_DATASET_ID: 'datacommons' | ||
|
|
||
| steps: | ||
| - id: 'import-automation-workflow' | ||
|
|
@@ -39,6 +40,11 @@ steps: | |
| args: ['functions', 'deploy', 'spanner-ingestion-helper', '--runtime', 'python312', '--source', 'ingestion-helper', '--no-allow-unauthenticated', '--trigger-http', '--entry-point', 'ingestion_helper', '--project', '${_PROJECT_ID}', '--set-env-vars', 'PROJECT_ID=${_PROJECT_ID},SPANNER_PROJECT_ID=${_SPANNER_PROJECT_ID},SPANNER_INSTANCE_ID=${_SPANNER_INSTANCE_ID},SPANNER_DATABASE_ID=${_SPANNER_DATABASE_ID},GCS_BUCKET_ID=${_GCS_BUCKET_ID},LOCATION=${_LOCATION}'] | ||
| dir: 'import-automation/workflow' | ||
|
|
||
| - id: 'import-aggregation-helper' | ||
| name: 'gcr.io/cloud-builders/gcloud' | ||
| args: ['functions', 'deploy', 'import-aggregation-helper', '--runtime', 'python312', '--source', 'aggregation-helper', '--no-allow-unauthenticated', '--trigger-http', '--entry-point', 'aggregation_helper', '--project', '${_PROJECT_ID}', '--set-env-vars', 'PROJECT_ID=${_PROJECT_ID},SPANNER_PROJECT_ID=${_SPANNER_PROJECT_ID},SPANNER_INSTANCE_ID=${_SPANNER_INSTANCE_ID},SPANNER_DATABASE_ID=${_SPANNER_DATABASE_ID},GCS_BUCKET_ID=${_GCS_BUCKET_ID},LOCATION=${_LOCATION},BQ_DATASET_ID=${_BQ_DATASET_ID}'] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The args: ['functions', 'deploy', 'import-aggregation-helper', '--runtime', 'python312', '--source', 'aggregation-helper', '--no-allow-unauthenticated', '--trigger-http', '--entry-point', 'aggregation_helper', '--project', '${_PROJECT_ID}', '--set-env-vars', 'BQ_DATASET_ID=${_BQ_DATASET_ID}'] |
||
| dir: 'import-automation/workflow' | ||
|
|
||
| - id: 'import-automation-helper' | ||
| name: 'gcr.io/cloud-builders/gcloud' | ||
| args: ['functions', 'deploy', 'import-automation-helper', '--runtime', 'python312', '--source', 'import-helper', '--trigger-topic', 'import-automation-trigger' , '--entry-point', 'handle_feed_event', '--project', '${_PROJECT_ID}', '--set-env-vars', 'PROJECT_ID=${_PROJECT_ID},LOCATION=${_LOCATION},SPANNER_PROJECT_ID=${_SPANNER_PROJECT_ID},SPANNER_INSTANCE_ID=${_SPANNER_INSTANCE_ID},SPANNER_DATABASE_ID=${_SPANNER_DATABASE_ID},WORKFLOW_ID=spanner-ingestion-workflow'] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching a broad
Exceptioncan hide bugs and make debugging difficult. It's better to catch more specific exceptions. For BigQuery client initialization, consider catchinggoogle.auth.exceptions.DefaultCredentialsErroror other specific exceptions from thegoogle-cloud-bigquerylibrary if you know what might go wrong.