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