Spark入门(十一)之排序

 一、Sort

计算文本里面的每个单词出现的个数,单词个数逆序(相同个数单词正序)输出结果。

 

二、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 SortApp implements SparkConfInfo{public static class SortData implements Comparable<SortData>, Serializable {private String word;private Integer count;public SortData(String word, Integer count) {this.word = word;this.count = count;}public String getWord() {return word;}public void setWord(String word) {this.word = word;}public Integer getCount() {return count;}public void setCount(Integer count) {this.count = count;}@Overridepublic int compareTo(SortData o) {if (o == null) {return 1;}int diff = o.count - this.count;if (diff != 0)return diff;if(word == o.word)return 0 ;if(word == null)return -1;if(o.word == null)return 1;return  this.word.compareTo(o.word);}}public static void main(String[]args){String filePath = "F:\\test\\log.txt";SparkSession sparkSession = new SortApp().getSparkConf("sort");List<String> wordCounts = sparkSession.sparkContext().textFile(filePath, 4).toJavaRDD().flatMap(v -> Arrays.asList(v.split("[(\\s+)(\r?\n),.。'’]")).iterator()).filter(v -> v.matches("[a-zA-Z-]+")).map(String::toLowerCase).mapToPair(v -> new Tuple2<>(v, 1)).reduceByKey(Integer::sum).map(v->new SortData(v._1, v._2)).sortBy(v -> v, true,4).map(v->v.word).collect();wordCounts.forEach(v -> System.out.println(v));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;}
}

文件内容

Spark Streaming is an extension of the core Spark API that enables scalable,high-throughput, fault-tolerant stream processing of live 。data streams. Data, can be ,ingested from many sources like Kafka, Flume, Kinesis, or TCP sockets, and can be processed using complex algorithms expressed with high-level functions like map, reduce, join and window. Finally, processed data can be pushed out to filesystems,Spark Streaming provides a high-level abstraction called discretized stream or DStream, which represents a continuous stream of data. DStreams can be created either from input data streams from sources such as Kafka, Flume, and Kinesis, or by applying high-level operations on other DStreams. Internally, a DStream is represented as a sequence of RDDs.This guide shows you how to start writing Spark Streaming programs with DStreams. You can write Spark Streaming programs in Scala, Java or Python (introduced in Spark 1.2), all of which are presented in this guide. You will find tabs throughout this guide that let you choose between code snippets of different languages. databases, and live dashboards. In fact, you can apply Spark’s machine learning and graph processing algorithms on data streams.

输出

spark
can
of
and
data
you
a
be
in
or
streaming
dstreams
from
guide
high-level
stream
streams
this
algorithms
as
dstream
flume
is
kafka
kinesis
like
live
on
processed
processing
programs
sources
that
to
which
with
abstraction
all
an
api
apply
applying
are
between
by
called
choose
code
complex
continuous
core
created
dashboards
databases
different
discretized
either
enables
expressed
extension
fact
fault-tolerant
filesystems
finally
find
functions
graph
high-throughput
how
input
internally
introduced
java
join
languages
learning
let
machine
many
map
operations
other
out
presented
provides
pushed
python
rdds
reduce
represented
represents
s
scala
scalable
sequence
shows
snippets
sockets
start
such
tabs
tcp
the
throughout
using
will
window
write
writing

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322457.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

(四)十大经典排序算法(动画图解,代码完全)

排序算法是《数据结构与算法》中最基本的算法之一 1. 冒泡排序 1.1 算法步骤 比较相邻的元素。如果第一个比第二个大&#xff0c;就交换他们两个。 对每一对相邻元素作同样的工作&#xff0c;从开始第一对到结尾的最后一对。这步做完后&#xff0c;最后的元素会是最大的数。…

Spark入门(十二)之最值

一、最值 计算文本里面的最值&#xff08;最大值、最小值、平均值&#xff09;&#xff0c;输出结果。 二、maven设置 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"ht…

jzoj1764-游戏【dp,dfs】

正题 题目大意 一个n∗nn*nn∗n的矩阵中fi,jfi−1,jfi−1,j1f_{i,j}f_{i-1,j}f_{i-1,j1}fi,j​fi−1,j​fi−1,j1​。 但是有格子恒定为0 给出fn,1f_{n,1}fn,1​要求在第一列的数字不超过MaxMaxMax的情况下字典序最小。 求这个字典序。 解题思路 若不考虑坏格子&#xff0c;…

(五)SpringBoot 能挣钱的几个项目!!!

不得不佩服 Spring Boot 的生态如此强大&#xff0c;今天给大家推荐几款 Gitee 上优秀的后台开源版本的管理系统&#xff0c;小伙伴们再也不用从头到尾撸一个项目了&#xff0c;简直就是接私活&#xff0c;挣钱的利器啊。SmartAdmin我们开源一套漂亮的代码和一套整洁的代码规范…

Spark入门(十三)之分组求平均值

一、分组求平均值 计算文本里面的每个key分组求平均值&#xff0c;输出结果。 二、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/XM…

手把手引进门之 ASP.NET Core Entity Framework Core(官方教程翻译版 版本3.2.5)

以下是手把手引进门教程&#xff0c;基于 ASP.NET Core&#xff0c; Entity Framework Core &#xff0c;ABP 框架 创建Web 应用&#xff0c; PS&#xff1a; 自带自动的测试模块哦。样例下载 &#xff08;上 github 的请自便&#xff09;介绍这是系列文章的第一部分&#xff1…

