作用

作用:只对Value进行操作

案例

每个value前面添加个前缀

  1. import org.apache.spark.rdd.RDD
  2. import org.apache.spark.{SparkConf, SparkContext}
  3. object demo {
  4. def main(args: Array[String]): Unit = {
  5. val conf: SparkConf = new SparkConf().setAppName("SparkCoreTest").setMaster("local[*]")
  6. val sc: SparkContext = new SparkContext(conf)
  7. val rdd: RDD[(Int, String)] = sc.makeRDD(List((1, "a"), (1, "d"), (2, "b"), (3, "c")))
  8. val newRDD: RDD[(Int, String)] = rdd.mapValues("|||" + _) //对value前面添加三个 |||
  9. newRDD.collect().foreach(println)
  10. /* 输出
  11. (1,|||a)
  12. (1,|||d)
  13. (2,|||b)
  14. (3,|||c)
  15. */
  16. sc.stop()
  17. }
  18. }