定义
The $tumblingWindow stage specifies a tumbling window for aggregation of data. Atlas Stream Processing windows are stateful, can be recovered if interrupted, and have mechanisms for processing late-arriving data. You must apply all other aggregation queries to your streaming data within this window stage.
$tumblingWindow$tumblingWindow管道阶段采用以下原型形式:{ "$tumblingWindow": { "boundary": "eventTime" | "processingTime", "interval": { "size": <int>, "unit": "<unit-of-time>" }, "pipeline" : [ <aggregation-stage-array> ], "offset": { "offsetFromUtc": <int>, "unit": "<unit-of-time>" }, "idleTimeout": { "size": <int>, "unit": "<unit-of-time>" }, "allowedLateness": { size: <int>, unit: "<unit-of-time>" } } } 或者,
$tumblingWindow管道阶段可以具有allowedLateness和idleTimeout字段,其整数值为 0,如下所示:{ "$tumblingWindow": { "boundary": "eventTime" | "processingTime", "interval": { "size": <int>, "unit": "<unit-of-time>" }, "pipeline" : [ <aggregation-stage-array> ], "offset": { "offsetFromUtc": <int>, "unit": "<unit-of-time>" }, "idleTimeout": 0, "allowedLateness": 0 } }
语法
$tumblingWindow 阶段采用包含以下字段的文档:
字段 | 类型 | 必要性 | 说明 |
|---|---|---|---|
| 字符串 | Optional | 指定窗口边界是由事件时间还是处理时间决定的字符串。值可以是
|
| 文档 | 必需 | 以大小和时间单位的组合形式指定滚动窗口间隔的文档,其中:
例如, |
| 阵列 | 必需 | 根据窗口内的消息对嵌套聚合管道进行评估。 |
| 文档 | Optional | 指定窗口边界相对于 UTC 的时间偏移的文档。 该文档是大小字段
例如, |
| 文档 | Optional | 该文档指定
如果设置 例如,考虑12 : 00下午 到1 : 00下午 窗口和 Alternatively, you can define this setting with an integer value of 0. See the pipeline defintion for more information. 要了解窗口在窗口过程中空闲超时如何与水印交互,请参阅空闲超时。 |
| 文档 | Optional | 文档,用于指定在处理窗口结束时间的文档后,将从源生成的窗口保持开放状态多长时间以接受延迟到达的数据。如果省略,则默认值为 3 秒。 Alternatively, you can define this setting with an integer value of 0. See the pipeline defintion for more information. 要了解允许的延迟如何延迟相对水印的窗口关闭,请参阅 允许的延迟。 |
行为
Atlas Stream Processing 仅支持每个管道一个窗口阶段。
When you apply the $group stage to your window stage, a single group key has a limit of 100 megabytes of RAM.
在 Windows 中,对某些聚合阶段的支持可能会受到限制或不可用。要了解更多信息,请参阅支持的聚合管道阶段。
如果服务中断,您可以从中断点的状态恢复窗口的内部管道。要了解更多信息,请参阅检查点。
示例
A streaming data source generates detailed weather reports from various locations, conformant to the schema of the Sample Weather Dataset. The following aggregation has three stages:
$source阶段与 Apache Kafka 代理建立连接,在名为my_weatherdata的主题中收集这些报告,从而在将每条记录引入后续聚合阶段时将其公开。The
$tumblingWindowstage defines consecutive windows with 30-second duration. Each window executes an internalpipeline, which finds the average, median, maximum, and minimumatmosphericPressureObservation.altimeterSetting.valuefor the duration of that window. Thepipelinethen outputs a single document with an_idequivalent to the start timestamp of the window it represents and the specified values for that window.The
$mergestage writes the output to an Atlas collection namedstreamin thesample_weatherstreamdatabase. If no such database or collection exist, Atlas creates them.
{ '$source': { connectionName: 'sample_weatherdata', topic: 'my_weatherdata', tsFieldName: 'ingestionTime' } }, { '$tumblingWindow': { interval: { size: 30, unit: "second" }, pipeline: [{ $group: { _id: { $meta: "stream.window.start"}, averagePressure: { $avg: "$atmosphericPressureObservation.altimeterSetting.value" }, medianPressure: { $median: { input: "$atmosphericPressureObservation.altimeterSetting.value", method: "approximate" } }, maxPressure: { $max: "$atmosphericPressureObservation.altimeterSetting.value" }, minPressure: { $min: "$atmosphericPressureObservation.altimeterSetting.value" } } }] } }, { '$merge': { into: { connectionName: 'weatherStreamOutput', db: 'sample_weatherstream', coll: 'stream' } } }
要查看生成的 sample_weatherstream.stream 集合中的文档,请连接到您的 Atlas 集群并运行以下命令:
db.getSiblingDB("sample_weatherstream").stream.find()
{ _id: ISODate('2024-09-26T16:34:00.000Z'), averagePressure: 5271.47894736842, maxPressure: 9999.9, medianPressure: 1015.9, minPressure: 1015.9 }, { _id: ISODate('2024-09-26T16:34:30.000Z'), averagePressure: 5507.9, maxPressure: 9999.9, medianPressure: 1015.9, minPressure: 1015.9 }
注意
以上是一个有代表性的示例。流数据不是静态的,每个用户看到的都是不同的文档。