首页 > 代码库 > spark 的一些常用函数 filter,map,flatMap,lookup ,reduce,groupByKey
spark 的一些常用函数 filter,map,flatMap,lookup ,reduce,groupByKey
定义不带参数也不带返回值的函数(def :定义函数的关键字 printz:方法名称)
scala> def printz = print("scala hello")
scala> def minNum(x:Int,y:Int):Int = if(x>y) x else y //:Int 是该函数的返回值类型 minNum: (x: Int, y: Int)Int
scala> minNum(2,3) res10: Int = 3
scala> val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF") colors: scala.collection.immutable.Map[String,String] = Map(red -> #FF0000, azur e -> #F0FFFF)
scala> colors.map(x=>(x._2,x._1)) res0: scala.collection.immutable.Map[String,String] = Map(#FF0000 -> red, #F0FFF F -> azure)
scala> var rdd1=sc.parallelize(List(1,2,3,4))//创建rdd1 rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[53] at parallelize at <console>:24 scala> var rdd2=sc.parallelize(List(5,6,7,8))//创建rdd2 rdd2: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[54] at parallelize at <console>:24 scala> var unionRes=rdd1 union rdd2 //合并rdd unionRes: org.apache.spark.rdd.RDD[Int] = UnionRDD[55] at union at <console>:28 scala> unionRes.collect //查合并后的rdd res19: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)
scala> var rdd1=sc.parallelize(List((1,"spark"),(2,"hadoop"),(3,"scala"),(4,"java"))) //创建rdd1 rdd1: org.apache.spark.rdd.RDD[(Int, String)] = ParallelCollectionRDD[58] at parallelize at <console>:24 scala> var rdd2=sc.parallelize(List((1,"30k"),(2,"28k"),(3,"5k"),(5,"10k"))) //创建rddd2 rdd2: org.apache.spark.rdd.RDD[(Int, String)] = ParallelCollectionRDD[59] at parallelize at <console>:24 scala> var joinRes=rdd1 join rdd2 //join rdd joinRes: org.apache.spark.rdd.RDD[(Int, (String, String))] = MapPartitionsRDD[62] at join at <console>:28 scala> joinRes.collect //查看join后的结果,可以看出join会把两个rdd有相同key的元素的值进行合并 res20: Array[(Int, (String, String))] = Array((1,(spark,30k)), (3,(scala,5k)), (2,(hadoop,28k)))
//这个是以左边的rdd为主,右边为辅。右边与左边没有匹配项,则会显示左边的值,所以这里key为4的也会显示 scala> var leftJoinRes=rdd1.leftOuterJoin(rdd2) leftJoinRes: org.apache.spark.rdd.RDD[(Int, (String, Option[String]))] = MapPartitionsRDD[65] at leftOuterJoin at <console>:28 scala> leftJoinRes.collect res21: Array[(Int, (String, Option[String]))] = Array((4,(java,None)), (1,(spark,Some(30k))), (3,(scala,Some(5k))), (2,(hadoop,Some(28k))))
//这个是以右边边的rdd为主,左边边为辅。右边与左边没有匹配项,则只显示右边,所以这里key为5的也会显示 scala> var rightJoinRes=rdd1.rightOuterJoin(rdd2) rightJoinRes: org.apache.spark.rdd.RDD[(Int, (Option[String], String))] = MapPartitionsRDD[68] at rightOuterJoin at <console>:28 scala> rightJoinRes.collect res22: Array[(Int, (Option[String], String))] = Array((1,(Some(spark),30k)), (3,(Some(scala),5k)), (5,(None,10k)), (2,(Some(hadoop),28k)))
scala> f1.flatMap(x=>x.split("-")).map((_,1)).groupByKey.collect res28: Array[(String, Iterable[Int])] = Array((4,CompactBuffer(1)), (7,CompactBuffer(1, 1, 1, 1)), (6,CompactBuffer(1, 1, 1)), (2,CompactBuffer(1, 1)), (3,CompactBuffer(1, 1, 1)), (1,CompactBuffer(1, 1, 1)))
scala> var rdd1=sc.parallelize(List(1,2,3,4,5)) rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[80] at parallelize at <console>:24 scala> rdd1.reduce(_+_) res32: Int = 15
scala> var rdd1=sc.parallelize(List((1,"a"),(2,"b"),(3,"c"))) rdd1: org.apache.spark.rdd.RDD[(Int, String)] = ParallelCollectionRDD[81] at parallelize at <console>:24 scala> rdd1.lookup(1) res34: Seq[String] = WrappedArray(a)
Array[Student] = Array(Student(zhangxs,24,chenxy), Student(wangYr,21,teacher), Student(wangx,26,teacher))
scala> val a = sc.parallelize(1 to 9, 3) scala> val b = a.map(x => x*2) scala> a.collect res10: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> b.collect res11: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14, 16, 18)
上述例子中把原RDD中每个元素都乘以2来产生一个新的RDD。
res9: Array[String] = Array(zhangxs, 24, chenxy, wangYr, 21, teacher, wangx, 26, teacher)
mapPartitions
mapPartitions是map的一个变种。map的输入函数是应用于RDD中每个元素,而mapPartitions的输入函数是应用于每个分区,也就是把每个分区中的内容作为整体来处理的。
它的函数定义为:
def mapPartitions[U: ClassTag](f: Iterator[T] => Iterator[U], preservesPartitioning: Boolean = false): RDD[U]
f即为输入函数,它处理每个分区里面的内容。每个分区中的内容将以Iterator[T]传递给输入函数f,f的输出结果是Iterator[U]。最终的RDD由所有分区经过输入函数处理后的结果合并起来的。
比如
scala> val a = sc.parallelize(1 to 9, 3) scala> def myfunc[T](iter: Iterator[T]) : Iterator[(T, T)] = { var res = List[(T, T)]() var pre = iter.next while (iter.hasNext) { val cur = iter.next; res .::= (pre, cur) pre = cur; } res.iterator } scala> a.mapPartitions(myfunc).collect res0: Array[(Int, Int)] = Array((2,3), (1,2), (5,6), (4,5), (8,9), (7,8))
上述例子中的函数myfunc是把分区中一个元素和它的下一个元素组成一个Tuple。因为分区中最后一个元素没有下一个元素了,所以(3,4)和(6,7)不在结果中。
mapPartitions还有些变种,比如mapPartitionsWithContext,它能把处理过程中的一些状态信息传递给用户指定的输入函数。还有mapPartitionsWithIndex,它能把分区的index传递给用户指定的输入函数。
mapValues
mapValues顾名思义就是输入函数应用于RDD中Kev-Value的Value,原RDD中的Key保持不变,与新的Value一起组成新的RDD中的元素。因此,该函数只适用于元素为KV对的RDD。
scala> val a = sc.parallelize(List("dog", "tiger", "lion", "cat", "panther", " eagle"), 2) scala> val b = a.map(x => (x.length, x)) //key:元组的长度 value:元组的值 scala> b.mapValues("x" + _ + "x").collect //“_”就是每个元组的value,在每个value的前后加"x" res5: Array[(Int, String)] = Array((3,xdogx), (5,xtigerx), (4,xlionx),(3,xcatx), (7,xpantherx), (5,xeaglex))
mapWith
mapWith是map的另外一个变种,map只需要一个输入函数,而mapWith有两个输入函数。它的定义如下:
def mapWith[A: ClassTag, U: ](constructA: Int => A, preservesPartitioning: Boolean = false)(f: (T, A) => U): RDD[U]
- 第一个函数constructA是把RDD的partition index(index从0开始)作为输入,输出为新类型A;
- 第二个函数f是把二元组(T, A)作为输入(其中T为原RDD中的元素,A为第一个函数的输出),输出类型为U。
比如把partition index 乘以10,然后加上2作为新的RDD的元素。
val x = sc.parallelize(List(1,2,3,4,5,6,7,8,9,10), 3) x.mapWith(a => a * 10)((a, b) => (b + 2)).collect res4: Array[Int] = Array(2, 2, 2, 12, 12, 12, 22, 22, 22, 22)
spark 的一些常用函数 filter,map,flatMap,lookup ,reduce,groupByKey