• Win7 Eclipse 搭建spark java1.8(lambda)环境:WordCount helloworld例子


    [学习笔记]

    Win7 Eclipse 搭建spark java1.8(lambda)环境:WordCount helloworld例子

    lambda表达式是java8给我们带来的一个重量的新特性,借用lambda表达式可以让我们的程序设计更加简洁。

    package com;
    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.VoidFunction;
    import scala.Tuple2;
    import java.util.Arrays;
    import java.util.List;
    public class WordCountLambda {
        public static void main(String[] args) {
            SparkConf conf = new SparkConf().setAppName("WordCountLambda马克-to-win @ 马克java社区:").setMaster("local");
            JavaSparkContext sc = new JavaSparkContext(conf);
             JavaRDD<String> lines = sc.textFile("E://temp//input//friend.txt");
             JavaRDD<String> words = lines.flatMap(line -> Arrays.asList(line.split(" ")));
             JavaPairRDD<String, Integer> wordAndOne = words.mapToPair(word -> new Tuple2<>(word, 1));
            JavaPairRDD<String, Integer> results = wordAndOne.reduceByKey((x, y) -> x + y);
    /* 下面一句也能工作。*/       
     //       reduced.saveAsTextFile("E://temp//input//friend1.txt");
    /*word:o1abc count:4
    word:45 count:1
    word:77 count:1*/       
            results.foreach(new VoidFunction<Tuple2<String,Integer>>() {
                public void call(Tuple2<String, Integer> tuple) throws Exception {
                    System.out.println("word:" + tuple._1 + " count:" + tuple._2);
                }
            });
    /*resultsPair is (o1abc,4)
    resultsPair is (45,1)
    resultsPair is (77,1)*/       
            List<Tuple2<String,Integer>> resultsPairs = results.collect();
            for (Tuple2<String, Integer> resultsPair : resultsPairs) {
        

    更多请见:https://blog.csdn.net/qq_44596980/article/details/93385254

  • 相关阅读:
    OFDM系统中频域同步技术及FPGA实现
    SystemVerilog的挑战和机遇
    基础知识要牢固..复习复习,再复习
    样式在.net下测试不成功.附解决办法
    学习.net2.0的网站
    08年又快结束了..抱怨下!~
    今天去面试.net开发,感想
    在b/s开发中经常用到的javaScript技术
    淘宝"新版"首页
    Ajax技术简单入门
  • 原文地址:https://www.cnblogs.com/haima1949/p/11341588.html
Copyright © 2020-2023  润新知