繁体   English   中英

在flink中使用折叠功能时出错

[英]error when using fold function in flink

代码如下:

env
  .addSource(...)
  .map(r => (0, r))
  .keyBy(0)
  .timeWindow(Time.seconds(30), Time.seconds(1))
  .fold(mutable.HashSet[String](),(a:(Int,String),b:mutable.HashSet[String])=>a)

编译期间发生错误,错误消息是:

错误:在WindowedStream类中缺少方法折叠的参数; 如果你想把它作为一个部分应用的函数timeWindow(Time.seconds(30),Time.seconds(1))来处理这个方法,请使用`_'.fold(mutable.HashSetString,

但是WindowedStream类中定义的函数是:

公共折叠(R initialValue,FoldFunction函数)

问题是双重的:首先,如果你使用的是Scala,则fold函数需要在第二个参数列表中传递FoldFunction 其次, FoldFunction的第一个参数应该是聚合类型。 因此,在您的情况下,它应该是mutable.HashSet[String]类型。 以下代码片段可以解决这个问题:

env
  .addSource(...)
  .map(r => (0, r))
  .keyBy(0)
  .timeWindow(Time.seconds(30), Time.seconds(1))
  .fold(mutable.HashSet[String]()){
    (a: mutable HashSet[String], b: (Int, String)) => a
  }

请注意,不推荐使用Flink的fold API调用。 现在建议使用aggregate API调用。

暂无
暂无

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

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