[英]Get messages from event hub to variable
I am absolutely new to Azure Event Hub.我对 Azure 事件中心完全陌生。 I checked documentation and see that messages are set to console log.
我检查了文档并看到消息设置为控制台日志。
async function main() {
console.log(`Running receiveEvents sample`);
const consumerClient = new EventHubConsumerClient(consumerGroup, connectionString);
const subscription = consumerClient.subscribe(
{
// The callback where you add your code to process incoming events
processEvents: async (events, context) => {
// Note: It is possible for `events` to be an empty array.
// This can happen if there were no new events to receive
// in the `maxWaitTimeInSeconds`, which is defaulted to
// 60 seconds.
// The `maxWaitTimeInSeconds` can be changed by setting
// it in the `options` passed to `subscribe()`.
for (const event of events) {
console.log(
`Received event: '${JSON.stringify(event.body)}' from partition: '${context.partitionId}' and consumer group: '${context.consumerGroup}'`
);
}
},
processError: async (err, context) => {
console.log(`Error : ${err}`);
}
},
{ startPosition: earliestEventPosition }
);
// Wait for a bit before cleaning up the sample
setTimeout(async () => {
await subscription.close();
await consumerClient.close();
console.log(`Exiting receiveEvents sample`);
}, 30 * 1000);
}
main().catch((error) => {
console.error("Error running sample:", error);
});
I would like to have this one `Received event:我想要这个`Received 事件:
${JSON.stringify(event.body)}
in variable and then operate it in thenable.在变量中,然后在 thenable 中操作它。
How can I do this?我怎样才能做到这一点? Also is any possibility to filter events?
还有可能过滤事件吗? I haven't found any example.
我没有找到任何例子。
What I understood from your question, you want a thenable (promise) async processing of the event.我从您的问题中了解到,您想要对事件进行可实现的(承诺)异步处理。 You can do something like below.
您可以执行以下操作。
async function main() {
console.log(`Running receiveEvents sample`);
const consumerClient = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);
const subscription = consumerClient.subscribe(
{
// The callback where you add your code to process incoming events
processEvents: async (events, context) => {
// Note: It is possible for `events` to be an empty array.
// This can happen if there were no new events to receive
// in the `maxWaitTimeInSeconds`, which is defaulted to
// 60 seconds.
// The `maxWaitTimeInSeconds` can be changed by setting
// it in the `options` passed to `subscribe()`.
for (const event of events) {
console.log(
`Received event: '${JSON.stringify(event.body)}' from partition: '${context.partitionId}' and consumer group: '${context.consumerGroup}'`
);
processAsync(event)
.then(e => {
console.log('Done processing event');
});
}
},
processError: async (err, context) => {
console.log(`Error : ${err}`);
}
},
{ startPosition: earliestEventPosition }
);
const processAsync = async (event) => {
// do something
// you have access to the full event object
return event;
};
// Wait for a bit before cleaning up the sample
setTimeout(async () => {
await subscription.close();
await consumerClient.close();
console.log(`Exiting receiveEvents sample`);
}, 86400 * 1000);
}
main().catch((error) => {
console.error("Error running sample:", error);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.