gpt4 book ai didi

java - 加入Spark RDD后排序乱序

转载 作者:行者123 更新时间:2023-12-02 02:05:59 24 4
gpt4 key购买 nike

我试图从评级数据集中获取观看次数最多的电影,并将电影数据集中相应的电影名称与通用电影 ID 进行映射。当我加入已排序的前 10 名观看次数最多的电影列表时,最终结果未排序。我还尝试了 sortbykey(false) ,但它不起作用。

JavaRDD<String> movies = sc.textFile("in/ml-1m/ratings.dat");
// System.out.println(getmovieidanduserid());
JavaRDD<String> movieid = movies.flatMap(line -> Arrays.asList(line.split("::")[1]).iterator());

JavaPairRDD<String, Integer> moviemost = movieid.mapToPair(id -> new Tuple2<>(id, 1));

JavaPairRDD<String, Integer> moviemostlist = moviemost.reduceByKey((x, y) -> x + y);

JavaPairRDD<Integer, String> countToWordParis = moviemostlist.mapToPair(wordToCount -> new Tuple2<>(wordToCount._2(),
wordToCount._1()));
JavaPairRDD<Integer, String> sortedCountToWordParis = countToWordParis.sortByKey(false);

JavaPairRDD<String, Integer> sortedWordToCountPairs = sortedCountToWordParis
.mapToPair(countToWord -> new Tuple2<>(countToWord._2(), countToWord._1()));

JavaPairRDD<String, Integer> mostwatched = sc.parallelizePairs(sortedWordToCountPairs.take(10));

System.out.println(sortedWordToCountPairs.take(10));

for( Tuple2<String, Integer> mlost : mostwatched.collect())
{
System.out.println(mlost._1() + " : " + mlost._2());
}

JavaRDD<String> moviesname = sc.textFile("in/ml-1m/movies.dat");

JavaPairRDD<String, String> moviesiduser=moviesname.mapToPair(getPairFunction());
JavaPairRDD<String,Tuple2<Integer,String>> joindata=mostwatched.join(moviesiduser);
System.out.println("-------top movies----------");
System.out.println(joindata.take(10));



for (Tuple2<String, Tuple2<Integer, String>> wordToCount : joindata.collect()) {
System.out.println(wordToCount._1() + " : " + wordToCount._2());
}
}


private static PairFunction<String, String, String> getmovieidanduserid() {
return (PairFunction<String, String, String>) line -> new Tuple2<>(line.split("::")[1],
line.split("::")[0]);
}

private static PairFunction<String, String, String> getPairFunction() {
return (PairFunction<String, String, String>) line -> new Tuple2<>(line.split("::")[0],
line.split("::")[1]);
}

排名前 10 的电影 ID 和观看次数

2858 : 3428
260 : 2991
1196 : 2990
1210 : 2883
480 : 2672
2028 : 2653
589 : 2649
2571 : 2590
1270 : 2583
593 : 2578

与电影名称映射后

593 : (2578,Silence of the Lambs, The (1991))
589 : (2649,Terminator 2: Judgment Day (1991))
480 : (2672,Jurassic Park (1993))
2858 : (3428,American Beauty (1999))
260 : (2991,Star Wars: Episode IV - A New Hope (1977))
2571 : (2590,Matrix, The (1999))
2028 : (2653,Saving Private Ryan (1998))
1270 : (2583,Back to the Future (1985))
1210 : (2883,Star Wars: Episode VI - Return of the Jedi (1983))
1196 : (2990,Star Wars: Episode V - The Empire Strikes Back (1980))

《美国丽人》收视率最高,但排在第四位

最佳答案

在spark(实际上几乎所有类SQL查询引擎)中,join操作并不能保证排序的维持。加入后需要排序。

关于java - 加入Spark RDD后排序乱序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50798041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com