博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spark中的常用算子
阅读量:5978 次
发布时间:2019-06-20

本文共 7143 字,大约阅读时间需要 23 分钟。

更多有用的例子和算子讲解参见:

map是对每个元素操作, mapPartitions是对其中的每个partition操作--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------mapPartitionsWithIndex : 把每个partition中的分区号和对应的值拿出来, 看源码val func = (index: Int, iter: Iterator[(Int)]) => {  iter.toList.map(x => "[partID:" +  index + ", val: " + x + "]").iterator}val rdd1 = sc.parallelize(List(1,2,3,4,5,6,7,8,9), 2)rdd1.mapPartitionsWithIndex(func).collect--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------aggregatedef func1(index: Int, iter: Iterator[(Int)]) : Iterator[String] = {  iter.toList.map(x => "[partID:" +  index + ", val: " + x + "]").iterator}val rdd1 = sc.parallelize(List(1,2,3,4,5,6,7,8,9), 2)rdd1.mapPartitionsWithIndex(func1).collect###是action操作, 第一个参数是初始值, 二:是2个函数[每个函数都是2个参数(第一个参数:先对个个分区进行合并, 第二个:对个个分区合并后的结果再进行合并), 输出一个参数]###0 + (0+1+2+3+4   +   0+5+6+7+8+9)rdd1.aggregate(0)(_+_, _+_)rdd1.aggregate(0)(math.max(_, _), _ + _)###5和1比, 得5再和234比得5 --> 5和6789比,得9 --> 5 + (5+9)rdd1.aggregate(5)(math.max(_, _), _ + _)val rdd2 = sc.parallelize(List("a","b","c","d","e","f"),2)def func2(index: Int, iter: Iterator[(String)]) : Iterator[String] = {  iter.toList.map(x => "[partID:" +  index + ", val: " + x + "]").iterator}rdd2.aggregate("")(_ + _, _ + _)rdd2.aggregate("=")(_ + _, _ + _)val rdd3 = sc.parallelize(List("12","23","345","4567"),2)rdd3.aggregate("")((x,y) => math.max(x.length, y.length).toString, (x,y) => x + y)val rdd4 = sc.parallelize(List("12","23","345",""),2)rdd4.aggregate("")((x,y) => math.min(x.length, y.length).toString, (x,y) => x + y)val rdd5 = sc.parallelize(List("12","23","","345"),2)rdd5.aggregate("")((x,y) => math.min(x.length, y.length).toString, (x,y) => x + y)--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------aggregateByKeyval pairRDD = sc.parallelize(List( ("cat",2), ("cat", 5), ("mouse", 4),("cat", 12), ("dog", 12), ("mouse", 2)), 2)def func2(index: Int, iter: Iterator[(String, Int)]) : Iterator[String] = {  iter.toList.map(x => "[partID:" +  index + ", val: " + x + "]").iterator}pairRDD.mapPartitionsWithIndex(func2).collectpairRDD.aggregateByKey(0)(math.max(_, _), _ + _).collectpairRDD.aggregateByKey(100)(math.max(_, _), _ + _).collect--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------checkpointsc.setCheckpointDir("hdfs://node-1.itcast.cn:9000/ck")val rdd = sc.textFile("hdfs://node-1.itcast.cn:9000/wc").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_)rdd.checkpointrdd.isCheckpointedrdd.countrdd.isCheckpointedrdd.getCheckpointFile--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------coalesce, repartitionval rdd1 = sc.parallelize(1 to 10, 10)val rdd2 = rdd1.coalesce(2, false)rdd2.partitions.length-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------combineByKey : 和reduceByKey是相同的效果###第一个参数x:原封不动取出来, 第二个参数:是函数, 局部运算, 第三个:是函数, 对局部运算后的结果再做运算###每个分区中每个key中value中的第一个值, (hello,1)(hello,1)(good,1)-->(hello(1,1),good(1))-->x就相当于hello的第一个1, good中的1val rdd1 = sc.textFile("hdfs://master:9000/wordcount/input/").flatMap(_.split(" ")).map((_, 1))val rdd2 = rdd1.combineByKey(x => x, (a: Int, b: Int) => a + b, (m: Int, n: Int) => m + n)rdd1.collectrdd2.collect###当input下有3个文件时(有3个block块, 不是有3个文件就有3个block, ), 每个会多加3个10val rdd3 = rdd1.combineByKey(x => x + 10, (a: Int, b: Int) => a + b, (m: Int, n: Int) => m + n)rdd3.collectval rdd4 = sc.parallelize(List("dog","cat","gnu","salmon","rabbit","turkey","wolf","bear","bee"), 3)val rdd5 = sc.parallelize(List(1,1,2,2,2,1,2,2,2), 3)val rdd6 = rdd5.zip(rdd4)val rdd7 = rdd6.combineByKey(List(_), (x: List[String], y: String) => x :+ y, (m: List[String], n: List[String]) => m ++ n)--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------countByKey val rdd1 = sc.parallelize(List(("a", 1), ("b", 2), ("b", 2), ("c", 2), ("c", 1)))rdd1.countByKeyrdd1.countByValue--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------filterByRangeval rdd1 = sc.parallelize(List(("e", 5), ("c", 3), ("d", 4), ("c", 2), ("a", 1)))val rdd2 = rdd1.filterByRange("b", "d")rdd2.collect--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------flatMapValues  :  Array((a,1), (a,2), (b,3), (b,4))val rdd3 = sc.parallelize(List(("a", "1 2"), ("b", "3 4")))val rdd4 = rdd3.flatMapValues(_.split(" "))rdd4.collect--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------foldByKeyval rdd1 = sc.parallelize(List("dog", "wolf", "cat", "bear"), 2)val rdd2 = rdd1.map(x => (x.length, x))val rdd3 = rdd2.foldByKey("")(_+_)val rdd = sc.textFile("hdfs://node-1.itcast.cn:9000/wc").flatMap(_.split(" ")).map((_, 1))rdd.foldByKey(0)(_+_)--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------foreachPartitionval rdd1 = sc.parallelize(List(1, 2, 3, 4, 5, 6, 7, 8, 9), 3)rdd1.foreachPartition(x => println(x.reduce(_ + _)))--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------keyBy : 以传入的参数做keyval rdd1 = sc.parallelize(List("dog", "salmon", "salmon", "rat", "elephant"), 3)val rdd2 = rdd1.keyBy(_.length)rdd2.collect--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------keys valuesval rdd1 = sc.parallelize(List("dog", "tiger", "lion", "cat", "panther", "eagle"), 2)val rdd2 = rdd1.map(x => (x.length, x))rdd2.keys.collectrdd2.values.collect--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------mapPartitions

 

转载于:https://www.cnblogs.com/DreamDrive/p/6759638.html

你可能感兴趣的文章
利用Nginx加GeoIP MaxMind数据库获取用户的地理位置
查看>>
日志概述
查看>>
HBase实操 | 如何使用HBase存储图片
查看>>
.NET Core SDK在Windows系统安装后出现Failed to load the hostfxr.dll等问题的解决方法
查看>>
openlayer2 三:加载geoserver图层
查看>>
Spark Worker启动源码分析
查看>>
安卓开发学习笔记(一):如何用Android Stuidio导出apk文件?
查看>>
SwiftNotice 发布——纯 Swift 编写的弹出提示及“菊花会动”库
查看>>
linux ssh 登录脚本
查看>>
React-Native组件
查看>>
css 实现文字自动换行切同行元素高度自适应
查看>>
在线图表编辑工具 draw.io 10.6.5 版本发布
查看>>
Python爬虫之Xpath学习
查看>>
CentOS 搭建 ftp 服务
查看>>
第一个 spring Boot 应用通过Docker 来实现构建、运行、发布
查看>>
在Windows平台上搭建Docker开发环境
查看>>
Flask 快速入门
查看>>
04.HTML5(拖放)
查看>>
video标签隐藏下载按钮
查看>>
生物药公司“普米斯”获1.8亿元融资,华金资本、珠海高科创投、弘晖资本联合投资...
查看>>