mapPartitions操作与 map类似,只不过映射的参数由RDD中的每一个元素变成了RDD中每一个分区的迭代器,如果映射过程需要频繁创建额外的对象,使用mapPartitions操作要比map操作效率高效许多。比如将RDD中的所有数据通过JDBC链接写入数据库,如果使用map函数,可能要为每个元素创建一个connection,开销很大。如果使用mapPartitions,那么只需要针对一个分区建立connection.

Scala中的yield的主要作用是记住每次迭代中的有关值,并逐一存入到一个数组中。

for {子句} yield {变量或表达式}

scala> val numrdd=sc.makeRDD(1 to 10,3)

numrdd: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[51] at makeRDD at <console>:25

scala> def sumn(iter:Iterator[Int])={val aa=for(i<-iter) yield i*2;aa.toIterator}

sumn: (iter: Iterator[Int])Iterator[Int]

scala> numrdd.mapPartitions(sumn).collect

res49: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

-----------------------------------------------

分区中的数值求和

scala> val numRDD=sc.makeRDD(1 to 10,3)
numRDD: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[210] at makeRDD at <console>:25

scala> numRDD.mapPartitions(x=>{val result=List(); var i=0;while(x.hasNext){i+=x.next()};result.::(i).toIterator}).collect
res136: Array[Int] = Array(6, 15, 34)

scala> numRDD.mapPartitions(x=>{

val result=List();

var i=0;

while(x.hasNext)

{

i+=x.next()

};

result.::(i).toIterator

}

).collect
res136: Array[Int] = Array(6, 15, 34)

-------------------------------------------------------------

scala> val numRDD=sc.makeRDD(1 to 10,3)

numRDD: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[4] at makeRDD at <console>:24

scala> def partionsum(iter:Iterator[Int])={var result=List[Int]();var i:Int= 0;while(iter.hasNext){var n:Int=iter.next; i += n;} ;result.::(i).toIterator}
partionsum: (iter: Iterator[Int])Iterator[Int]

scala> def partionsum(iter:Iterator[Int])={

var result=List[Int]();

var i:Int= 0;

while(iter.hasNext){

var n:Int=iter.next;

i += n;

} ;

result.::(i).toIterator

}
partionsum: (iter: Iterator[Int])Iterator[Int]

scala> numRDD.mapPartitions(partionsum).collect

res7: Array[Int] = Array(6, 15, 34)

--------------------------------------

分区内的数值进行求和,并展示分区号

scala> val numRDD=sc.makeRDD(1 to 10,3)

numRDD: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[4] at makeRDD at <console>:24

scala> numRDD.mapPartitionsWithIndex((x,iter)=>{val result=List(); var i=0;while(iter.hasNext){i+=iter.next()};result.::(x+"|"+i).toIterator}).collect
res138: Array[String] = Array(0|6, 1|15, 2|34)

scala> numRDD.mapPartitionsWithIndex((x,iter)=>{

val result=List();

var i=0;

while(iter.hasNext){

i+=iter.next()

};

result.::(x+"|"+i).toIterator

}).collect

res138: Array[String] = Array(0|6, 1|15, 2|34)

------------------------------

scala> val numRDD=sc.makeRDD(1 to 10,3)

numRDD: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[4] at makeRDD at <console>:24

scala> def partionwithindexsum(x:Int,iter:Iterator[Int])={var result=List[Int]();var i:Int= 0;while(iter.hasNext){var n:Int=iter.next; i += n;} ;result.::(x+"|"+i).toIterator} partionwithindexsum: (x: Int, iter: Iterator[Int])Iterator[Any]

scala> def partionwithindexsum(x:Int,iter:Iterator[Int])={

var result=List[Int]();

var i:Int= 0;

while(iter.hasNext){

var n:Int=iter.next;

i += n;

} ;

result.::(x+"|"+i).toIterator

}

partionwithindexsum: (x: Int, iter: Iterator[Int])Iterator[Any]

scala> numRDD.mapPartitionsWithIndex(partionwithindexsum).collect

res9: Array[Any] = Array(0|6, 1|15, 2|34)

----------------------

统计每个分区的元素数

scala> val numRDD=sc.makeRDD(1 to 10,3)

numRDD: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[4] at makeRDD at <console>:24

scala> def partionwithindexlength(x:Int,iter:Iterator[Int])={var result=List[Int]();var i:Int= iter.toList.length;result.::(x+"|"+i).toIterator}

partionwithindexlength: (x: Int, iter: Iterator[Int])Iterator[Any]

