如何在不使用案例类但使用 StructType 的情况下创建数据集(不是数据帧)? [英] How to create Dataset (not DataFrame) without using case class but using StructType?

查看:23
本文介绍了如何在不使用案例类但使用 StructType 的情况下创建数据集(不是数据帧)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 StructType 创建数据集?

How can I create Dataset using StructType?

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

We can create a Dataset as follows:

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

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

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

Is there a way to create a Dataset without using a case class?

我想使用 case 类和 StructType 创建一个 DataFrame.

I'd like to create a DataFrame using a case class and using StructType.

推荐答案

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

If you know how to create DataFrame, you already now how to create 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]

这篇关于如何在不使用案例类但使用 StructType 的情况下创建数据集(不是数据帧)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