且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Spark写一个简单的wordcount词频统计程序

更新时间:2022-09-18 20:26:52

public class WordCountLocal {

 public static void main(String[] args) {

  SparkConf conf = new SparkConf().setAppName("WordCountLocal").setMaster("local[2]");

  

  JavaSparkContext sc = new JavaSparkContext(conf);

  JavaRDD<String> words = sc.textFile("c:.//words.txt").flatMap(new FlatMapFunction<String, String>() {

   @Override

   public Iterable<String> call(String line) throws Exception {

    return Arrays.asList(line.split(" "));

   }

  });  

  JavaPairRDD<String, Integer> mapToPair = words.mapToPair(new PairFunction<String, String, Integer>() {

   @Override

   public Tuple2<String, Integer> call(String word) throws Exception {    

    return new Tuple2<String, Integer>(word,1);   

   }

  });  

  JavaPairRDD<String, Integer> result = mapToPair.reduceByKey(new Function2<Integer, Integer, Integer>() {

   

   @Override

   public Integer call(Integer v1, Integer v2) throws Exception {   

    return v1 + v2;

   }

  });

 

  result.foreach(new VoidFunction<Tuple2<String,Integer>>() {

   

   @Override

   public void call(Tuple2<String, Integer> wordCount) throws Exception {

   

    System.out.println(wordCount._1 + " appear " + wordCount._2 + " times!");

   }

  });

  

  sc.close();

 }

 

}

本文转自   ChinaUnicom110   51CTO博客,原文链接:http://blog.51cto.com/xingyue2011/1933462