09、高级编程之基于排序机制的wordcount程序

package sparkcore.java;
import java.util.Arrays;
import java.util.Iterator;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;
/**
 * 排序的wordcount程序:根据单词出现的次数进行排序
 */
public class SortWordCount {
    public static void main(String[] args) {
        // 创建SparkConf和JavaSparkContext
        SparkConf conf = new SparkConf().setAppName("SortWordCount").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(conf);
        // 创建lines RDD
        JavaRDD<String> lines = sc.textFile("test.txt");
        // 执行我们之前做过的单词计数
        JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Iterator<String> call(String tthrows Exception {
                return Arrays.asList(t.split(" ")).iterator();
            }
        });
        JavaPairRDD<String, Integer> pairs = words.mapToPair(
                new PairFunction<String, String, Integer>() {
                    private static final long serialVersionUID = 1L;
                    @Override
                    public Tuple2<String, Integer> call(String tthrows Exception {
                        return new Tuple2<String, Integer>(t, 1);
                    }
                });
        JavaPairRDD<String, Integer> wordCounts = pairs.reduceByKey(
                new Function2<Integer, Integer, Integer>() {
                    private static final long serialVersionUID = 1L;
                    @Override
                    public Integer call(Integer v1, Integer v2throws Exception {
                        return v1 + v2;
                    }
                });
        // 到这里为止,就得到了每个单词出现的次数
        // 但是,问题是,我们的新需求,是要按照每个单词出现次数的顺序,降序排序
        // wordCounts RDD内的元素是什么?应该是这种格式的吧:(hello, 3) (you, 2)
        // 我们需要将RDD转换成(3, hello) (2, you)的这种格式,才能根据单词出现次数进行排序把!
        // 进行key-value的反转映射
        JavaPairRDD<Integer, String> countWords = wordCounts.mapToPair(
                new PairFunction<Tuple2<String, Integer>, Integer, String>() {
                    private static final long serialVersionUID = 1L;
                    @Override
                    public Tuple2<Integer, String> call(Tuple2<String, Integer> tthrows Exception {
                        return new Tuple2<Integer, String>(t._2t._1);
                    }
                });
        // 按照key进行排序。注:其实可以使用sortBy()函数来根据自定义排序规则来进行排序,而不用像这里在排序前后进行Key与Value对调
        JavaPairRDD<Integer, String> sortedCountWords = countWords.sortByKey(false);
        // 再次将value-key进行反转映射
        JavaPairRDD<String, Integer> sortedWordCounts = sortedCountWords.mapToPair(
                new PairFunction<Tuple2<Integer, String>, String, Integer>() {
                    private static final long serialVersionUID = 1L;
                    @Override
                    public Tuple2<String, Integer> call(Tuple2<Integer, String> tthrows Exception {
                        return new Tuple2<String, Integer>(t._2t._1);
                    }
                });
        // 到此为止,我们获得了按照单词出现次数排序后的单词计数
        // 打印出来
        sortedWordCounts.foreach(new VoidFunction<Tuple2<String, Integer>>() {
            private static final long serialVersionUID = 1L;
            @Override
            public void call(Tuple2<String, Integer> tthrows Exception {
                System.out.println(t._1 + " : " + t._2);
            }
        });
        // 关闭JavaSparkContext
        sc.close();
    }
}


package sparkcore.scala
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
object SortWordCount {
  def main(args: Array[String]) {
    val conf = new SparkConf()
      .setAppName("SortWordCount")
      .setMaster("local")
    val sc = new SparkContext(conf)
    val lines = sc.textFile("test.txt"1)
    val words = lines.flatMap { line => line.split(" ") }
    val pairs = words.map { word => (word, 1) }
    val wordCounts = pairs.reduceByKey(_ + _)
    val countWords = wordCounts.map(wordCount => (wordCount._2, wordCount._1))
    val sortedCountWords = countWords.sortByKey(false)
    val sortedWordCounts = sortedCountWords.map(sortedCountWord => (sortedCountWord._2, sortedCountWord._1))
    sortedWordCounts.foreach(sortedWordCount => println(sortedWordCount._1 + " : " + sortedWordCount._2))
  }
}
posted @ 2017-07-31 12:46  江正军  阅读(...)  评论(... 编辑 收藏