简体   繁体   English

如何在scala中使用flink fold函数

[英]How to use flink fold function in scala

This is a non working try for using Flink fold with scala anonymous function: 这是一个非常有效的尝试使用Flink折叠与scala匿名函数:

val myFoldFunction = (x: Double, t:(Double,String,String)) => x + t._1
env.readFileStream(...).
...
.groupBy(1)
.fold(0.0, myFoldFunction : Function2[Double, (Double,String,String), Double])

It compiles well, but at execution, I get a "type erasure issue" (see below). 它汇编得很好,但在执行时,我得到了“类型擦除问题”(见下文)。 Doing so in Java is fine, but of course more verbose. 在Java中这样做很好,但当然更冗长。 I like the concise and clear lambdas. 我喜欢简洁明了的lambda。 How can I do that in scala? 我怎么能在scala中做到这一点?

Caused by: org.apache.flink.api.common.functions.InvalidTypesException:
Type of TypeVariable 'R' in 'public org.apache.flink.streaming.api.scala.DataStream org.apache.flink.streaming.api.scala.DataStream.fold(java.lang.Object,scala.Function2,org.apache.flink.api.common.typeinfo.TypeInformation,scala.reflect.ClassTag)' could not be determined. 
This is most likely a type erasure problem. 
The type extraction currently supports types with generic variables only in cases where all variables in the return type can be deduced from the input type(s).

The problem you encountered is a bug in Flink [1]. 您遇到的问题是Flink [1]中的错误。 The problem originates from Flink's TypeExtractor and the way the Scala DataStream API is implemented on top of the Java implementation. 问题源于Flink的TypeExtractor以及Scala DataStream API在Java实现之上的实现方式。 The TypeExtractor cannot generate a TypeInformation for the Scala type and thus returns a MissingTypeInformation . TypeExtractor无法为Scala类型生成TypeInformation ,因此返回MissingTypeInformation This missing type information is manually set after creating the StreamFold operator. 创建StreamFold运算符后,手动设置此缺失类型信息。 However, the StreamFold operator is implemented in a way that it does not accept a MissingTypeInformation and, consequently, fails before setting the right type information. 但是, StreamFold运算符的实现方式是它不接受MissingTypeInformation ,因此在设置正确的类型信息之前失败。

I've opened a pull request [2] to fix this problem. 我已经打开了一个拉取请求[2]来解决这个问题。 It should be merged within the next two days. 它应该在接下来的两天内合并。 By using then the latest 0.10 snapshot version, your problem should be fixed. 通过使用最新的0.10快照版本,您的问题应该得到解决。

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

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