理解和使用spark的flatMap的最好的一个例子


import org.apache.spark.sql.SparkSession


object Test {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder.appName("Test Application").enableHiveSupport().getOrCreate()
    import spark.implicits._

    val data = Seq(("Java", "20000"), ("Python", "100000"), ("Scala", "3000"), ("Scala", "4000"))

    val rdd = spark.sparkContext.parallelize(data)
    val dfFromRDD = rdd.toDF("language","count")
    val result = dfFromRDD.rdd.flatMap(row=> {
      val language = row.getAs[String]("language")
      for(i <- language)yield{
        i
      }
    })

    for(item<-result.collect()){
      println(item)
    }

  }
}

print结果:
J
a
v
a
P
y
t
h
o
n
S
c
a
l
a
S
c
a
l
a

上一篇:C++笔记-brpc flatmap


下一篇:Stream系列(三) FlatMap方法使用