spark jdk8 单词统计示例
在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 单词统计示例的更多相关文章
- spark复习笔记(3):使用spark实现单词统计
wordcount是spark入门级的demo,不难但是很有趣.接下来我用命令行.scala.Java和python这三种语言来实现单词统计. 一.使用命令行实现单词的统计 1.首先touch一个a. ...
- Storm基础概念与单词统计示例
Storm基本概念 Storm是一个分布式的.可靠地.容错的数据流处理系统.Storm分布式计算结构称为Topology(拓扑)结构,顾名思义,与拓扑图十分类似.该拓扑图主要由数据流Stream.数据 ...
- scala基本语法和单词统计
scala 基本语法 1.声明变量 (1)val i = 1 使用val声明的变量值是不可变的,相当于java里final修饰的变量,推荐使用. (2)var i = "hello" ...
- 2、 Spark Streaming方式从socket中获取数据进行简单单词统计
Spark 1.5.2 Spark Streaming 学习笔记和编程练习 Overview 概述 Spark Streaming is an extension of the core Spark ...
- Spark入门(三)--Spark经典的单词统计
spark经典之单词统计 准备数据 既然要统计单词我们就需要一个包含一定数量的文本,我们这里选择了英文原著<GoneWithTheWind>(<飘>)的文本来做一个数据统计,看 ...
- MapReduce 单词统计案例编程
MapReduce 单词统计案例编程 一.在Linux环境安装Eclipse软件 1. 解压tar包 下载安装包eclipse-jee-kepler-SR1-linux-gtk-x86_64.ta ...
- ytu 2002:C语言实验——单词统计(水题)
C语言实验——单词统计 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 61 Solved: 34[Submit][Status][Web Board] ...
- hive学习之WordCount单词统计
看hive目录下就可以了,程序在hdfs里创建一个hive的大文件夹,相当于数据库吧.上面就是一个完整的利用hive来做单词统计,其中的优劣也能看出一点.
- 运行spark官方的graphx 示例 ComprehensiveExample.scala报错解决
运行spark官方的graphx 示例 ComprehensiveExample.scala报错解决 在Idea中,直接运行ComprehensiveExample.scala,报需要指定master ...
随机推荐
- hibernate(一对多关系)
代码 public class Main { public static void main(String[] args) { SessionFactory sty = HibernateUtil ...
- bzoj1005题解
[解题思路] 引理:Prufer编码 定义:不断删除树中度数为1的最小序号的点,并输出与其相连的节点的序号,直至树中只有两个节点,所得输出序列即为Prufer编码. 性质:任意一棵n节点的树都可以用长 ...
- NX二次开发-UFUN将实体放入STL文件中函数UF_STD_put_solid_in_stl_file
NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...
- DLL劫持技术例子: HijackDll
控制台程序:DllLoader Dll加载器,用于动态加载目标Dll,并动态调用目标函数 #include <cstdio> #include <windows.h> type ...
- Allow Pin Swapping Using these Methods options
Frm:http://techdocs.altium.com/display/ADOH/Pin,+Pair+and+Part+Swapping#Pin,PairandPartSwapping-Swap ...
- webAPI(DOM) 2.1 获取页面元素 | 事件1 | 属性操作 | 节点 | 创建元素 | 事件2
js分三个部分: ECMAScript标准:js的基本语法 DOM:Ducument Object Model--->文档对象模型--->操作页面的元素 BOM:Browser Objec ...
- 2018ICPC焦作 F. Honeycomb /// BFS
题目大意: 给定n m表示一共n行每行m个蜂巢 求从S到T的最短路径 input 1 3 4 +---+ +---+ / \ / \ + +---+ +---+ \ \ / \ + + S +---+ ...
- 我写的界面,在ARM上跑
这个...其实,我对ARM了解并不多,我顶多也就算是知道ARM怎么玩,EMMC干啥,MMU干啥,还有早期的叫法,比如那个NorFlash NandFlash ,然后也就没啥了. 然后写个裸机什么的,那 ...
- SQLAlchemy连接数据库创建表
# 连接数据库,创建表 def create_all(): engine = create_engine( 'mysql+pymysql://root:123456@127.0.0.1:3306/sq ...
- vue axios简单配置
参考:https://www.cnblogs.com/sophie_wang/p/7844119.html 1. 安装 npm install axios 2. main.js import axio ...