gpt4 book ai didi

scala - 如何使用一个或多个 StructType 创建模式(StructType)?

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

我正在尝试创建一个 StructType在另一个 StructType ,但它只允许添加 StructField .我找不到任何添加 StructType 的方法到它。

如何创建 StructType以下字符串表示的模式?

struct<abc:struct<name:string>,pqr:struct<address:string>>

最佳答案

Spark SQL 有一个隐藏的特性,可以使用所谓的 定义模式。模式 DSL (即没有许多圆括号等)。

import org.apache.spark.sql.types._
val name = new StructType().add($"name".string)
scala> println(name.simpleString)
struct<name:string>

val address = new StructType().add($"address".string)
scala> println(address.simpleString)
struct<address:string>

val schema = new StructType().add("abc", name).add("pqr", address)
scala> println(schema.simpleString)
struct<abc:struct<name:string>,pqr:struct<address:string>>

scala> schema.simpleString == "struct<abc:struct<name:string>,pqr:struct<address:string>>"
res4: Boolean = true

scala> schema.printTreeString
root
|-- abc: struct (nullable = true)
| |-- name: string (nullable = true)
|-- pqr: struct (nullable = true)
| |-- address: string (nullable = true)

关于scala - 如何使用一个或多个 StructType 创建模式(StructType)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46568435/

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