Google cloud platform 用python创建Google Cloud API批处理主题

Google cloud platform 用python创建Google Cloud API批处理主题,google-cloud-platform,google-cloud-pubsub,Google Cloud Platform,Google Cloud Pubsub,我们正在尝试使用GoogleCloudAPI将不同的主题创建调用(数十到数百个)批处理到GoogleCloudPubSub。我们如何做到这一点还不清楚 目前,我们有以下内容,但似乎找不到关于如何在批处理调用中注入createtopic请求的文档。有人知道如何做到这一点吗 api = build('pubsub', 'v1') batch = api.new_batch_http_request(callback=foo) publisher = pubsub_v1.PublisherClien

我们正在尝试使用GoogleCloudAPI将不同的主题创建调用(数十到数百个)批处理到GoogleCloudPubSub。我们如何做到这一点还不清楚

目前,我们有以下内容,但似乎找不到关于如何在批处理调用中注入createtopic请求的文档。有人知道如何做到这一点吗

api = build('pubsub', 'v1')
batch = api.new_batch_http_request(callback=foo)

publisher = pubsub_v1.PublisherClient()

for topic_name in [.....]:
  topic_path = publisher.topic_path('my-project-id', topic_name)
  request = ???.create_topic(topic_path)
  batch.add(request)

batch.execute()

谢谢

在代码中,您尝试实现两种不同的功能

  • 执行批处理请求,这意味着只在一个请求中发布大量消息。为此,您可以使用发现API。如果您愿意的话,它是为这个而存在的
  • 创建一个子主题。这是另一个API调用。您仍然可以使用discovery API或

有了它,你可以实现你想要的。

没有办法批量创建主题请求。每次调用创建主题都必须单独完成:

from google.cloud import pubsub_v1

project_id = "Your Google Cloud Project ID"
topic_name = "Your Pub/Sub topic name"

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_name)

topic = publisher.create_topic(topic_path)

print('Topic created: {}'.format(topic))
这将创建一个单独的主题,因此必须为您希望创建的每个主题重复此逻辑

唯一的批量云发布/订阅服务是发布消息。客户端库处理此操作,批处理的大小由控制。例如,假设
messages
包含要发布的消息列表,那么下面的代码将最多批处理10条消息

from google.cloud import pubsub
from google.cloud.pubsub import types

client = pubsub.PublisherClient(
  batch_settings=types.BatchSettings(max_messages=10),
)

def callback(future):
  message_id = future.result()
  print('Message published {}'.format(message_id))

for m in messages:
  future = client.publish(topic, m)
  future.add_done_callback(callback)

谢谢我希望批量创建主题请求,但显然,这是不可能的:(我将稍微澄清一下我的问题。