利用Java的Spark做单词统计并排序

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Comparator;

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 scala.Tuple2;

public class Demo {

    public static void main(String[] args) {
        String filename = "/home/iiip/PycharmProjects/InterestModel/data/sample/机器学习/D0002033.txt";
        Demo demo = new Demo();
        demo.CountWord(filename);
    }

    public List<Map.Entry<String, Integer>> CountWord(String filename) {
        SparkConf conf = new SparkConf();
        conf.set("Spark.testing.memory", "2147480000");
        JavaSparkContext sc = new JavaSparkContext("local[*]", "Spark", conf);
        JavaRDD<String> lines = sc.textFile(filename);   // 读取文件的每一行
        JavaRDD<String> words = lines.flatMap(new FlatWord());    // 从每一个行中读取单个的词
        JavaPairRDD<String, Integer> wordsPair = words.mapToPair(new MapOne()); // 将词语做成<key,Value>键值对
        JavaPairRDD<String , Integer> wordWithNum = wordsPair.reduceByKey(new WordCount());  // 对<key,Value>做一个Reduce操作
        List<Tuple2<String, Integer>>  result = wordWithNum.collect(); // 获取结果
        Map<String, Integer> map = new HashMap<String , Integer> ();
        for(Tuple2<String , Integer> tuple : result){
            map.put(tuple._1(), tuple._2());
        }
        //根据结果的键值对, 按值排序
        List<Map.Entry<String, Integer>> sortedWord = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        Collections.sort(sortedWord, new Comparator<Map.Entry<String, Integer>>(){
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2){
                return (o2.getValue() - o1.getValue());
            }
        });
        for(Map.Entry<String, Integer> word : sortedWord){
            System.out.println(word.getKey() + "   " + word.getValue());
        }
        return sortedWord;
    }

    static class FlatWord implements FlatMapFunction<String, String>{
         // 将每个单词映射成为单个元素
        @Override
        public Iterable<String> call(String sentence) throws Exception {
            String[] words = sentence.split(" ");
            return Arrays.asList(words);
        }
    }

    static class MapOne implements PairFunction<String, String, Integer>{

        @Override
        public Tuple2<String, Integer> call(String word){
            return new Tuple2<String, Integer>(word, 1);
        }
    }

    static class WordCount implements Function2<Integer, Integer , Integer>{

        @Override
        public Integer call(Integer i1, Integer i2){
            return i1 + i2;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_30843221/article/details/75635210