spark算子map reduce小案例

Spark常用算子详解汇总 : 实战案例、Java版本、Scala版本 Spark算子[01]:foreach,foreachPartition Spark算子[02]:coalesce,repartition Spark算子[03]:mapPartitions,mapPartitionsWithIndex 源码实战案例分析 Spark算子[04]:map,flatMap,mapToPair,flatMapToPair Spar 阅读详情

1.RDD两种操作方式介绍

1.1 转换操作(Transformation)

转换操作(Transformation)使用了链式调用的设计模式,对一个RDD计算之后,转换为另一个RDD,然后这个RDD又可以经过转换,转换为另一个RDD。这个过程是分布式的,它 必须等待行动操作(Action)完成后,才真正的触发Spark提交作业,开始执行计算。

1.2行动操作(Action)

行动操作Action是和转换操作相对应的一种操作,在Spark程序运行中,每调用一次Action操作,都会触发一次Spark的作业提交,并 返回相应的结果。

2.转换操作map()介绍

map函数定义如下:
def
map[U](f: (T) ⇒ U)(implicit arg0: ClassTag[U]): RDD[U]
 Permalink
Return a new RDD by applying a function to all elements of this RDD.
map方法将原来的RDD中类型为T的元素,通过用户自定义的函数f按照一对一地映射为U类型的元素。函数f可以是简单的加减,也可以是复杂的将一元组转换为二元组等等。
 val rdd1 = sc.parallelize(List(1,2,3,4))
 val maprdd1 = rdd1.map(x => x+1)
结果为:2 3 4 5 (当然结果应该是按列列出,看完整代码如下)

3.行动操作reduce()操作介绍

行动操作reduce()它接收一个函数作为参数,这个 函数要操作两个 RDD 的元素类型的数据并返
Spark大数据处理学习笔记3.2 掌握RDD算子 上面这个映射写成函数: f ( x ) = 2 x , x ∈ R f(x)=2x,x\in \Bbb R f(x)=2x,x∈R。一个数组中存放了三个元组,将该数组转为RDD集合,然后对该RDD按照每个元素中的第二个值进行降序排列。,不是一元函数,而是二元函数,系统立马就蒙逼了,不晓得该怎么取两个参数来进行乘法运算。中的x代表rdd1中的每个元素。因为RDD的元素为分布式的,数据可能分布在不同的节点上。Spark会将RDD中的每个元素传入该函数的参数中。上述代码中,向算子map()传入了一个函数。 阅读详情

相关推荐

Spark核心编程(RDD行动算子)-action

文章目录RDD行动算子reducecollectcountcountByValuefirsttaketakeOrderedaggregatefoldcountByKeysava相关算子foreach RDD行动算子 所谓的行动算子,其实不会再产生新的RDD,而是触发作业的执行。行动算子执行后,会获取到作业的执行结果。 转换算子不会触发作业的执行,只是功能的扩展和包装。 Spark的行动算子执行时,会产生Job对象,然后提交这个Job对象。 reduce 函数签名: def reduce(f: (T,

Sarahdsy的博客 636

MapReduce 与 Spark 3.5 核心算子对比:WordCount 案例的 5 倍性能差异与适用场景分析

本文对比了MapReduce与Spark 3.5在WordCount案例中的性能差异与适用场景,揭示了Spark 3.5在内存计算、DAG调度等方面的5倍性能优势。通过实测数据和核心算子分析,为数据工程师提供了技术选型和优化建议,帮助提升大数据处理效率。

weixin_30613343的博客 427

Spark中reduce算子详解介绍

前面有篇文章讲解了如何使用 `reduceByKey` 这个算子来对键值类型数据进行聚合,该算子实现了将相同key的数据在分区内和分区间按照我们的指定逻辑进行聚合,不过该算子只使用于键值类型,本篇文章讲解更为灵活的聚合算子 `reduce` ,该算子可以处理单值类型和键值类型数据。

优快云 精品推荐 1182

Spark基础学习笔记17:掌握RDD算子

RDD转化算子、RDD行动算子

howard2005的专栏 2947

Mplus—交叉滞后面板模型(Cross-Lagged Panel Model, CLPM)

本文介绍交叉滞后模型相关知识,以及显变量模型与潜变量模型在Mplus中的语法。

m0_54401011的博客 1万+

基于ResNet-50的猫狗分类训练

基于ResNet-50的猫狗分类python训练

weixin_46352497的博客 1004

Arduino程序设计(十)8×8 共阴极LED点阵显示(MAX7219)

本文主要介绍8×8 LED点阵显示实验,分别是:1、介绍MAX7219点阵模块;2、点阵显示部分图形;3、点阵显示数字 0~9 及 26 个英文字母。1、通过上述两个点阵实验,了解了8*8点阵显示的基本原理;2、通过SPI通信控制点阵显示,学会了Arduino用SPI通信方式控制外设。

Alice的博客 1万+

FPGA实现串口的任意字节数接收

基于FPGA的任意字节数(单字节、多字节)的串口(UART)接收,真的不来看看吗?

孤独的单刀 1万+

使用 Python 实现基于 AGA8 GERG - 2008 方程计算掺氢天然气压缩因子的示例代码

AGA8 GERG - 2008 方程是用于计算天然气混合物热力学性质的一种方法,下面是一个使用 Python 实现基于 AGA8 GERG - 2008 方程计算掺氢天然气压缩因子的示例代码。需要注意的是,AGA8 GERG - 2008 方程非常复杂,完整实现需要大量的系数和详细的计算步骤。这里我们简化了部分过程,使用CoolProp库来完成计算,因为CoolProp已经实现了 GERG - 2008 方程。

AI的内部世界的博客 887

RK3576 Buildroot 双机 ADB 异常排障实录:同版 `boot.img`,一台可识别一台不可识别

进程在跑”不等于“USB 枚举成功”,adbd存活只是必要不充分条件。生产环境中的全局别名可能反向污染系统脚本。shell 命令替换中的字符串问题,必须做字节级验证(od -tx1相比“改环境规避”,核心脚本对输入做鲁棒处理更可长期维护。

小灰灰的博客 212

★教程3:Simulink学习教程入门60例目录

1.订阅本教程用户可以免费获得本博任意1个(包括所有免费专栏和付费专栏)博文对应代码;2.本Simulink课程的所有案例(部分理论知识点除外)均由博主编写而成,供有兴趣的朋友们自己订阅学习使用。未经本人允许,禁止任何形式的商业用途;3.本课程除了介绍常见的Simulink模块介绍之外,我们更侧重于各种实例的完整设计介绍。从通信,控制器,图像处理,语音处理,电力系统等诸多常用领域介绍了相关案例,如果对于某个较为复杂的案例,初学者无法正确复现,也可以私信博主获得完整Simulink模型文件。

FPGA/MATLAB学习教程/源码/项目合作开发 1万+

rk3576 点亮 LCD(eDP)

RK3576 处理器集成了对 **eDP (Embedded DisplayPort)** 接口的支持,主要用于驱动嵌入式或笔记本电脑的内置屏幕。支持 **eDP 1.3 版本协议**, 最大可支持 ****4K@120Hz**。

weixin_35723192的博客 354

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

上一篇: spark算子union使用
下一篇: spark算子flatMap与textFile、saveAsTextFile结合小案例
RiverCode
博客等级 码龄10年 39粉丝 28原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值