Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取数据流中以前的窗口数据_Java_Google Cloud Dataflow_Dataflow_Sliding Window - Fatal编程技术网

Java 获取数据流中以前的窗口数据

Java 获取数据流中以前的窗口数据,java,google-cloud-dataflow,dataflow,sliding-window,Java,Google Cloud Dataflow,Dataflow,Sliding Window,试图创建一些警报系统的机制,我正在寻找两个窗口之间的平均值下降 我很高兴能找到这样的例子,尤其是当我看到它说: 如果滑动窗口中的速度占绝大多数,则会发生“减速” 小于上一窗口的读数 我查看了,但无法理解为什么这意味着我们从上一个窗口获取上一个值。因为我到现在还没有滑动车窗的经验,我想我可能错过了一些东西 实现这种机制,无论是否使用滑动窗口,都不会像我怀疑的那样从以前的窗口获取数据 知道我错过了什么吗? 是否有某种方法可以从上一个窗口获取值 我正在使用SDK 1.9.0执行GCP数据流 请告知 舒

试图创建一些警报系统的机制,我正在寻找两个窗口之间的平均值下降

我很高兴能找到这样的例子,尤其是当我看到它说:

如果滑动窗口中的速度占绝大多数,则会发生“减速” 小于上一窗口的读数

我查看了,但无法理解为什么这意味着我们从上一个窗口获取上一个值。因为我到现在还没有滑动车窗的经验,我想我可能错过了一些东西

实现这种机制,无论是否使用滑动窗口,都不会像我怀疑的那样从以前的窗口获取数据

知道我错过了什么吗? 是否有某种方法可以从上一个窗口获取值

我正在使用SDK 1.9.0执行GCP数据流

请告知

舒舒

我的假设:

  • 您的警报系统已将数据划分为由“度量ID”标识的“度量”
  • 给定时间的度量值为
    Double
  • 您以
    PCollection
    的形式接收度量数据,其中
    字符串
    是度量id,
    Double
    是度量值,每个元素都有相应的隐式时间戳(如果没有,可以使用
    with timestamps
    转换来分配一个)
  • 您需要计算从每1分钟开始的每5分钟间隔的每个度量的滑动平均值,并希望在T+1分钟开始的间隔的平均值小于从T开始的间隔的平均值的情况下做一些事情
您可以这样完成:

PCollection<KV<String, Double>> metricValues = ...;
// Collection of (metric, timestamped 5-minute average)
// windowed into the same 5-minute windows as the input,
// where timestamp is assigned as the beginning of the window.
PCollection<KV<String, TimestampedValue<Double>>>
  metricSlidingAverages = metricValues
    .apply(Window.<KV<String, Double>>into(
        SlidingWindows.of(Duration.standardMinutes(5))
                      .every(Duration.standardMinutes(1))))
    .apply(Mean.<String, Double>perKey())
    .apply(ParDo.of(new ReifyWindowFn()));

// Rewindow the previous collection into global window so we can
// do cross-window comparisons.
// For each metric, an unsorted list of (timestamp, average) pairs.
PCollection<KV<String, Iterable<TimestampedValue<Double>>>
  metricAverageSequences = metricSlidingAverages
    .apply(Window.<KV<String, TimestampedValue<Double>>>into(
        new GlobalWindows()))
    // We need to group the data by key again since the grouping key
    // has changed (remember, GBK implicitly groups by key and window)
    .apply(GroupByKey.<String, TimestampedValue<Double>>create())

metricAverageSequences.apply(new DetectAnomaliesFn());

...

class ReifyWindowFn extends DoFn<
    KV<String, Double>, KV<String, TimestampedValue<Double>>> {
  @ProcessElement
  public void process(ProcessContext c, BoundedWindow w) {
    // This DoFn makes the implicit window of the element be explicit
    // and extracts the starting timestamp of the window.
    c.output(KV.of(
      c.element().getKey(),
      TimestampedValue.of(c.element.getValue(), w.minTimestamp())));
  }
}

class DetectAnomaliesFn extends DoFn<
    KV<String, Iterable<TimestampedValue<Double>>>, Void> {
  @ProcessElement
  public void process(ProcessContext c) {
    String metricId = c.element().getKey();
    // Sort the (timestamp, average) pairs by timestamp.
    List<TimestampedValue<Double>> averages = Ordering.natural()
        .onResultOf(TimestampedValue::getTimestamp)
        .sortedCopy(c.element().getValue());
    // Scan for anomalies.
    for (int i = 1; i < averages.size(); ++i) {
      if (averages.get(i).getValue() < averages.get(i-1).getValue()) {
        // Detected anomaly! Could do something with it,
        // e.g. publish to a third-party system or emit into
        // a PCollection.
      }
    }
  }
}
PCollection metricValues=。。。;
//收集(公制,时间戳为5分钟的平均值)
//窗口设置为与输入相同的5分钟窗口,
//其中时间戳被指定为窗口的开头。
P收集
metricSlidingAverages=度量值
应用于(
滑动窗口(持续时间标准分钟(5))
.每(持续时间.标准分钟(1)))
.apply(Mean.perKey())
.应用(新ReifyWindowFn()的第1部分);
//将上一个集合重新导入全局窗口,以便
//进行跨窗口比较。
//对于每个度量,一个未排序的(时间戳,平均值)对列表。
P收集{
@过程元素
公共作废进程(ProcessContext c,BoundedWindow w){
//这个DoFn使得元素的隐式窗口是显式的
//并提取窗口的开始时间戳。
c、 输出(千伏)(
c、 元素().getKey(),
TimestampedValue.of(c.element.getValue(),w.minTimestamp());
}
}
类DetectAnomaliesFn扩展了DoFn<
KV,无效>{
@过程元素
公共作废流程(ProcessContext c){
字符串metricId=c.element().getKey();
//按时间戳对(时间戳,平均值)对进行排序。
列表平均值=排序.natural()
.onResultOf(TimestampedValue::getTimestamp)
.sortedCopy(c.element().getValue());
//扫描异常。
对于(int i=1;i

请注意,我没有测试这段代码,但它应该为您完成这项任务提供足够的概念指导。

Hmm,您文章中的引用与代码中的文本不匹配-它说“如果此滑动窗口中的绝大多数速度低于我们称之为“减速”的前一读数,则没有提及减速”“上一个窗口”。代码通过处理给定路线上所有站点的所有速度测量的完整列表(按时间戳排序),查看特定站点的上一个速度测量值。通常,“上一个窗口”的概念没有明确定义:例如,如果钥匙有3个窗口[1,3]、[2,3]和[1,2],哪一个是“上一个窗口”“对于[2,3]?此外,所有窗口都是同时处理的,不一定按任何特定顺序处理。您可以将窗口简单地视为聚合/分组操作(如GroupByKey和Combine)的另一个隐式分组键。我认为仍然有一种合理的方法可以实现您的原始目标,即使TrafficRoutes不是最好的模仿示例。让我把它写在一个答案里……谢谢。引语来自第一,非常感谢!第二,考虑到在流式数据上运行,这种解决方案不会爆炸得很快吗?我们把所有的数据都推到了全局窗口,不是吗?无论如何,我会玩它,看看它如何适合我的需要。10倍!特定键/窗口的数据不会永远得到缓冲-使用默认的累积模式(丢弃激发的窗格),数据将在DetectAnomaliesFn应用于此特定键/值对时被GC缓存。讨论注释不是一个好方法,因此我将在下面发送更新。