gpt4 book ai didi

scala - 如何在不使用案例类但使用 StructType 的情况下创建数据集(不是 DataFrame)?

转载 作者:行者123 更新时间:2023-12-04 17:55:14 25 4
gpt4 key购买 nike

如何使用 StructType 创建数据集?

我们可以如下创建一个Dataset:

case class Person(name: String, age: Int)

val personDS = Seq(Person("Max", 33), Person("Adam", 32), Person("Muller",
62)).toDS()
personDS.show()

有没有办法在不使用案例类的情况下创建 Dataset

我想使用案例类和 StructType 创建一个 DataFrame

最佳答案

如果您知道如何创建 DataFrame,那么您现在已经知道如何创建 Dataset :)

DataFrame = Dataset[Row].

这是什么意思?试试:

val df : DataFrame = spark.createDataFrame(...) // with StructType
import org.apache.spark.sql._
val ds : Dataset[Row] = df; // no error, as DataFrame is only a type alias of Dataset[Row]

关于scala - 如何在不使用案例类但使用 StructType 的情况下创建数据集(不是 DataFrame)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46285303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com