Spark入门(十)之Distinct去重

 一、Distinct

计算文本里面的每个单词去重,输出结果。

 

二、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 DistinctApp implements SparkConfInfo{public static void main(String[]args){String filePath = "F:\\test\\log.txt";SparkSession sparkSession = new DistinctApp().getSparkConf("distinct");List<String> words = 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).distinct().collect();words.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.

输出

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

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

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

相关文章

牛客练习赛46T1-华华教奕奕写几何【数学】

正题 题目链接:https://ac.nowcoder.com/acm/contest/894/A 题目大意 一个大半圆里面放两个小半圆要求两个小半圆的半径之和等于大半圆的半径&#xff0c;而且红色面积为SSS。 求最小的大半圆的半径。 解题思路 假设小半圆的半径为x,yx,yx,y那么大半圆的半径为xyxyxy。那么我…

(三)MySQL入门,看我就够了!!!

课程目标 一、为什么要学习数据库 二、数据库的相关概念 DBMS、DB、SQL三、数据库存储数据的特点 四、初始MySQL MySQL产品的介绍 MySQL产品的安装 ★ MySQL服务的启动和停止 ★MySQL服务的登录和退出 ★ MySQL的常见命令和语法规范 …

ABP .Net Core Entity Framework迁移使用MySql数据库

一、迁移说明ABP模板项目Entity Framework Core默认使用的是Sql Server&#xff0c;也很容易将数据库迁移到MySQL&#xff0c;步骤如下。二、迁移MySQL步骤1、 下载项目请到 http://aspnetboilerplate.com/Templates 下载一个新的项目&#xff0c;选择ASP.NET Core 2.x标签&…

Spark入门(十一)之排序

一、Sort 计算文本里面的每个单词出现的个数&#xff0c;单词个数逆序&#xff08;相同个数单词正序&#xff09;输出结果。 二、maven设置 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0&quo…

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

排序算法是《数据结构与算法》中最基本的算法之一 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…