As of January 1, 2020 this library no longer supports Python 2 on the latest released version.
Library versions released prior to that date will continue to be available. For more information please
visit Python 2 support on Google Cloud.
Admin Operations¶
Admin operations are handled through the
AdminClient
class (aliased as
google.cloud.pubsublite.AdminClient
).
Instantiating an admin client requires you to provide a valid cloud region for the Pub/Sub Lite service:
from google.cloud.pubsublite import AdminClient
cloud_region = CloudRegion("us-central1")
admin_client = AdminClient(cloud_region)
Create a topic¶
To create a message, use the
create_topic()
method. This method accepts
one positional arguments: a Topic
object, where the name
of the topic is passed along as a string.
Pub/Sub Lite topics have the canonical form of
projects/{project_number}/locations/{location}/topics/{topic_id}
A location (a.k.a. zone) is comprised of a cloud region and a zone ID.
A call to create a Pub/Sub Lite topic looks like:
from google.cloud.pubsublite import AdminClient, Topic
from google.cloud.pubsublite.types import (
CloudRegion, CloudZone, TopicPath,
)
project_number = 1122334455
zone_id = "a"
topic_id = "your-topic-id"
cloud_region = CloudRegion(cloud_region)
location = CloudZone(cloud_region, zone_id)
topic_path = TopicPath(project_number, location, topic_id)
topic = Topic(
name=str(topic_path),
partition_config=Topic.PartitionConfig(
# 1 partition
count=1,
# Publish at 4 MiB/s and subscribe at 8 MiB/s
capacity=Topic.PartitionConfig.Capacity(
publish_mib_per_sec=4,
subscribe_mib_per_sec=8,
),
),
retention_config=Topic.RetentionConfig(
# 30 GiB
per_partition_bytes=30 * 1024 * 1024 * 1024,
# 7 days
period=Duration(seconds=60 * 60 * 24 * 7),
),
)
admin_client = AdminClient(cloud_region)
response = admin_client.create_topic(topic)