我想了解map和flatMap是如何工作的,但是卡在下面的一段代碼中。 flatMap()函數返回一個RDD [Char],但我期待的是RDD [String]。 有人可以解釋爲什麼它產生RDD [Char]?flatMap()函數返回RDD [Char]代替RDD [String]
scala> val inputRDD = sc.parallelize(Array(Array("This is Spark"), Array("It is a processing language"),Array("Very fast"),Array("Memory operations")))
scala> val mapRDD = inputRDD.map(x => x(0))
mapRDD: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[28] at map at <console>:26
scala> mapRDD.collect
res27: Array[String] = Array(This is Spark, It is a processing language, Very fast, Memory operations)
scala> val mapRDD = inputRDD.flatMap(x => x(0))
mapRDD: org.apache.spark.rdd.RDD[Char] = MapPartitionsRDD[29] at flatMap at <console>:26
scala> mapRDD.collect
res28: Array[Char] = Array(T, h, i, s, , i, s, , S, p, a, r, k, I, t, , i, s, , a, , p, r, o, c, e, s, s, i, n, g, , l, a, n, g, u, a, g, e, V, e, r, y, , f, a, s, t, M, e, m, o, r, y, , o, p, e, r, a, t, i, o, n, s)
可能的重複[有人可以向我解釋地圖和flatMap之間的區別,什麼是每個好的用例?](https://stackoverflow.com/questions/22350722/can-someone-explain-to- me-the-difference-between-map-and-flatmap-and-what-is-ag) –