一、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

Spark入门(十)之Distinct去重相关推荐

  1. 大数据Spark面试,distinct去重原理,是如何实现的

    最近,有位朋友问我,distinct去重原理是怎么实现的? "在面试时,面试官问他了解distinct算子吗?" "了解啊,Spark的rdd,一种transFormat ...

  2. 数据分析从零到精通第二课 Hive和Spark入门

    03 离线利器:大数据离线处理工具 Hive 的常用技巧 今天为你介绍数据分析师最常用的数据处理工具 Hive 的一些使用技巧.这些技巧我们在工作中使用得比较频繁,如果运用得当,将为我们省去不少时间精 ...

  3. Spark入门系列(二)| 1小时学会RDD编程

    作者 | 梁云1991 转载自Python与算法之美(ID:Python_Ai_Road) 导读:本文为 Spark入门系列的第二篇文章,主要介绍 RDD 编程,实操性较强,感兴趣的同学可以动手实现一 ...

  4. Spark入门实战系列--6.SparkSQL(中)--深入了解SparkSQL运行计划及调优

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.1  运行环境说明 1.1.1 硬软件环境 l  主机操作系统:Windows 64位, ...

  5. Spark入门实战系列--5.Hive(下)--Hive实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.Hive操作演示 1.1 内部表 1.1.1 创建表并加载数据 第一步   启动HDFS ...

  6. Spark入门实战系列--2.Spark编译与部署(下)--Spark编译安装

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.编译Spark Spark可以通过SBT和Maven两种方式进行编译,再通过make-d ...

  7. Spark入门(Python)

    Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到"大数据(Big Data)"广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰 ...

  8. Spark入门实战系列--6.SparkSQL(上)--SparkSQL简介

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.SparkSQL的发展历程 1.1 Hive and Shark SparkSQL的前身 ...

  9. Spark入门(Python版)

    Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到"大数据(Big Data)"广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰 ...

  10. Spark入门实战系列--3.Spark编程模型(上)--编程模型及SparkShell实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.Spark编程模型 1.1 术语定义 l应用程序(Application): 基于Spa ...

最新文章

  1. sklearn快速入门教程:(五)集成学习
  2. ma5626如何进入web配置_如何给Dnsmasq提供一个Web配置界面?
  3. 新浪微博开放平台之OAuth2.0认证
  4. javascript王者归来--属性和方法的类型
  5. 数据可视化(BI报表的开发)第三天
  6. Python 列表List的定义及操作
  7. LeetCode 2200. 找出数组中的所有 K 近邻下标
  8. 插槽的使用_思维导图
  9. [NHibernate]集合类(Collections)映射
  10. asp.net添加自定义用户控件并传值
  11. 一种支持多种流媒体协议的播放内核
  12. 浮点错误是什么意思_Excel函数计算常见错误值,都是什么意思
  13. vim中字符串的替换
  14. RFID 工作频率的分类
  15. 全球及中国DIN筒夹行业运营前景与投资决策建议报告2022版
  16. freemarker 导出word文件中包含图片
  17. bios设置使用集成显卡输出视频信号后,显示器黑屏了
  18. leetcode-881 救生艇
  19. Python 的while正反三角和(菱形)
  20. 文本串加密和解密程序。一个文本串可用事先给定的字母映射表进行加密

热门文章

  1. 宝塔mysql远程链接_宝塔apache启动失败:报错 AH00526: Syntax error on line 解决方案
  2. java使用教程——组件及事件处理——窗口(设置窗口的颜色和背景)
  3. c++的assert函数
  4. idea报错Class not found (在target中没有生成对应的class文件)
  5. [JavaWeb-JavaScript]JavaScript注释数据类型
  6. 高等数学下-赵立军-北京大学出版社-题解-练习10.2
  7. 2019年第十届蓝桥杯国赛B组试题E-路径计数-dfs(坑题)
  8. 经典排序算法(12)——总结
  9. Matlab出现On Startup: Error using eval undefined function 'workspacefunc' for input arguments of type
  10. Hibernate懒加载问题的5种解决方案