在github上有spark-java8 实例地址:

https://github.com/ypriverol/spark-java8

https://github.com/ihr/java8-spark

学些java8 Lambda Expressions 的可以参考下,同时自己也做下比较。

java8 代码实例

 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package com.east.spark.stream; import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern; import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.sql.SparkSession; import scala.Tuple2; public final class JavaWordCount2 {
private static final Pattern SPACE = Pattern.compile(" "); public static void main(String[] args) throws Exception { args = new String[] { "D:/tmp/spark/test.txt" }; if (args.length < 1) {
System.err.println("Usage: JavaWordCount <file>");
System.exit(1);
} SparkSession spark = SparkSession.builder().appName("JavaWordCount").master("local").getOrCreate(); // SparkConf conf = new
// SparkConf().setAppName("ingini-spark-java8").setMaster("local"); JavaRDD<String> lines = spark.read().textFile(args[0]).javaRDD(); JavaRDD<String> words = lines.flatMap(line -> Arrays.asList(line.split(" ")).iterator()); JavaPairRDD<String, Integer> counts = words.mapToPair(w -> new Tuple2<String, Integer>(w, 1))
.reduceByKey((x, y) -> x + y);
// counts.collect(); List<Tuple2<String, Integer>> output = counts.collect();
for (Tuple2<?, ?> tuple : output) {
System.out.println(tuple._1() + ":== " + tuple._2());
} spark.stop();
}
}

更简洁的写法:

 JavaRDD<String> lines = sc.textFile("src/main/resources/a.txt");
JavaPairRDD<String, Integer> counts = lines.flatMap(line -> Arrays.asList(line.split(REGEX)))
.mapToPair(word -> new Tuple2(word, 1))
.reduceByKey((x, y) -> (Integer) x + (Integer) y)
.sortByKey(); counts.foreach(stringIntegerTuple2 ->System.out.println( stringIntegerTuple2._1+":"+stringIntegerTuple2._2));

最新文章

  1. python中获取指定目录下所有文件名列表的程序
  2. 解决 HttpClient 模拟 http 的get 请求后 ,出现 403 错误
  3. bmob
  4. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分
  5. java控制反转与依赖注入
  6. android:照片涂画功能实现过程及原理
  7. web服务器分析与设计(一)
  8. java实现.net中的枚举
  9. Linux 上的数据可视化工具
  10. HTML5 总结-画布-4
  11. IDEA for Mac注册码使用
  12. Java 在PDF中添加水印——文本/图片水印
  13. PHP异步请求之fsockopen()方法详解
  14. 如何修改DEDECMS文章标题长度
  15. linux-CentOS初学terminal命令(2)vi、gcc、g++、./、mv、cp、ifconfig
  16. linux报错 find: missing argument to `-exec&#39;
  17. selenium+python自动化89-用例不通过的时候发送邮件
  18. supervisor管理ELK进程
  19. JS实现一位数显示为两位
  20. MongoDB 数据迁移 备份 导入(自用)

热门文章

  1. 关于移动端使用swiper做图片文字轮播的思考
  2. C语言itoa()函数和atoi()函数详解(整数转字符C实现)【转载】
  3. NX二次开发-UFUN单按钮模态对话框窗口打印uc1601用法
  4. C++之内存分区
  5. ionic-CSS:ionic Toggle(切换开关)
  6. python中字典排序
  7. 随笔-ansible-2
  8. Pytorch模型定义的三要
  9. 43个实例xHTML+CSS(DIV+CSS)网页及导航布局
  10. 在Linux(centos)下,安装Apache和PHP环境