示例#1
0
    async def publish(self,
                      topic: Union[TopicPath, str],
                      data: bytes,
                      ordering_key: str = "",
                      **attrs: Mapping[str, str]) -> str:
        if isinstance(topic, str):
            topic = TopicPath.parse(topic)

        async def create_and_open():
            client = self._publisher_factory(topic)
            await client.__aenter__()
            return client

        publisher = await self._multiplexer.get_or_create(
            topic, create_and_open)
        try:
            return await publisher.publish(data=data,
                                           ordering_key=ordering_key,
                                           **attrs)
        except GoogleAPICallError as e:
            await self._multiplexer.try_erase(topic, publisher)
            raise e
def delete_lite_topic(project_number, cloud_region, zone_id, topic_id):
    # [START pubsublite_delete_topic]
    from google.api_core.exceptions import NotFound
    from google.cloud.pubsublite import AdminClient
    from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath

    # TODO(developer):
    # project_number = 1122334455
    # cloud_region = "us-central1"
    # 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)

    client = AdminClient(cloud_region)
    try:
        client.delete_topic(topic_path)
        print(f"{topic_path} deleted successfully.")
    except NotFound:
        print(f"{topic_path} not found.")
def list_lite_subscriptions_in_topic(project_number, cloud_region, zone_id,
                                     topic_id):
    # [START pubsublite_list_subscriptions_in_topic]
    from google.cloud.pubsublite import AdminClient
    from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath

    # TODO(developer):
    # project_number = 1122334455
    # cloud_region = "us-central1"
    # 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)

    client = AdminClient(cloud_region)
    response = client.list_topic_subscriptions(topic_path)

    for subscription_path in response:
        print(subscription_path)

    print(f"{len(response)} subscription(s) listed in your topic.")
def get_lite_topic(project_number, cloud_region, zone_id, topic_id):
    # [START pubsublite_get_topic]
    from google.api_core.exceptions import NotFound
    from google.cloud.pubsublite import AdminClient
    from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath

    # TODO(developer):
    # project_number = 1122334455
    # cloud_region = "us-central1"
    # 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)

    client = AdminClient(cloud_region)
    try:
        response = client.get_topic(topic_path)
        num_partitions = client.get_topic_partition_count(topic_path)
        print(f"{response.name} has {num_partitions} partition(s).")
    except NotFound:
        print(f"{topic_path} not found.")
 def create_topic(self, topic: Topic) -> Topic:
     path = TopicPath.parse(topic.name)
     return self._underlying.create_topic(parent=str(
         path.to_location_path()),
                                          topic=topic,
                                          topic_id=path.name)
def topic():
    return TopicPath.parse("projects/1/locations/us-central1-a/topics/topic")
示例#7
0
import datetime as dt
import json
import random
import time

from google.cloud.pubsublite.cloudpubsub import PublisherClient
from google.cloud.pubsublite.types import (CloudRegion, CloudZone,
                                           MessageMetadata, TopicPath)

project_number = 533637743951
cloud_region = "asia-east1"
zone_id = "a"
topic_id = "test_topic"

location = CloudZone(CloudRegion(cloud_region), zone_id)
topic_path = TopicPath(project_number, location, topic_id)

with PublisherClient() as publisher:
    for i in range(200):
        data = {
            "timestamp": dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "value": random.randint(1, 10)
        }
        data_encoded = json.dumps(data).encode("utf8")
        future = publisher.publish(topic_path, data_encoded)
        print(future.result())
        time.sleep(1)