简体   繁体   English

在 Cloud PubSub 中发布非字符串消息

[英]Publish non-string message in Cloud PubSub

Almost all of the examples of pub/sub available over web uses String messages.几乎所有可通过网络获得的发布/订阅示例都使用字符串消息。

How do I publish message other than text messages like Java Object, JSON or AVRO to topic and then consume it through subscription.如何将 Java Object、JSON 或 AVRO 等文本消息以外的消息发布到主题,然后通过订阅使用它。

Many Thanks Pari非常感谢帕里

You cannot really do that directly, as you can see here and here the published message has to be a bytestring.您不能真正直接做到这一点,正如您在此处此处所看到的,发布的消息必须是字节串。 What you can do instead is load your file, encode it to utf-8 and then publish it.您可以做的是加载您的文件,将其编码为utf-8 ,然后发布它。 Afterwards, when pulling, you can dump the message data to a file.之后,在拉取时,您可以将消息数据转储到文件中。 For instance, using python for json, you would need to publish like so:例如,将 python 用于 json,您需要像这样发布:

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

with open(json_path) as f:
    data = str(json.load(f))

data = data.encode('utf-8')
publisher.publish(topic_path, data=data)

Then you can pull and dump like so:然后你可以像这样拉和转储:

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
    project, subscription_name)

def callback(message):
    with open('received_message.json', 'w') as outfile:
        json.dump(message.data, outfile)

subscriber.subscribe(subscription_path, callback=callback)

publish JSON with Node - publishMessage :使用 Node 发布 JSON - publishMessage

const {PubSub} = require('@google-cloud/pubsub')
const pubsub = new PubSub()

const json = {
  foo: 'bar'
}

await pubsub.topic('my-topic').publishMessage({json})

As of this writing在撰写本文时

The node.js @google-cloud/pubsub library only accepts buffer objects and you'll need to stringify. node.js @google-cloud/pubsub 库只接受缓冲区对象,您需要进行字符串化。

https://github.com/googleapis/nodejs-pubsub/blob/master/samples/topics.js https://github.com/googleapis/nodejs-pubsub/blob/master/samples/topics.js

async function publishMessage(topicName, data) {

  // Imports the Google Cloud client library
  const {PubSub} = require('@google-cloud/pubsub');

  // Creates a client
  const pubsub = new PubSub();

  // Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
  const dataBuffer = Buffer.from(data);

  const messageId = await pubsub
    .topic(topicName)
    .publisher()
    .publish(dataBuffer);
  console.log(`Message ${messageId} published.`);
}

This might change soon!这可能很快就会改变!

Please follow this request/issue: https://github.com/googleapis/nodejs-pubsub/issues/121请遵循此请求/问题: https : //github.com/googleapis/nodejs-pubsub/issues/121

The library may soon be modified to accept non buffer objects and buffer them for you.该库可能很快会被修改为接受非缓冲对象并为您缓冲它们。

The following code lets you publish a message in Pub/Sub using JSON:以下代码可让您使用 JSON 在 Pub/Sub 中发布消息:

topic_path = 'your-topic-id'

publisher = pubsub_v1.PublisherClient()

record = {
    'Key1': 'Value1',
    'Key2': 'Value2',
    'Key3': 'Value3',
    'Key4': 'Value4'
}

data = json.dumps(record).encode("utf-8")
future = publisher.publish(topic_path, data)
print(f'published message id {future.result()}')

However, you must encode the JSON to UTF-8.但是,您必须将 JSON 编码为 UTF-8。

I hope that It helps you.我希望它可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将向 PubSub 发布消息的 Cloud Function(由 HTTP 触发) - Cloud Function (trigger by HTTP) that would publish a message to PubSub GCP PubSub:创建配置类以发布消息 - GCP PubSub:Creating a Configuration Class to publish message 获取 Google Cloud PubSub 中单条消息的大小 - Get the size of a single message in Google Cloud PubSub 在Compute Engine中使用Google Cloud Pubsub发布到主题 - Publish to a topic using Google Cloud Pubsub within Compute Engine 通过 mosquitto 代理发布到谷歌云中不同的 pubsub 主题? - Publish to different pubsub topic in Google cloud via mosquitto broker? 实现由GCS最终确定的发布到pubsub的云功能 - Implementing a cloud function to publish to pubsub triggered by GCS finalize Google Cloud Function 不在 PubSub 上发布,超过超时 - Google Cloud Function don't publish on PubSub, Timeout exceeded GCP Nodejs8 Cloud Function - 同步 PubSub 发布 - GCP Nodejs8 Cloud Function - Synchronous PubSub publish Stackdriver-PubSub主题的“发布消息操作”度量标准不可用 - Stackdriver - “Publish Message Operations” metric for PubSub topic is not available REST API,用于监视Google Cloud pubsub中的未传递消息 - REST API for monitoring undelivered message in google cloud pubsub
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM