Apache Beam从事件流创建时间序列

时间:2020-02-18 16:42:10

标签: java apache-beam windowing

我正在尝试创建给定时间内发生的事件计数的时间序列。

事件编码为

PCollection<KV<String, Long>> events;

其中String是事件源的ID,而long是事件的时间戳。

我想要的是PCollection<Timeseries>时间序列,其格式为

class Timeseries  {
  String id;
  List<TimeseriesWindow> windows;
}

class TimeseriesWindow  {
  long timestamp;
  long count;
}

玩具示例,其窗口大小固定为 10秒(这是正确的术语吗?),总时间序列持续时间为 60秒

输入:

[("one", 1), ("one", 13), ("one", 2), ("one", 43), ("two", 3)]

输出:

[
  {
    id: "one"
    windows: [
      {
        timestamp: 0,
        count: 2
      },
      {
        timestamp: 10,
        count: 1
      },
      {
        timestamp: 20,
        count: 0
      },
      {
        timestamp: 30,
        count: 0
      },
      {
        timestamp: 40,
        count: 1
      },
      {
        timestamp: 50,
        count: 0
      }
    ]
  },
  {
    id: "two"
    windows: [
      {
        timestamp: 0,
        count: 1
      },
      {
        timestamp: 10,
        count: 0
      },
      {
        timestamp: 20,
        count: 0
      },
      {
        timestamp: 30,
        count: 0
      },
      {
        timestamp: 40,
        count: 0
      },
      {
        timestamp: 50,
        count: 0
      }
    ]
  }
]

我希望这是有道理的:)

1 个答案:

答案 0 :(得分:0)

您可以执行GroupByKey将输入转换为

[
    ("one", [1, 13, 2, 43]),
    ("two", [3]),
]

这时您可以应用DoFn将整数列表转换为Timeseries对象(例如,通过在适当的时间创建TimeseriesWindow的列表,然后迭代增加计数的值)。

您也可以查看builtin windowing capabilities,看看是否可以满足您的需求。

相关问题