我有一个循环,它在每次迭代中生成行。我的目标是创建一个具有给定模式的数据框,该数据框仅包含那些行。我心里有一组要遵循的步骤,但我不能够在新在每次循环迭代Spark - 从循环中生成的行列表创建数据框
我尝试以下方法添加到List[Row]
:
var listOfRows = List[Row]()
val dfToExtractValues: DataFrame = ???
dfToExtractValues.foreach { x =>
//Not really important how to generate here the variables
//So to simplify all the rows will have the same values
var col1 = "firstCol"
var col2 = "secondCol"
var col3 = "thirdCol"
val newRow = RowFactory.create(col1,col2,col3)
//This step I am not able to do
//listOfRows += newRow -> Just for strings
//listOfRows.add(newRow) -> This add doesnt exist, it is a addString
//listOfRows.aggregate(1)(newRow) -> This is not how aggreage works...
}
val rdd = sc.makeRDD[RDD](listOfRows)
val dfWithNewRows = sqlContext.createDataFrame(rdd, myOriginalDF.schema)
有人能告诉我我做错了什么,或者我在改变生成一个数据框的方法时会改变什么?
也许有更好的方法来收集行而不是List [Row]。但是,我需要将其他类型的集合转换为数据框。