【问题标题】:Apache Beam create timeseries from event streamApache Beam 从事件流创建时间序列
【发布时间】:2020-02-18 16:42:10
【问题描述】:

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

事件被编码为

PCollection<KV<String, Long>> events;

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

我想要的是具有以下形式的时间序列的PCollection&lt;Timeseries&gt;

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
      }
    ]
  }
]

我希望这是有道理的:)

【问题讨论】:

    标签: java apache-beam windowing


    【解决方案1】:

    您可以通过GroupByKey 将您的输入转换为

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2020-01-18
      • 2013-09-20
      相关资源
      最近更新 更多