Scala 将数据帧行从列表缓冲区生成一列
我试图从我拥有的对象的listbuffer中生成一个数据帧 我有一个不同的Scala 将数据帧行从列表缓冲区生成一列,scala,dataframe,Scala,Dataframe,我试图从我拥有的对象的listbuffer中生成一个数据帧 我有一个不同的水果的列表缓冲区,希望用这些事件创建一个数据帧行(忽略case类中的列) 我的目标是创建不同的行,然后从这些行生成数据帧 myfruits中的对象数量和内容可能会发生变化。在这种情况下,我试图让我的排为: [{"indentifier":"one", "name":"banana"}, {"indentifier":&quo
水果的列表缓冲区,希望用这些事件创建一个数据帧行(忽略case类中的列)
我的目标是创建不同的行,然后从这些行生成数据帧
myfruits
中的对象数量和内容可能会发生变化。在这种情况下,我试图让我的排为:
[{"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]的成员”我们是否可以将列表缓冲区声明为case类而不是Any
?如果是这样,这对我来说很好:
var fruits= new scala.collection.mutable.ListBuffer[Fruit]()
val fruit1 = Fruit("one", "banana")
val fruit2 = Fruit("two", "apple")
val fruit3 = Fruit("three", "orange")
fruits+= fruit1
fruits += fruit2
fruits+= fruit3
val x = Seq(fruits).toDF()
// x: org.apache.spark.sql.DataFrame = [value: array<struct<indentifier:string,name:string>>]
您应该在作用域中有一个SparkSession
,并将其隐式导入为import SparkSession.implicits.\u
var fruits= new scala.collection.mutable.ListBuffer[Fruit]()
val fruit1 = Fruit("one", "banana")
val fruit2 = Fruit("two", "apple")
val fruit3 = Fruit("three", "orange")
fruits+= fruit1
fruits += fruit2
fruits+= fruit3
val x = Seq(fruits).toDF()
// x: org.apache.spark.sql.DataFrame = [value: array<struct<indentifier:string,name:string>>]
+----------------------------------------------+
|value |
+----------------------------------------------+
|[[one, banana], [two, apple], [three, orange]]|
+----------------------------------------------+