Reputation: 167
There is no support of triggers in BigQuery tables.
One way to do this is, creating a Pub/Sub sink and receiving this event in a cloud function.
Lets assume this data is updated/inserted in the table { name: 'abc', id: '09nv' }
I want to send this data to cloud function as soon as the table is updated.
The best I can do is to send this inside Pub/Sub message.
Pub/Sub message format:
{
"data": string, // will have to stringify the json row(s) as required format is string
"attributes": {
string: string,
...
},
"messageId": string,
"publishTime": string,
"orderingKey": string
}
However, I don't know how to send this updated data inside Pub/Sub message(data).
If there is a way other than Pub/Sub approach, can try that as well.
Upvotes: 1
Views: 1526
Reputation: 1780
With Google Pub/Sub you can publish messages to topics associated. Messages need to be encoded in the schema and specified format. You can send this message to Cloud Function and trigger by messages published from Pub/Sub topics.
You need to publish the message to a topic.
You can see this example. In addition, you can see more documentation in this link.
def publish(request):
request_json = request.get_json(silent=True)
topic_name = request_json.get("topic")
message = request_json.get("message")
if not topic_name or not message:
return ('Missing "topic" and/or "message" parameter.', 400)
print(f'Publishing message to topic {topic_name}')
Then, the Cloud function triggered from a Pub/Sub topic will send events to PubSubMessage.
You need to code the cloud function.
def hello_pubsub(event, context):
import base64
print("""This Function was triggered by messageId {} published at {} to {}
""".format(context.event_id, context.timestamp, context.resource["name"]))
if 'data' in event:
name = base64.b64decode(event['data']).decode('utf-8')
else:
name = 'World'
print('Hello {}!'.format(name))
Another option you can try is to use CloudSQL using MySQL languages. With Cloud SQL, you can use Server-level triggers.
Google Cloud SQL is an easy-to-use service that delivers fully managed SQL databases in the cloud. Google Cloud SQL provides either MySQL or PostgreSQL databases.
Upvotes: 2