繁体   English   中英

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

[英]Apache Beam create timeseries from event stream

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

事件被编码为

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

我希望这是有道理的 :)

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

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

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

您还可以查看内置窗口功能,看看它是否能满足您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM