spark 的transformations之map,flatMap,mapPartitions,mapPartitionsWithIndex的用法
来源:互联网 发布:mysql安装win7 64位 编辑:程序博客网 时间:2024/05/17 06:42
版本spark1.6.1
spark的编程思想跟mapreduce有很大的相似之处,这几个函数都可以看做是类似在map端的操作处理。
一个RDD(分布式弹性数据集),包含n个partition,你可以将每个partition看做是类似map的操作。
TransformationMeaningmap(func) Return a new distributed dataset formed by passing each element of the source through a function func.flatMap(func) Similar to map, but each input item can be mapped to 0 or more output items (so func should return a Seq rather than a single item).mapPartitions(func) Similar to map, but runs separately on each partition (block) of the RDD, so func must be of type Iterator<T> => Iterator<U> when running on an RDD of type T.mapPartitionsWithIndex(func) Similar to mapPartitions, but also provides func with an integer value representing the index of the partition, so func must be of type (Int, Iterator<T>) => Iterator<U> when running on an RDD of type T.这几个transformations的主要区别:
map和flatMap的func接受的都是一个item参数,map返回也是一个item,flatMap返回是一个Seq
mapPartitions的func函数接收的是一个迭代器,返回也是一个迭代器
import org.apache.spark.SparkConfimport org.apache.spark.SparkContextimport scala.collection.mutable.ArrayBufferobject Test2 { def main(args:Array[String]){ val sparkconf = new SparkConf().setAppName("test2").setMaster("local") val sc = new SparkContext(sparkconf) val list = List(1,2,3,4,5,6,7,8,9,0) val rdd = sc.parallelize(list, 3); // map : transformations // map(func) Return a new distributed dataset formed by passing each element of // the source through a function func. //map的作用是将 T =》U val rddmap = rdd.map(_+10) rddmap.foreach ( println ) // flatMap : Transformations // flatMap(func) Similar to map, but each input item can be mapped to 0 or more output items //(so func should return a Seq rather than a single item). //flatMap T=>Seq(u) //flatMap与map的区别在于,flatMap对于每一条item,会输出一个Seq,而map只是一个另一个item val rddflatmap = rdd.flatMap ( x => for(y<-1 to x) yield y ) println("count="+rddflatmap.count()) rddflatmap.foreach ( x=>println("rddflatmap="+x) ) //mapPartitions : Transformations //mapPartitions(func) Similar to map, but runs separately on each partition (block) of the RDD, //so func must be of type Iterator<T> => Iterator<U> when running on an RDD of type T. //mapPartitions的func接收的参数是一个迭代器(iter),然后输出的是另一个迭代器(iter)。作用于该RDD中的每一个partition val rddmappartitions = rdd.mapPartitions(iter => { val s= new ArrayBuffer[Int](); while(iter.hasNext) {val m=iter.next(); for(i<-(1 to m)) s+=i } ; s.iterator}, false) rddmappartitions.foreach ( println ) //mapPartitionsWithIndex : Transformations //mapPartitionsWithIndex(func) Similar to mapPartitions, but also provides func with an integer value //representing the index of the partition, so func must be of type (Int, Iterator<T>) => Iterator<U> //when running on an RDD of type T. //mapPartitionsWithIndex相比mapPartitions,只是增加了一个partition id。 val mapPartitionsWithIndex = rdd.mapPartitionsWithIndex((x,y)=>{println("partition id is "+x);if(x==0){for(t<-y) yield t+10};else for(t<-y) yield 0; }, false); mapPartitionsWithIndex.foreach(println) }}
0 0
- spark 的transformations之map,flatMap,mapPartitions,mapPartitionsWithIndex的用法
- Spark编程之基本的RDD算子之map,mapPartitions, mapPartitionsWithIndex.
- spark:map mapPartitions flatmap
- spark:map mapPartitions flatmap
- spark map flatMap flatMapToPair mapPartitions 的区别和用途
- Spark RDD中Transformation的map、flatMap、mapPartitions、glom详解
- spark map flatMap flatMapToPair mapPartitions 的区别和用途
- spark学习-19-Spark的mapPartitions与MapPartitionsWithIndex理解
- 通过代码实例来说明spark api mapPartitions和mapPartitionsWithIndex的使用
- 通过代码实例来说明spark api mapPartitions和mapPartitionsWithIndex的使用
- Spark API 详解/大白话解释 之 map、mapPartitions、mapValues、mapWith、flatMap、flatMapWith、flatMapValues
- Spark API 详解/大白话解释 之 map、mapPartitions、mapValues、mapWith、flatMap、flatMapWith、flatMapValues
- 第45课 Spark 2.0实战之Dataset:map、flatMap、mapPartitions、dropDuplicate、coalesce、repartition等
- spark map和mapPartitions的区别
- Spark API 之 map、mapPartitions
- Spark的flatMap和Map的区别
- Spark 中 map 与 flatMap 的区别
- Spark 中 map 与 flatMap 的区别
- 分类中使用@property注意点
- java集合面试锦集
- js生成和下载二维码
- JAVA从菜鸟【入门】到新手【实习】——Github熟悉
- Git_Git远程操作_第2课_Git远程协作的主要命令
- spark 的transformations之map,flatMap,mapPartitions,mapPartitionsWithIndex的用法
- Oracle Navicat连接oracle报错setZHS16GBK
- 对称矩阵&稀疏矩阵的两种转置算法
- IOS 国际化语言
- ASIHttpRequest
- 使用Docker创建JavaWeb运行环境(亲测)
- servlet3.1规范: 第1章 Servlet概览
- 详解Visual Studio 2010中QT环境搭建配置
- [Leetcode] Majority Element II