Forked from antoniocachuan/message_generator_pubsub.py
Last active
August 11, 2022 17:50
-
-
Save fonylew/01323b3ed8a11b0508aac6fc40892bc6 to your computer and use it in GitHub Desktop.
Simple Message generator for Google Cloud Pub/Sub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Code modified from https://cloud.google.com/dataflow/docs/samples/join-streaming-data-with-sql#expandable-3 | |
import datetime, json, os, random, time | |
# Set the `project` variable to a Google Cloud project ID. | |
project = 'GCP-PROJECT-ID' | |
BRANCH = ['LIM', 'BOG', 'SFO', 'LAX', 'PEK', 'ATL', 'CDG', 'AMS', | |
'HKG', 'ICN', 'FRA', 'MAD', 'SEA', 'LAS', 'SIN', 'BKK', 'DFW', | |
'DXB', 'LHR', 'DEL', 'BCN', 'SZX', 'KUL', 'JFK'] | |
TRANSACTION = ['A00001', 'B00001', 'C00001', 'D00001', 'E00001', 'F00001', 'G00001', | |
'H00001', 'I00001', 'F00001', 'G00001', 'H00001', 'I00001', 'J00001', 'K00001', 'L00001', | |
'M00001', 'N00001', 'O00001', 'P00001', 'Q00001', 'R00001', 'S00001', 'T00001', 'U00001'] | |
while True: | |
data = { | |
'tr_time_str': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | |
'branch_txn': random.choice(BRANCH), | |
'transaction_id': random.choice(TRANSACTION), | |
'price': float(random.randrange(50000, 70000)) / 100, | |
} | |
# For a more complete example on how to publish messages in Pub/Sub. | |
# https://cloud.google.com/pubsub/docs/publisher | |
message = json.dumps(data) | |
command = "gcloud --project={} pubsub topics publish sales --message='{}'".format(project, message) | |
print(command) | |
os.system(command) | |
time.sleep(random.randrange(1, 5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment