使用Java的lambda编程flink报错,Caused by: org.apache.flink.api.common.functions.InvalidTypesException:

楼主在使用Java的lambda表达式,进行flink编程时报错,如下:

Caused by: org.apache.flink.api.common.functions.InvalidTypesException: The generic type parameters of 'Collector' are missing

Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: The return type of function

异常信息如下:

 

解决方法:需要加上返回值泛型, 举例如下:

flatMap算子,返回类型String

//######flatMap算子的lambda表达式###########
 //注意这里执行算子操作,有两种接受类型的写法,
 //使用DataSet接受,指定的泛型,是每一有一个元素的泛型,类似于RDD中的没有一个元素
 //使用Operator接受,指定类型有两个,输入参数类型,输出参数类型
DataSet<String> words = dataSource.flatMap((FlatMapFunction<String, String>) (line, out)
// final FlatMapOperator<String, String> words = dataSource.flatMap((FlatMapFunction<String, String>) (line, out)
         -> {
     for (String word : line.split(" ")) {
         if (word.trim() != null)   //过滤无效值
             out.collect(word);
     }
 }).returns(Types.STRING);

flatMap算子,返回类型Tuple,

//######flatMap算子的lambda表达式###########
//返回值是Tuple,注意,泛型的指定,FlatMapFunction也要指定的
DataSet<Tuple2<String,Integer>> wordsandOnes = dataSource.flatMap((FlatMapFunction<String, Tuple2<String,Integer>>) (line, out)
        -> {
    for (String word : line.split(" ")) {
      //  if (word.trim() != null)   //过滤无效值
         Tuple2 t =  new Tuple2(word,1);
            out.collect(t);
    }
}).returns(Types.TUPLE(Types.STRING,Types.INT));

 

Map算子,返回Tuple

//######Map算子的lambda表达式###########
 //使用lambda表达式,注意要知道返回的类型,否则会报错,另外注意返回是Tuple类型时2,是如何指定的。
final MapOperator<String, Tuple2<String,Integer>> map1 = dataSource
        .map((MapFunction<String,Tuple2<String,Integer>>)(in)
        -> new Tuple2<>(in,1))
        .returns(Types.TUPLE(Types.STRING,Types.INT));

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值