在 Apache Beam 中将事件汇总到一分钟

Rolling up events to a minute in Apache Beam

所以,我有一个具有这种结构的数据流(很抱歉它在 SQL 中)

CREATE TABLE github_events
(
    event_id bigint,
    event_type text,
    event_public boolean,
    repo_id bigint,
    payload jsonb,
    repo jsonb,
    user_id bigint,
    org jsonb,
    created_at timestamp 
);

在 SQL 中,我会将此数据汇总到一分钟,如下所示:

1.Create 汇总 table 为此目的:

CREATE TABLE github_events_rollup_minute
(
    created_at timestamp,
    event_count bigint
);

2.And 填充 INSERT/SELECT:

INSERT INTO github_events_rollup_minute(
    created_at,
    event_count
)
SELECT
    date_trunc('minute', created_at) AS created_at,
    COUNT(*)the  AS event_count
FROM github_events
GROUP BY 1;

在 Apache Beam 中,我试图将事件汇总到一分钟,即根据事件的时间戳字段计算那一分钟内收到的事件总数。

Timestamp(in YYYY-MM-DDThh:mm): event_count

因此,稍后在管道中如果我们收到更多具有相同重叠时间戳的事件(由于事件接收延迟,因为客户可能离线),我们只需要获取汇总计数并增加计数对于那个时间戳。

这将允许我们在应用程序中简单地将 YYYY-MM-DDThh:mm 的计数增加 event_count

假设事件可能会延迟,但它们将始终具有 timestamp 字段。

我想在 Apache Beam 中完成同样的事情。我是 Apache Beam 的新手,我觉得我在 Beam 中遗漏了一些可以让我完成此任务的东西。 Apache Beam Programming Guide 我已经读了好几遍了。

看看 Windowing and Triggers 上的部分。您所描述的是 fixed-time windows 允许的延迟数据。管道的大致形状听起来像:

  1. 读取输入github_events数据
  2. Window 固定 windows 1 分钟,允许延迟数据
  3. 计数事件 per-window
  4. 将结果输出到github_events_rollup_minute

WindowedWordCount 示例项目演示了此模式。