算子,听起来很高大上的样子。我没有接触之前觉得学起来会很难!
从认知心理学角度来看,解决问题是将问题的初始状态,通过一系列的操作对问题的状态进行转换,然后将问题解决。
这里的操作就是指算子
其实算子就是函数,方法,只不过说法比较高级。
value类型交互
map(func)算子
作用:返回一个新的RDD, 该RDD由每一个输入元素经过map中的函数转换后组成
案例:创建一个包含1-10的的 RDD,然后将每个元素*2形成新的 RDD
val config: SparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
val sc = new SparkContext(config)
val listRDD: RDD[Int] = sc.makeRDD(1 to 10)
val mapRDD: RDD[Int] = listRDD.map(_*2)
mapRDD.collect().foreach(println)
mapPartitions(func)算子
作用: 类似于map(func), 但是是独立在每个分区上运行.所以:Iterator <T> => Iterator <U>
假设有N个元素,有M个分区,
那么map的函数的将被调用N次,而mapPartitions被调用M次
一个函数(mapPartitions中的函数)一次处理所有分区。
Spark中的计算是指发给Excetuer执行器的计算,mapPartition将整个分区发给执行器,减少了发个执行器的计算,减少了计算量。
而map算子将RDD中的每个数据都给个执行器,计算量较大
案例:创建一个包含1-10的的 RDD,然后将每个元素*2形成新的 RDD
命令行模式:
scala> val source = sc.parallelize(1 to 10)
source: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[5] at parallelize at <console>:24
scala> source.mapPartitions(it => it.map(_ * 2))
res7: org.apache.spark.rdd.RDD[Int] = MapPartitionsRDD[6] at mapPartitions at <console>:27
scala> res7.collect
res8: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
代码模式:
val config: SparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
val sc = new SparkContext(config)
val listRDD: RDD[Int] = sc.makeRDD(1 to 10)
//mapPartitions 可以对RDD中所有分区进行遍历
val mapPartitionsRDD: RDD[Int] = listRDD.mapPartitions(datas => {
datas.map(data=>data*2)
})
mapPartitionsRDD.collect().foreach(println)
1)mapPartitions可以对一个RDD中的所有分区进行遍历
2)mapPartitions效率优于map算子,减少了发送到执行器执行交互次数
3)mapPartitions可能会出现内存溢出(OOM)
mapPartitionsWithIndex(func)
** 作用: 和mapPartitions(func)类似. 但是会给func多提供一个Int值来表示分区的索引. **
所以func的类型是:(Int, Iterator) => Iterator
scala> val rdd1 = sc.parallelize(Array(10,20,30,40,50,60))
rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at :24
scala> rdd1.mapPartitionsWithIndex((index, items) => items.map((index, _)))
res8: org.apache.spark.rdd.RDD[(Int, Int)] = MapPartitionsRDD[3] at mapPartitionsWithIndex at :27
scala> res8.collect
res9: Array[(Int, Int)] = Array((0,10), (0,20), (0,30), (1,40), (1,50), (1,60))
分区数的确定, 和对数组中的元素如何进行分区
-
确定分区数:
override def defaultParallelism(): Int =
scheduler.conf.getInt(“spark.default.parallelism”, totalCores) -
对元素进行分区
// length: RDD 中数据的长度 numSlices: 分区数
def positions(length: Long, numSlices: Int): Iterator[(Int, Int)] = {
(0 until numSlices).iterator.map { i =>
val start = ((i * length) / numSlices).toInt
val end = (((i + 1) * length) / numSlices).toInt
(start, end)
}
}
seq match {
case r: Range =>
case nr: NumericRange[_] =>
case _ =>
val array = seq.toArray // To prevent O(n^2) operations for List etc
positions(array.length, numSlices).map { case (start, end) =>
array.slice(start, end).toSeq
}.toSeq
}
案例:创建一个RDD,使每个元素跟所在分区形成一个元组形成一个新的RDD
代码:
val config: SparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
val sc = new SparkContext(config)
val listRDD: RDD[Int] = sc.makeRDD(1 to 10, 2)//将元素放在两个分区中
//模式匹配
val tupleRDD: RDD[Int] = lisRDD.mapPartitionWithIndex{
case (num, datas) => {
//num和datas是mapPartitionWithIndex中的参数
datas.map((_, "分区号:"+num))//
}