简体   繁体   English

Mergesort使用apache-spark

[英]Mergesort using apache-spark

I am new to Apache Spark, I have to write java code for mergesort in apache spark. 我是Apache Spark的新手,我必须在apache spark中为mergesort编写java代码。

SparkConf conf = new SparkConf().setMaster("local").setAppName("Merge Sort App");
JavaSparkContext sc = new JavaSparkContext(conf);

JavaRDD<Integer> input = sc.parallelize(a); // where a is list of Integers  

I am done with loading array-list of Integers in javaRDD<Integer> : 我完成了在javaRDD<Integer>加载整数的数组列表:

I have to use integer element list to sort using megersort with apache spark. 我必须使用整数元素列表来使用带有apache spark的megersort进行排序。

Any help? 有帮助吗?

You could define a function merge that takes in two lists of type Integer and outputs a single sorted list of Integer 's. 您可以定义一个函数merge ,它接收两个Integer类型的列表,并输出一个Integer的排序列表。 Then just call input.reduce(<classname>::merge) . 然后只需调用input.reduce(<classname>::merge)

Of course, this depends on you having your RDD hold lists of Integer s instead of individual elements. 当然,这取决于你的RDD保持Integer列表而不是单个元素。 Think the first part of merge sort where you recurse to the single elements. 考虑合并排序的第一部分,您可以将其递归到单个元素。 You could do this by writing a method toList that takes an Integer and outputs a List with that element in it. 您可以通过编写一个方法toList来执行此操作,该方法采用Integer并输出包含该元素的List Then you can just do input.map(<classname>::toList).reduce(<classname>::merge) . 然后你可以只做input.map(<classname>::toList).reduce(<classname>::merge)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM