如何在Node.js中发布到主题时传递partitionKey或MessageId?

huangapple go评论82阅读模式
英文:

How to pass partitionKey or MessageId when you publish to topic in NodeJS?

问题

我有一个启用了“Partitioning”和“Duplicate detection”的服务总线主题。我正在按照Microsoft的方法发布到主题。

在批量发送消息时,我遇到以下错误。

> 发生错误:ServiceBusError:InvalidOperationError:启用了重复检测的分区实体的消息必须具有PartitionKey或MessageId

问题:

如何在我的Node应用程序中传递PartitionKeyMessageId?我正在使用@azure/service-bus包。

代码:

const { ServiceBusClient } = require("@azure/service-bus");

const connectionString = "<SERVICE BUS NAMESPACE CONNECTION STRING>";
const topicName = "<TOPIC NAME>";

const messages = [
	{ body: "Albert Einstein" },
	{ body: "Werner Heisenberg" },
	{ body: "Marie Curie" },
	{ body: "Steven Hawking" },
	{ body: "Isaac Newton" },
	{ body: "Niels Bohr" },
	{ body: "Michael Faraday" },
	{ body: "Galileo Galilei" },
	{ body: "Johannes Kepler" },
	{ body: "Nikolaus Kopernikus" }
 ];

 async function main() {
	// 使用连接字符串创建Service Bus客户端以访问Service Bus命名空间
	const sbClient = new ServiceBusClient(connectionString);

	// createSender()也可以用于创建队列的发送器。
	const sender = sbClient.createSender(topicName);

	try {
		// 尝试在单个批次中发送所有消息。
		// 如果消息无法放入批次中,则会失败。
		// await sender.sendMessages(messages);

		// 创建一个批次对象
		let batch = await sender.createMessageBatch(); 
		for (let i = 0; i < messages.length; i++) {
			// 对于数组中的每条消息			

			// 尝试将消息添加到批次中
			if (!batch.tryAddMessage(messages[i])) {			
				// 如果无法将消息添加到当前批次中
				// 作为批次已满,请发送当前批次
				await sender.sendMessages(batch);

				// 然后创建一个新批次 
				batch = await sender.createMessageBatch();

				// 现在,将无法添加到上一批次的消息添加到此批次中
				if (!batch.tryAddMessage(messages[i])) {
					// 如果仍然无法将消息添加到批次中,那么消息可能太大而无法放入批次中
					throw new Error("消息太大,无法放入批次中");
				}
			}
		}

		// 将最后创建的消息批次发送到主题
		await sender.sendMessages(batch);

		console.log(`已发送一批消息到主题:${topicName}`);

		// 关闭发送器
		await sender.close();
	} finally {
		await sbClient.close();
	}
}

// 调用主函数
main().catch((err) => {
	console.log("发生错误:", err);
	process.exit(1);
 });
英文:

I have service bus topic with Partitioning and Duplicate detection enabled. I am following Microsoft approach to publish to the topic.

如何在Node.js中发布到主题时传递partitionKey或MessageId?

While sending the messages in batch, I am getting following error.

> Error occurred: ServiceBusError: InvalidOperationError: Message to a partitioned entity with duplicate detection enabled must have either PartitionKey or MessageId

Question:

How to pass PartitionKey or MessageId in my node app? I am using @azure/service-bus&quot; package.

Code:

const { ServiceBusClient } = require(&quot;@azure/service-bus&quot;);
const connectionString = &quot;&lt;SERVICE BUS NAMESPACE CONNECTION STRING&gt;&quot;
const topicName = &quot;&lt;TOPIC NAME&gt;&quot;;
const messages = [
{ body: &quot;Albert Einstein&quot; },
{ body: &quot;Werner Heisenberg&quot; },
{ body: &quot;Marie Curie&quot; },
{ body: &quot;Steven Hawking&quot; },
{ body: &quot;Isaac Newton&quot; },
{ body: &quot;Niels Bohr&quot; },
{ body: &quot;Michael Faraday&quot; },
{ body: &quot;Galileo Galilei&quot; },
{ body: &quot;Johannes Kepler&quot; },
{ body: &quot;Nikolaus Kopernikus&quot; }
];
async function main() {
// create a Service Bus client using the connection string to the Service Bus namespace
const sbClient = new ServiceBusClient(connectionString);
// createSender() can also be used to create a sender for a queue.
const sender = sbClient.createSender(topicName);
try {
// Tries to send all messages in a single batch.
// Will fail if the messages cannot fit in a batch.
// await sender.sendMessages(messages);
// create a batch object
let batch = await sender.createMessageBatch(); 
for (let i = 0; i &lt; messages.length; i++) {
// for each message in the arry			
// try to add the message to the batch
if (!batch.tryAddMessage(messages[i])) {			
// if it fails to add the message to the current batch
// send the current batch as it is full
await sender.sendMessages(batch);
// then, create a new batch 
batch = await sender.createMessageBatch();
// now, add the message failed to be added to the previous batch to this batch
if (!batch.tryAddMessage(messages[i])) {
// if it still can&#39;t be added to the batch, the message is probably too big to fit in a batch
throw new Error(&quot;Message too big to fit in a batch&quot;);
}
}
}
// Send the last created batch of messages to the topic
await sender.sendMessages(batch);
console.log(`Sent a batch of messages to the topic: ${topicName}`);
// Close the sender
await sender.close();
} finally {
await sbClient.close();
}
}
// call the main function
main().catch((err) =&gt; {
console.log(&quot;Error occurred: &quot;, err);
process.exit(1);
});

答案1

得分: 1

你只需在消息的 messageId 属性中指定消息 ID,然后在消息的 partitionKey 属性中指定分区键。例如,您的负载将如下所示:

const messages = [
    { body: "Albert Einstein", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Werner Heisenberg", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Marie Curie", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Steven Hawking", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Isaac Newton", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Niels Bohr", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Michael Faraday", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Galileo Galilei", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Johannes Kepler", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Nikolaus Kopernikus", messageId: 'message-id', partitionKey: 'partition-key' }
];

参考链接:https://learn.microsoft.com/en-us/javascript/api/@azure/service-bus/servicebusmessage?view=azure-node-latest

英文:

> How to pass PartitionKey or MessageId in my node app?

You just need to specify the message id in messageId property and partition key in partitionKey property of the message. For example, your payload would look something like:

const messages = [
{ body: &quot;Albert Einstein&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Werner Heisenberg&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Marie Curie&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Steven Hawking&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Isaac Newton&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Niels Bohr&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Michael Faraday&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Galileo Galilei&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Johannes Kepler&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; },
{ body: &quot;Nikolaus Kopernikus&quot;, messageId: &#39;message-id&#39;, partitionKey: &#39;partition-key&#39; }
];

Reference: https://learn.microsoft.com/en-us/javascript/api/@azure/service-bus/servicebusmessage?view=azure-node-latest

huangapple
  • 本文由 发表于 2023年1月11日 03:39:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75075029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定