from google.cloud import pubsub_v1 from google.api_core.exceptions import DeadlineExceeded import csv import os import asyncio # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json" project_id = "fp-20230425" # project_id = "svig-384801" ''' topic_name = "fp-topic-NFL-zipcodes" subscription_name = "fp-subscription-NFL-zipcodes" subscriber = pubsub_v1.SubscriberClient() subscription_path = f"projects/{project_id}/subscriptions/{subscription_name}" subscriber.delete_subscription(request={"subscription": subscription_path}) print("Subscription deleted: {}".format(subscription_path)) # Delete the topic publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path(project_id, topic_name) publisher.delete_topic(request={"topic": topic_path}) print(f"Topic '{topic_name}' deleted.") ''' # pubsub topic publisher = pubsub_v1.PublisherClient() topic_name = "fp-topic-NFL-zipcodes" topic_path = publisher.topic_path(project_id, topic_name) # Create a Pub/Sub subscription subscriber = pubsub_v1.SubscriberClient() ''' subscription_name = "fp-subscription-zipcodes" subscription_path = subscriber.subscription_path(project_id, subscription_name) subscription = subscriber.create_subscription( name=subscription_path, topic=topic_path ) ''' # Publish a message to the topic # Read the data from the csv file # file_path = "C:/Users/Karen/Desktop/zipcodes.csv" file_path = "zipcodes.csv" with open(file_path, "r") as csv_file: reader = csv.reader(csv_file) next(reader) # skip header row for row in reader: # Format the data as a string and publish it to the topic data = ",".join(row).encode("utf-8") try: future = publisher.publish(topic_path, data) print(f"Published message ID {future.result()}") except DeadlineExceeded as de: print(f"Caught DeadlineExceeded exception: {de}") ''' async def receive_messages(): response = subscriber.pull(subscription=subscription_path, max_messages=1) for received_message in response.received_messages: print("Received message: {}".format(received_message.message.data)) asyncio.run(receive_messages()) '''