0
0
GCPcloud~10 mins

Publishing messages in GCP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Pub/Sub client in Python.

GCP
from google.cloud import pubsub_v1

publisher = pubsub_v1.PublisherClient()

topic_path = publisher.topic_path('my-project', [1])
Drag options to blanks, or click blank then click option'
Aproject-id
Bmy-topic
Ctopic-name
Dsubscription
Attempts:
3 left
💡 Hint
Common Mistakes
Using project ID instead of topic name.
Using subscription name instead of topic name.
2fill in blank
medium

Complete the code to publish a message to the topic.

GCP
data = 'Hello, Cloud!'.encode('utf-8')
publisher.publish(topic_path, [1]=data)
Drag options to blanks, or click blank then click option'
Amessage
Bcontent
Cdata
Dpayload
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'message' or 'payload' instead of 'data'.
Passing a string instead of bytes.
3fill in blank
hard

Fix the error in the code to correctly publish a message with attributes.

GCP
attributes = {'origin': 'python-sample'}
publisher.publish(topic_path, data=data, [1]=attributes)
Drag options to blanks, or click blank then click option'
Aattributes
Battrs
Cmeta
Dprops
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'attrs' or 'meta'.
Passing attributes as a list instead of a dictionary.
4fill in blank
hard

Fill both blanks to create a topic path and publish a message with attributes.

GCP
topic_path = publisher.[1]('my-project', 'my-topic')
data = 'Test message'.encode('utf-8')
publisher.publish(topic_path, data=data, [2]={'source': 'test'})
Drag options to blanks, or click blank then click option'
Atopic_path
Battributes
Ctopic
Dattrs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'topic' instead of 'topic_path'.
Using 'attrs' instead of 'attributes'.
5fill in blank
hard

Fill all three blanks to create a publisher client, build a topic path, and publish a message with attributes.

GCP
from google.cloud import pubsub_v1

publisher = pubsub_v1.[1]()
topic_path = publisher.[2]('project-123', 'events')
data = 'Event data'.encode('utf-8')
publisher.publish(topic_path, data=data, [3]={'env': 'prod'})
Drag options to blanks, or click blank then click option'
APublisherClient
Btopic_path
Cattributes
DSubscriberClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using SubscriberClient instead of PublisherClient.
Using wrong method names or parameter names.