scala> def partionwithindexlength(x:Int,iter:Iterator[Int])={

var result=List[Int]();

var i:Int= iter.toList.length;

result.::(x+"|"+i).toIterator

}

partionwithindexlength: (x: Int, iter: Iterator[Int])Iterator[Any]

scala> numRDD.mapPartitionsWithIndex(partionwithindexlength).collect

res10: Array[Any] = Array(0|3, 1|3, 2|4)

mapPartitions的更多相关文章

  1. map与mapPartitions

    区别在于sc.map是将RDD下的所有行数据统计处理.而sc.mapPartitions是按RDD分区进行数据统计处理. 测试一下: val data = sc.parallelize(1 to 6, ...

  2. spark小技巧-mapPartitions

    与map方法类似,map是对rdd中的每一个元素进行操作,而mapPartitions(foreachPartition)则是对rdd中的每个分区的迭代器进行操作.如果在map过程中需要频繁创建额外的 ...

  3. spark中map与mapPartitions区别

    在spark中,map与mapPartitions两个函数都是比较常用,这里使用代码来解释一下两者区别 import org.apache.spark.{SparkConf, SparkContext ...

  4. Spark 学习笔记之 map/flatMap/filter/mapPartitions/mapPartitionsWithIndex/sample

    map/flatMap/filter/mapPartitions/mapPartitionsWithIndex/sample:

  5. spark map和mapPartitions的区别

    package dayo1 import org.apache.spark.{SparkConf, SparkContext} import scala.collection.mutable.Arra ...

  6. java实现spark常用算子之mapPartitions

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  7. Spark API 之 map、mapPartitions、mapValues、flatMap、flatMapValues详解

    原文地址:https://blog.csdn.net/helloxiaozhe/article/details/80492933 1.创建一个RDD变量,通过help函数,查看相关函数定义和例子: & ...

  8. spark中map和mapPartitions算子的区别

    区别: 1.map是对rdd中每一个元素进行操作 2.mapPartitions是对rdd中每个partition的迭代器进行操作 mapPartitions优点: 1.若是普通map,比如一个par ...

  9. Spark算子--mapPartitions和mapPartitionsWithIndex

    mapPartitions--Transformation类算子 代码示例 result   mapPartitionsWithIndex--Transformation类算子 代码示例 result ...

随机推荐

  1. 2、以自定义struct或struct指针作为map的Key

    若干问题: struct Node { int k, b; friend bool operator <(Node a, Node b) { return a.k < b.k; } }no ...

  2. 问题解决:SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

    转载:y小川 SettingWithCopyWarning 解决方案 问题场景:我在读取csv文件之后,因为要新增一个特征列并根据已有特征修改新增列的值,结果在修改的时候就碰到了SettingWith ...

  3. [UE4]用Blenspace混合空间动画代替AimOffset动画偏移

  4. phpStudy配置站点 解决You don't have permission to access / on this server

    1.配置站点:打开phpStudy->其他选项菜单->站点域名管理 2.配置站点:打开phpStudy->其他选项菜单->打开hosts 3.在apache的配置文件vhost ...

  5. PostgreSQL手动主从切换

    主从切换操作: 1>主库宕机或者测试主备切换情况下停掉主库:systemctl stop postgres 从库会报日志错误信息:[root@db02 /]# cd /var/postgresq ...

  6. C#截取字符串按字节截取SubString

    public static string CutByteString(string str,int len)     {       string result=string.Empty;// 最终返 ...

  7. C# Microsoft.Office不存在空间名称Interop和Excel

    在实际开发过程中,我们经常会对Excel表进行操作.相信大家都都已经很熟悉C#操作Excel的步骤:添加引用->COM->Microsoft Office Excel 11 Object. ...

  8. 使用Dotfuscator混淆你的.net程序

    简介 众所周知C#等net框架的程序是无法防止反编译的,但可以通过混淆,让反编译出来的代码非常难看. Dotfuscator是微软推荐使用的第三方混淆器,用来保护你的net程序.可以在安装VS的时候顺 ...

  9. 通过表达式树把datareader和datatable转换为实体

    续上两篇文章,使用emit构造dynamic method,把 datareader转换为实体,以避免直接使用反射来实现带来的性能损失.代码看似没有纰漏,但是实际上我在framwork4下运行时,调用 ...

  10. for循环循环时间

    )) { Console.WriteLine(dt); } ("2011-5-5")       按需求定义 AddDays函数,   一天一天的增长