【发布时间】:2020-11-07 13:23:12
【问题描述】:
我正在尝试从我拥有的对象列表缓冲区中创建一个数据框。
我有一个不同 Fruit 的列表缓冲区,并希望使用这些事件创建一个 Dataframe 行(忽略案例类中的列)
import spark.implicits._
case class Fruit(
indentifier: String,
name: String)
var fruits= new scala.collection.mutable.ListBuffer[Any]()
val fruit1 = Fruit("one", "banana")
val fruit2 = Fruit("two", "apple")
val fruit3 = Fruit("three", "orange")
fruits+= fruit1
fruits += fruit2
fruits+= fruit3
val x = fruits.toSeq.toDF()
我的目标是创建不同的行,然后从这些行中创建一个数据框
fruits 中的对象数量和内容一样可以更改。在这种情况下,我试图让我的行成为:
[{"indentifier":"one", "name":"banana"}, {"indentifier":"two", "name":"apple"}, {"indentifier":"three", "name":"orange"}]
+---------------------------------------------------------------------------------------------------------------------------+
|Fruits
+------------------------------------------------------------------------------------------------------------------------------
|[{"indentifier":"one", "name":"banana"}, {"indentifier":"two", "name":"apple"}, {"indentifier":"three", "name":"orange"}] |
+-----------------------------------------------------------------------------------------------------------------------------+
我尝试做fruits.toSeq.toDF(),但不能这样做,因为“toDF 不是 Seq[Any] 的成员”
【问题讨论】:
-
你应该在范围内有一个
SparkSession,并将它的隐式导入为import sparkSession.implicits._