自定义泛型类的Flink类型提取问题

时间:2017-07-28 13:57:04

标签: generics apache-flink type-erasure

(Flink 1.3)

我在类型提取方面遇到问题:

The return type of function '...' could not be determined 
automatically, due to type erasure. You can give type information hints 
by using the returns(...) method on the result of the transformation 
call, or by letting your function implement the 'ResultTypeQueryable' 
interface.

使用DataStream<MyGenericClass<T>>时:

public class MyGenericClass<T> extends Tuple2<String, T> {
    ...
}

如果没有.returns(..)解决方案,我该如何解决问题呢? 您能举例说明如何实现类型信息工厂或如何为ResultTypeQueryable实施MyGenericClass吗?

提前谢谢

1 个答案:

答案 0 :(得分:0)

免责声明:我的回答是基于您使用Java8 lambda表达式的假设

我不太确定,但根据Flink文档 - 这是一个问题,因为“OpenJDK和Oracle JDK的javac等编译器丢弃了与Lambda Expressions相关的所有通用参数。这意味着类型如Tuple2或Collector声明为Lambda函数的输入或输出参数将被修剪为已编译的.class文件中的Tuple2或Collector,这对于Flink编译器而言信息太少。“

根据相同的Fink文档 - “只有Eclipse JDT编译器保留了安全使用整个Lambda表达式功能所必需的泛型类型信息。”

因此,为了解决您的问题,您可以在pom.xml中手动添加以下信息,以便您的maven将使用Eclipse JDT编译器 -

<!-- put these lines under "project/build/pluginManagement/plugins" of your pom.xml -->

<plugin>
  <!-- Use compiler plugin with tycho as the adapter to the JDT compiler. -->
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <compilerId>jdt</compilerId>
  </configuration>
  <dependencies>
    <!-- This dependency provides the implementation of compiler "jdt": -->
    <dependency>
        <groupId>org.eclipse.tycho</groupId>
        <artifactId>tycho-compiler-jdt</artifactId>
        <version>0.21.0</version>
    </dependency>
  </dependencies>
</plugin>

有关详细信息,请参阅此链接 - https://ci.apache.org/projects/flink/flink-docs-release-1.3/dev/java8.html

相关问题