Spark:scala集合转化为DS/DF
Posted yy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spark:scala集合转化为DS/DF相关的知识,希望对你有一定的参考价值。
scala集合转化为DS/DF
1 case class TestPerson(name: String, age: Long, salary: Double) 2 val tom = TestPerson("Tom Hanks",37,35.5) 3 val sam = TestPerson("Sam Smith",40,40.5) 4 val PersonList = mutable.MutableList[TestPerson]() 5 PersonList += tom 6 PersonList += sam 7 8 val personDS = PersonList.toDS() 9 println(personDS.getClass) 10 personDS.show() 11 12 val personDF = PersonList.toDF() 13 println(personDF.getClass) 14 personDF.show() 15 personDF.select("name", "age").show()
输出结果:
class org.apache.spark.sql.Dataset +---------+---+------+ | name|age|salary| +---------+---+------+ |Tom Hanks| 37| 35.5| |Sam Smith| 40| 40.5| +---------+---+------+ class org.apache.spark.sql.DataFrame +---------+---+------+ | name|age|salary| +---------+---+------+ |Tom Hanks| 37| 35.5| |Sam Smith| 40| 40.5| +---------+---+------+ +---------+---+ | name|age| +---------+---+ |Tom Hanks| 37| |Sam Smith| 40| +---------+---+
参考资料:
https://stackoverflow.com/questions/39397652/convert-scala-list-to-dataframe-or-dataset
以上是关于Spark:scala集合转化为DS/DF的主要内容,如果未能解决你的问题,请参考以下文章
Scala实战高手****第14课Scala集合上的函数式编程实战及Spark源码鉴赏
通过 jenkins 运行 SonarQube 分析,为 Spark Scala 代码抛出 *** 错误