代码之家  ›  专栏  ›  技术社区  ›  sasha

Spark中ReduceByKey函数的意外输出

  •  -1
  • sasha  · 技术社区  · 7 年前

    我正在编写代码,需要使用reduceByKey函数聚合密钥。

    //MapTopAir代码

    JavaPairRDD<String,Integer> taxiPair = taxiData.mapToPair(
    
                x->{
    
    
                    if(!x.isEmpty())
                    {
    
                        String [] split = x.split(",");
                        x=split[9]; //Extracting Index Value 9
    
                    }
    
    
    
               return new Tuple2<String,Integer>("Payment:"+x,1);
            }
    
        );
    
        List<Tuple2<String,Integer>> sample = taxiPair.take(10);
    
        for(Tuple2<String,Integer> t: sample)
        {
    
    
            System.out.println(t._1+","+t._2);
    
    
        }
    

    以上代码结果如预期。下面给出的代码段。打印10个值作为示例。

    Payment:1,1
    Payment:2,1
    Payment:1,1
    Payment:1,1
    Payment:1,1
    Payment:1,1
    Payment:1,1
    Payment:1,1
    Payment:1,1
    Payment:1,1
    

    根据上面的理解,我的理解是,一旦reducebykey完成,它应该给出结果:

    Payment:1,9
    Payment:2,1
    

    然而;

    //代码还原键

    JavaPairRDD<String,Integer> taxiReduce = taxiPair.reduceByKey(
    
         (x,y)-> (y+y)
    
    
    
        );
    
    
        List<Tuple2<String,Integer>> sample2 = taxiReduce.collect();
    
        for(Tuple2<String,Integer> t: sample2)
        {
    
    
            System.out.println(t._1+","+t._2);
    
    
        }
    

    //输出:这是完整数据集的集合值,但似乎与预期值不匹配。

    Payment:3,2
    Payment:2,2
    Payment:,2
    Payment:4,2
    Payment:1,2
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   pasha701    7 年前

    语句中的misspint,此处需要“x+y”而不是“y+y”:

      (x,y)-> (y+y)
    
        2
  •  0
  •   APH    7 年前
     JavaPairRDD<String,Integer> taxiReduce = taxiPair.reduceByKey(
    
     (x,y)-> (y+y) );
    

    应该是 (x,y)->(x+y);

    推荐文章