简体   繁体   中英

rdd.map() does not call the specified function

I have a dataset of 3 items. I call a function on each item using map() but the function is never called.

object MyProgram {

  val events = Seq("A","B","C")

  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder
      .appName("MyApp")
      .config("spark.master", "local")
      .getOrCreate()

    import spark.implicits._

    val eventsDS = events.toDS()
    System.out.println("Before")
    val tempDS = eventsDS.rdd.map(x => doSomething(x))
    System.out.println("After")

  }

  def doSomething(event: String) : Unit = {
    System.out.println("Do Something!")
  }
}

Output:

Before

After

map是惰性计算的,您需要调用诸如foreach类的action来执行计算:

eventsDS.foreach(doSomething _)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM