Cassandra Spark连接器JavaDemo编译错误

nvbavucw  于 2022-11-05  发布在  Cassandra
关注(0)|答案(1)|浏览(162)

我使用的是Cassandra 2.2.8,JDK 8,spark-cassandra-connector-java_2.10,spark-cassandra-connector_2.11-2.0.0-M3,cassandra-driver-core-3.1.0和以下的Cassandra Spark Connector Example JavaDemo。这个演示必须修复,才能用新的2.1 Connetcor API编译。我已经修复了一些东西,但下面的这个让我很为难:在此行发生编译错误:

JavaPairRDD<Integer, BigDecimal> allSalesRDD = joinedRDD.flatMap(new PairFlatMapFunction<Tuple2<Integer, Tuple2<Sale, Product>>, Integer, BigDecimal>() {
        @Override
        public Iterable<Tuple2<Integer, BigDecimal>> call(Tuple2<Integer, Tuple2<Sale, Product>> input) throws Exception {

错误:

The method 
    flatMap(FlatMapFunction<Tuple2<Integer,Tuple2<SparkJavaDemo.Sale,SparkJavaDemo.Product>>,U>) in the type 
         AbstractJavaRDDLike<Tuple2<Integer,Tuple2<SparkJavaDemo.Sale,SparkJavaDemo.Product>>,JavaPairRDD<Integer,Tuple2<SparkJavaDemo.
         Sale,SparkJavaDemo.Product>>> is not applicable for the arguments (new 
         PairFlatMapFunction<Tuple2<Integer,Tuple2<SparkJavaDemo.Sale,SparkJavaDemo.Product>>,Integer,BigDecimal>(){})

谢谢

uttx8gqw

uttx8gqw1#

您可以使用flatMapToPair代替flatMap,如下所示。

JavaPairRDD<Integer, BigDecimal> allSalesRDD = joinedRDD.flatMapToPair(new PairFlatMapFunction<Tuple2<Integer, Tuple2<Sale, Product>>, Integer, BigDecimal>() {
        @Override
        public Iterator<Tuple2<Integer, BigDecimal>> call(Tuple2<Integer, Tuple2<Sale, Product>> input) throws Exception {
            Tuple2<Sale, Product> saleWithProduct = input._2();
            List<Tuple2<Integer, BigDecimal>> allSales = new ArrayList<>(saleWithProduct._2().getParents().size() + 1);
            allSales.add(new Tuple2<>(saleWithProduct._1().getProduct(), saleWithProduct._1().getPrice()));
            for (Integer parentProduct : saleWithProduct._2().getParents()) {
                allSales.add(new Tuple2<>(parentProduct, saleWithProduct._1().getPrice()));
            }
            return allSales.iterator();
        }
    });

我已经在https://gist.github.com/baghelamit/f2963d9e37acc55474559104f5f16cf1上创建了更新代码的要点

相关问题