使用 Cloud Function 批量读取来自 Pub/Sub 的消息
Reading messages from Pub/Sub in batches using Cloud Function
完成本指南:
https://cloud.google.com/functions/docs/tutorials/pubsub
我 运行 遇到一个问题,我需要分批读取来自 Pub/Sub 的消息,每批 1000 条。我将从我的云功能中将消息分批发布到远程 API。
简而言之,每次调用需要从 Pub/Sub 读取 1000 条消息。
我之前使用 batch-size
参数对 Kinesis 和 Lambda 做过类似的事情,但没有找到类似的云功能配置。
aws lambda create-event-source-mapping --region us-west-2 --function-name kinesis-to-bigquery --event-source <arn of the kinesis stream> --batch-size 1000 --starting-position TRIM_HORIZON
函数:
// Pub/Sub function
export function helloPubSub (event, callback) {
const pubsubMessage = event.data;
const name = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : 'World';
console.log(`Hello, ${name}!`);
callback();
}
我的问题是这是否可以使用 Cloud Function 或者是否存在其他方法来解决这个问题。
Cloud Functions 不适用于 pub/sub 这样的 - 您不会从队列中读取消息。相反,事件会尽快 单独传送到您的函数。如果您想等到收到 1000 条消息,则必须使用其他一些持久性机制自行将它们排队,然后在有足够的可用消息时对它们采取行动。
完成本指南: https://cloud.google.com/functions/docs/tutorials/pubsub
我 运行 遇到一个问题,我需要分批读取来自 Pub/Sub 的消息,每批 1000 条。我将从我的云功能中将消息分批发布到远程 API。
简而言之,每次调用需要从 Pub/Sub 读取 1000 条消息。
我之前使用 batch-size
参数对 Kinesis 和 Lambda 做过类似的事情,但没有找到类似的云功能配置。
aws lambda create-event-source-mapping --region us-west-2 --function-name kinesis-to-bigquery --event-source <arn of the kinesis stream> --batch-size 1000 --starting-position TRIM_HORIZON
函数:
// Pub/Sub function
export function helloPubSub (event, callback) {
const pubsubMessage = event.data;
const name = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : 'World';
console.log(`Hello, ${name}!`);
callback();
}
我的问题是这是否可以使用 Cloud Function 或者是否存在其他方法来解决这个问题。
Cloud Functions 不适用于 pub/sub 这样的 - 您不会从队列中读取消息。相反,事件会尽快 单独传送到您的函数。如果您想等到收到 1000 条消息,则必须使用其他一些持久性机制自行将它们排队,然后在有足够的可用消息时对它们采取行动。