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) {
更多请见:
下一篇: Spfa