一、最值

计算文本里面的最值(最大值、最小值、平均值),输出结果。

二、maven设置

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mk</groupId><artifactId>spark-test</artifactId><version>1.0</version><name>spark-test</name><url>http://spark.mk.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><scala.version>2.11.1</scala.version><spark.version>2.4.4</spark.version><hadoop.version>2.6.0</hadoop.version></properties><dependencies><!-- scala依赖--><dependency><groupId>org.scala-lang</groupId><artifactId>scala-library</artifactId><version>${scala.version}</version></dependency><!-- spark依赖--><dependency><groupId>org.apache.spark</groupId><artifactId>spark-core_2.11</artifactId><version>${spark.version}</version></dependency><dependency><groupId>org.apache.spark</groupId><artifactId>spark-sql_2.11</artifactId><version>${spark.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies><build><pluginManagement><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version></plugin></plugins></pluginManagement></build>
</project>

三、编程代码

public class MaxApp implements SparkConfInfo{public static class IntegerComp implements Comparator<Integer>, Serializable{@Overridepublic int compare(Integer o1, Integer o2) {return o1.compareTo(o2);}}public static void main(String[]args){String filePath = "E:\\spark\\number.txt";SparkSession sparkSession = new MaxApp().getSparkConf("MaxApp");JavaRDD<Integer> numbers = sparkSession.sparkContext().textFile(filePath, 4).toJavaRDD().flatMap(v -> Arrays.asList(v.split("\n")).iterator()).map(Integer::new).cache();Integer max = numbers.max(new IntegerComp());Integer min = numbers.min(new IntegerComp());Integer sum = numbers.reduce(Integer::sum);long count = numbers.count();System.out.println("max:" + max);System.out.println("min:" + min);System.out.println("sum:" + sum);System.out.println("count:" + count);System.out.println("avg:" + sum * 1.0 / count);sparkSession.stop();}
}public interface SparkConfInfo {default SparkSession getSparkConf(String appName){SparkConf sparkConf = new SparkConf();if(System.getProperty("os.name").toLowerCase().contains("win")) {sparkConf.setMaster("local[4]");System.out.println("使用本地模拟是spark");}else{sparkConf.setMaster("spark://hadoop01:7077,hadoop02:7077,hadoop03:7077");sparkConf.set("spark.driver.host","192.168.150.1");//本地ip,必须与spark集群能够相互访问,如:同一个局域网sparkConf.setJars(new String[] {".\\out\\artifacts\\spark_test\\spark-test.jar"});//项目构建生成的路径}SparkSession session = SparkSession.builder().appName(appName).config(sparkConf).config(sparkConf).getOrCreate();return session;}
}

number.txt文件内容

100
24
43
774
43
37
78
42
68
89
49
543
36
888
258
538
79
6
67
99

输出

max:888
min:6
sum:3861
count:20
avg:193.05

遇到的问题

使用函数接口报错

 Integer max = numbers.max(Integer::compareTo);
org.apache.spark.SparkException: Task not serializableCaused by: java.io.NotSerializableException: com.mk.MaxApp$$Lambda$11/501991708
Serialization stack:- object not serializable (class: com.mk.MaxApp$$Lambda$11/501991708, value: com.mk.MaxApp$$Lambda$11/501991708@7fd26ad8)- field (class: scala.math.LowPriorityOrderingImplicits$$anon$7, name: cmp$2, type: interface java.util.Comparator)- object (class scala.math.LowPriorityOrderingImplicits$$anon$7, scala.math.LowPriorityOrderingImplicits$$anon$7@63b3ee82)- field (class: org.apache.spark.rdd.RDD$$anonfun$max$1, name: ord$10, type: interface scala.math.Ordering)- object (class org.apache.spark.rdd.RDD$$anonfun$max$1, <function0>)- field (class: org.apache.spark.rdd.RDD$$anonfun$max$1$$anonfun$apply$50, name: $outer, type: class org.apache.spark.rdd.RDD$$anonfun$max$1)- object (class org.apache.spark.rdd.RDD$$anonfun$max$1$$anonfun$apply$50, <function2>)

原因是函数接口对象实现没有序列化接口,需要实现序列化接口Serializable

Integer max = numbers.max(new IntegerComp());public static class IntegerComp implements Comparator<Integer>, Serializable{@Overridepublic int compare(Integer o1, Integer o2) {return o1.compareTo(o2);}}

Spark入门(十二)之最值相关推荐

  1. IM开发者的零基础通信技术入门(十二):上网卡顿?网络掉线?一文即懂!

    [来源申明]本文引用了微信公众号"鲜枣课堂"的<上网慢?经常掉线?这篇文章告诉你该怎么办!>文章内容.为了更好的内容呈现,即时通讯网在引用和收录时内容有改动,转载时请注 ...

  2. 网络编程懒人入门(十二):快速读懂Http/3协议,一篇就够!

    本文中文译文由作者"ably.io"发布于公众号"高可用架构",译文原题:<深入解读HTTP3的原理及应用>.英文原题:<HTTP/3 dee ...

  3. Android入门(十二)SQLite事务、升级数据库

    原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...

  4. 2021年大数据Spark(十二):Spark Core的RDD详解

    目录 RDD详解 为什么需要RDD? 什么是RDD? RDD的5大特性 第一个:A list of partitions 第二个:A function for computing each split ...

  5. Spark(十二) -- Spark On Yarn Spark as a Service Spark On Tachyon

    Spark On Yarn: 从0.6.0版本其,就可以在在Yarn上运行Spark 通过Yarn进行统一的资源管理和调度 进而可以实现不止Spark,多种处理框架并存工作的场景 部署Spark On ...

  6. Spark入门(二)多主standalone安装

    一.集群安装条件前置 实验spark安装在[Hadoop生态Zookeeper安装]机器上, 已完成安装zookeeper.jdk.hadoop和ssh.网络等配置环境等. spark所依赖的虚拟机和 ...

  7. 【FPGA入门十二】1bit全加器实现计算8位二进制数中1的个数

    文章目录 一.实验任务 二.设计思路 三.代码实现 ①设计按键消抖模块 ②设计按键输入8bit二进制数 ③设计计算8bit二进制数中1的个数模块 ④设计数码管显示模块 ⑤顶层模块 ⑥设计仿真文件 ⑦仿 ...

  8. [WebGL入门]十二,模型数据和顶点属性

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外.鄙人webgl研究还不够深入,一些专业词语.假设翻译有误.欢迎大家指 ...

  9. 序列(sequence)(Python入门十二)

    序列(sequence) - 序列是Python中最基本的一种数据结构     - 数据结构指计算机中数据存储的方式     - 序列用于保存一组有序的数据,所有的数据在序列当中都有一个唯一的位置(索 ...

  10. Spark机器学习实战 (十二) - 推荐系统实战

    0 相关源码 将结合前述知识进行综合实战,以达到所学即所用.在推荐系统项目中,讲解了推荐系统基本原理以及实现推荐系统的架构思路,有其他相关研发经验基础的同学可以结合以往的经验,实现自己的推荐系统. 1 ...

最新文章

  1. 关于jquery动态改变css样式后,对象获取不到的解决办法
  2. C/C++ 类库开发库参考【资料整理】
  3. MySql修改数据库编码为UTF8
  4. css --- [读书笔记] 盒模型(边框、内外边距)
  5. 数据库、表、表内容增删改查
  6. 我的JavaWeb学习1
  7. 吉大20春学期计算机系统结构在线作业一,吉大20春学期《计算机原理及系统结构》在线作业一【奥鹏百分答案】...
  8. 剑指offer面试题24. 反转链表(双指针)
  9. android jni示例_Android TextInputLayout示例
  10. iOS 逆向 越狱 砸壳 获取
  11. 好用的BUG、内存泄露捕捉工具 EurekaLog v6.0.3 Enterprise For D5-D2007
  12. Android 修改保持WLAN热点开启的时间与最大连接数
  13. 扎实的PHP编程基础,PHP的一些基础编程题
  14. 如何使用Jekyll搭建个人博客
  15. 哈工大李治军老师操作系统笔记【10】:内核级线程实现(Learning OS Concepts By Coding Them !)
  16. [笨木头FireFly01]入门篇1·最简单的服务端和客户端连接
  17. python一维表二维表转化
  18. 常见的js加密/js解密方法
  19. 高项 案例分析重点知识-多读(一)
  20. uml活动图 各个功能的操作流程和分支_软件工程专题:UML活动图

热门文章

  1. 面试必问系列之在浏览器中输入URL后到网页显示 其间发生了什么?
  2. [C++11]可调用对象包装器function
  3. [蓝桥杯][算法提高VIP]夺宝奇兵-递推+记忆化搜索
  4. K-periodic Garland CodeForces - 1353E(暴力+贪心+dp)
  5. linux下I2C驱动发送IO时序,I2C驱动情景分析——怎样控制I2C时序
  6. HDU6956-Pass!(2021杭电多校一)(BSGS)
  7. E - Rotate and Flip(转化一般性)
  8. #3027. [Ceoi2004]Sweet 生成函数 + 组合数学
  9. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) F. Bits And Pieces sosdp预处理超集
  10. 【WC2014】时空穿梭【组合数】【莫比乌斯反演】【整除分块】【暴力多项式】