简体   繁体   English

Spark Scala:将Case类对象添加到数据框

[英]Spark Scala : Add Case class object to dataframe

I have a simple empty dataframe created as 我有一个简单的空数据框创建为

import org.apache.spark.sql.SparkSession

lazy val sess = SparkSession.builder.appName("myapp").enableHiveSupport().getOrCreate()
case class MyClass (id:String, name:String)
val resultDf = sess.emptyDataset[MyClass]

now, i just want to create a new object, and append it to the dataframe. 现在,我只想创建一个新对象,并将其附加到数据框。 How do I do that? 我怎么做? I have tried many things but failed 我尝试了很多事情但是失败了

val x = MyClass("123", "zxc")
resultDf.union(x)

<console>:39: error: type mismatch;
 found   : ValidSignals
 required: org.apache.spark.sql.Dataset[ValidSignals]
       resultDf.union(x)

How can I convert the object to something that can be appended to the dataframe? 如何将对象转换为可以附加到数据框的对象?

union expects a Dataset[_] as an argument. union希望将Dataset[_]作为参数。 You need 你需要

val emptyDS = sess.emptyDataset[MyClass] 
val resultDS = emptyDS.union(Seq(MyClass("123","abc")).toDS())
resultDS.collect() // Array(MyClass(123,abc))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM