美文网首页
flink 遇到 Tuple2 泛型导致的问题:could no

flink 遇到 Tuple2 泛型导致的问题:could no

作者: 飞不高的老鸟 | 来源:发表于2019-11-08 20:22 被阅读0次

问题描述

Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: 
The return type of function 'main(WatermarkTest.java:30)' 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.
  • 错误原因:这里错误发生的地方使用了一个 tuple 元组,且没有指定泛型。

错误代码

SingleOutputStreamOperator<Tuple2> map = dataStream.map(new MapFunction<String, Tuple2>() {
            @Override
            public Tuple2 map(String value) throws Exception {

                String[] s = value.split(" ");

                return new Tuple2(s[0], s[1]);
            }
        });

解决方案

  • 在 tuple 元组使用时指定泛型。

正确代码

SingleOutputStreamOperator<Tuple2<String, String>> map = dataStream.map(new MapFunction<String, Tuple2<String, String>>() {
            @Override
            public Tuple2 map(String value) throws Exception {

                String[] s = value.split(" ");

                return new Tuple2(s[0], s[1]);
            }
        });

相关文章

网友评论

      本文标题:flink 遇到 Tuple2 泛型导致的问题:could no

      本文链接:https://www.haomeiwen.com/subject/lkwwbctx.html