当前位置: 代码迷 >> 综合 >> xxx in ‘Anonymous class derived from xxx clashes with ‘call(T)‘ in xxx attempting to use incompati
  详细解决方案

xxx in ‘Anonymous class derived from xxx clashes with ‘call(T)‘ in xxx attempting to use incompati

热度:66   发布时间:2024-02-06 08:39:16.0

完整报错如下:

'call(Tuple2<Long, Row>)' in 'Anonymous class derived from org.apache.spark.api.java.function.PairFlatMapFunction'

clashes with 'call(T)' in 'org.apache.spark.api.java.function.PairFlatMapFunction';

attempting to use incompatible return type

 

点击PairFlatMapFunction源码:

@FunctionalInterface
public interface PairFlatMapFunction<T, K, V> extends Serializable {Iterator<Tuple2<K, V>> call(T t) throws Exception;
}

发现要求返回iterator

 

原因:

两处不一致:

①return处与public后面的类型不一致。

②Tuple2<String,Row>应该改成Tuple2<String,Row>

 

解决方案如下:

    private static PairFlatMapFunction<Tuple2<Long,Row>, String, Row>func7= new PairFlatMapFunction<Tuple2<Long,Row>, String, Row>() {private static final long serialVersionUID = 1L;public Iterable<Tuple2<String, Row>> call(Tuple2<Long, Row> tuple) throws Exception 
{Random random = new Random();List<Tuple2<String, Row>> list = new ArrayList<Tuple2<String, Row>>();for(int i = 0; i < 100; i++) {list.add(new Tuple2<String, Row>(i + "_" + tuple._1, tuple._2));}return list;}
};

改成:

private static  PairFlatMapFunction<Tuple2<String,Row>, String, Row>  func7 = new PairFlatMapFunction<Tuple2<String,Row>, String, Row>(){private static final long serialVersionUID = 1L;
//    @Overridepublic Iterator<Tuple2<String, Row>> call(Tuple2<String, Row> tuple) throws Exception
{Random random = new Random();List<Tuple2<String, Row>> list = new ArrayList<Tuple2<String, Row>>();for(int i = 0; i < 100; i++){list.add(new Tuple2<String, Row>(i + "_" + tuple._1, tuple._2));}return list.iterator();}};

 

 

  相关解决方案