jzoj1758-过河【dp】

正题 题目大意 nnn个木板第iii个浮起来aisa_i\ sai​ s后沉bisb_i sbi​s如此反复。 每sss最多可以跨555格&#xff0c;最短时间到达右边。 解题思路 设fi,jf_{i,j}fi,j​表示在第isi\ si s的时候是否可以到达第jjj格木板 然后显而易见fi,jfi,k(∣k−j∣≤5)f_{i,j}f_{i,k}(…

(六)IT行业名博,你不知道的都在这里!!!

美团团队技术博客&#xff1a;https://tech.meituan.com/ 悦跑圈技术团队&#xff1a;https://joyrun.github.io/ 有赞技术团队&#xff1a;https://tech.youzan.com/ 360核心安全团队&#xff1a;https://blogs.360.cn/ Glow技术团队博客&#xff1a;https://tech.glowing…

图像识别:微信跳一跳机器人

准备IDE&#xff1a;VisualStudioLanguage&#xff1a;VB.NET/C#GitHub&#xff1a;AutoJump.NET本文将向你介绍一种通过图像识别实现“跳一跳”机器人的方法。 第一节 图像识别文中提到的所有方法和步骤均仅涉及简单的向量计算。需要哪些计算&#xff1f;比较像素点的颜色求向…

Spark入门(十四)之分组求最大值

一、分组求最大值 计算文本里面的每个key分组求最大值&#xff0c;输出结果。 二、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/XM…

jzoj4669-[NOIP2016提高A组模拟7.19]弄提纲【LCA,KMP,字符串】

正题 题目大意 一个字符串SSS&#xff0c;给出若干个l,rl,rl,r 求SSS以lll和rrr结尾的前缀一个公共后缀且它是SSS的前缀的子串。 求有多少和最长的那个的长度 解题思路 首先后缀前缀很容易想到KMPKMPKMP&#xff0c;我们先处理出nextnextnext数组 然后从(l,r)(l,r)(l,r)开始让…

(七)HTML和CSS 、JavaScript 和Java到底有什么区别,今天终于明白了!!!

有人曾经问过&#xff0c;“HTML CSS 和 JavaScript 以及 Java有什么区别” TCP VS UDP Java VS C 功能先上了再说 高级开发人员作为一个团队进行编程 调试CSS 高级开发人员重构代码 能一个手指完成的绝不用两只手 看实习生编码的时候&#xff0c;我的表情…… 当我尝试进入B…

Spark入门(十五)之分组求最小值

一、分组求最小值 计算文本里面的每个key分组求最小值&#xff0c;输出结果。 二、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/XM…

基于Accord.Audio和百度语言识别

目标需求使用录音形式&#xff0c;模拟微信语音聊天。按住录音&#xff0c;松开发送语音&#xff0c;并完成语音识别。ps&#xff1a;百度的语言识别有60秒长度限制&#xff0c;需要自己做好控制。实现方案采用C# winform 程序实现桌面版&#xff0c;采用Accord 实现语音录制停…

欢乐纪中A组周六赛【2019.5.18】

前言 重返纪中之日&#xff0c;又是被虐之时 成绩 JJJ表示初中&#xff0c;HHH表示高中后面加的是几年级 RankRankRankPersonPersonPersonScoreScoreScoreAAABBBCCC666(H−1)HJW(H-1)HJW(H−1)HJW191191191100100100919191000999(J−2)WYC(J-2)WYC(J−2)WYC151151151606060909…

Spark入门(十六)之分组求TOP N最小值

一、分组求TOP N最小值 计算文本里面的每个key分组求TOP N最小值&#xff0c;输出结果。 二、maven设置 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.o…

(八)Spring与MyBatis整合

持久层 目录 Mybatis 开发步骤回顾Mybatis 开发中存在的问题Spring 与 Mybatis 整合思路Spring 与 Mybatis 整合的开发步骤Spring 与 Mybatis 整合的编码搭建开发环境 pom.xmlSpring 配置文件的配置编码Spring 与 Mybatis 整合细节持久层整合总述 1、Spring 框架为什么要与持…

Git 企业开发者教程

为什么要写这样一个面向企业开发者的Git教程&#xff1f;这个问题也困扰我自己很久。其实我使用git的时间也不短了&#xff0c;但是就和正在阅读本文的每一位一样&#xff0c;常用的基本就是那么几个(git clone, git push)等等。然而git其实有着非常强大的功能&#xff0c;如果…

P1169-[ZJOI2007]棋盘制作【贪心】

正题 题目链接:https://www.luogu.org/problemnew/show/P1169 题目大意 一个矩阵中求一个最大的子矩阵和子正方形使得它们其中都是01交错。 解题思路 lefti,jleft_{i,j}lefti,j​表示(i,j)(i,j)(i,j)往左扩展多远&#xff0c;righti,jright_{i,j}righti,j​表示(i,j)(i,j)(i,…

(九)Spring 事务开发、事务属性详解

持久层 目录 事务回顾Spring 控制事务的开发Spring 中的事务属性&#xff08;Transaction Attribute&#xff09;隔离属性&#xff08;ISOLATION&#xff09;传播属性&#xff08;PROPAGATION&#xff09;只读属性&#xff08;readOnly&#xff09;超时属性&#xff08;timeo…