在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));

spark jdk8 单词统计示例的更多相关文章

  1. spark复习笔记(3):使用spark实现单词统计

    wordcount是spark入门级的demo,不难但是很有趣.接下来我用命令行.scala.Java和python这三种语言来实现单词统计. 一.使用命令行实现单词的统计 1.首先touch一个a. ...

  2. Storm基础概念与单词统计示例

    Storm基本概念 Storm是一个分布式的.可靠地.容错的数据流处理系统.Storm分布式计算结构称为Topology(拓扑)结构,顾名思义,与拓扑图十分类似.该拓扑图主要由数据流Stream.数据 ...

  3. scala基本语法和单词统计

    scala 基本语法 1.声明变量 (1)val i = 1 使用val声明的变量值是不可变的,相当于java里final修饰的变量,推荐使用. (2)var i = "hello" ...

  4. 2、 Spark Streaming方式从socket中获取数据进行简单单词统计

    Spark 1.5.2 Spark Streaming 学习笔记和编程练习 Overview 概述 Spark Streaming is an extension of the core Spark ...

  5. Spark入门(三)--Spark经典的单词统计

    spark经典之单词统计 准备数据 既然要统计单词我们就需要一个包含一定数量的文本,我们这里选择了英文原著<GoneWithTheWind>(<飘>)的文本来做一个数据统计,看 ...

  6. MapReduce 单词统计案例编程

    MapReduce 单词统计案例编程 一.在Linux环境安装Eclipse软件 1.   解压tar包 下载安装包eclipse-jee-kepler-SR1-linux-gtk-x86_64.ta ...

  7. ytu 2002:C语言实验——单词统计(水题)

    C语言实验——单词统计 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 61  Solved: 34[Submit][Status][Web Board] ...

  8. hive学习之WordCount单词统计

    看hive目录下就可以了,程序在hdfs里创建一个hive的大文件夹,相当于数据库吧.上面就是一个完整的利用hive来做单词统计,其中的优劣也能看出一点.

  9. 运行spark官方的graphx 示例 ComprehensiveExample.scala报错解决

    运行spark官方的graphx 示例 ComprehensiveExample.scala报错解决 在Idea中,直接运行ComprehensiveExample.scala,报需要指定master ...

随机推荐

  1. Python 空值和非空值

    1)任何值为0的值都是false,任何非0的值都是true if -0.0: print 'yes' #不打印yes if -0.1: print 'yes' #打印yes 2)任何为空的值都是fla ...

  2. Electron是个啥?

    于2013年作为构建Github上可编辑的文本编辑器Atom的框架而被开发出来 是目前开源开发者.初创企业和老牌公司常用的开发工具 是桌面应用框架 相当于一个浏览器的外壳,可以把网页程序嵌入到壳里面, ...

  3. NX二次开发-NXOPEN更改工程图视图名字baseView1->SetName("LSY");

    NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...

  4. flutter 超出俩行点点点

    Text( '${listItem["title"]}', overflow: TextOverflow.ellipsis, maxLines: 2, style: TextSty ...

  5. day 87 DjangoRestFramework学习一之restful规范、APIview、解析器组件、Postman等

    DjangoRestFramework学习一之restful规范.APIview.解析器组件.Postman等   本节目录 一 预备知识 二 restful规范 三 DRF的APIView和解析器组 ...

  6. 剑指offer——13矩阵中的路径

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...

  7. 剑指offer——05重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  8. Git仓库操作命令

    创建仓库 git init 在当前目录执行,会生成.git目录文件,这个和SVN一致. 提交到仓库 git commit -m "first commit" -m:表示提交描述,必 ...

  9. 【POJ】1321棋盘问题

    题目链接:http://poj.org/problem?id=1321 题意:见题干,很清楚了. 题解:简单dfs,参照八皇后 代码: #include<iostream> #includ ...

  10. 2019-8-31-dotnet-判断程序当前使用管理员运行降低权使用普通权限运行

    title author date CreateTime categories dotnet 判断程序当前使用管理员运行降低权使用普通权限运行 lindexi 2019-08-31 16:55:58 ...