1
votes

In my cloud function I have a query which I am executing and writing job result into new bigquery table. I want my query should be dynamic based on some unit values (using outside dynamic parameters), I am triggering this cloud function from google cloud scheduler(Which contains some parameter values in Body section (using post method with http call)), Can anybody suggest how to use this parameter values from body section of cloud scheduler into my cloud function to make my query dynamic

Passing certain parameters in Body section of cloud scheduler but dont know how to use them in cloud function.

Body of cloud scheduler:

{
'unit': 'myunitname'
'interval':'1'
}

cloud function:

import flask
from google.cloud import bigquery

app = flask.Flask(__name__)


def main(request):
    with app.app_context():
        query = "SELECT unitId FROM `myproject.mydataset.mytable` 
        where unit ='{}' and interval='{}'".format(unit,interval)
        client = bigquery.Client()
        job_config = bigquery.QueryJobConfig()
        dest_dataset = client.dataset('mydataset', 'myproject')
        dest_table = dest_dataset.table('mytable')
        job_config.destination = dest_table
        job_config.create_disposition = 'CREATE_IF_NEEDED'
        job_config.write_disposition = 'WRITE_APPEND'
        job = client.query(query, job_config=job_config)
        job.result()

        return "Triggered"
1

1 Answers

5
votes

The best way to do this is to keep all internal to Google Cloud using pub/sub:

[official Google tutorial]

Your Cloud Scheduler sends a message into Pub/Sub with a payload object of the information that your Cloud Function needs. Cloud Function should be triggered off of Pub/Sub topic and then you can access the message.attributes that you need for the dynamic portion you are referencing.

Cloud Scheduler -- publishes to --> Pub/Sub Topic -- subscriber push --> Cloud Function


If you want to keep using HTTP, you can follow here where it describes how to use POST from your function. You are using the requests module to pull the body attributes that you passed in your Cloud Scheduler job.