本文介绍了如何使用一个或多个StructTypes创建架构(StructType)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在另一个StructType
内创建一个StructType
,但是它只允许添加一个StructField
.我找不到任何向其中添加StructType
的方法.
I am trying to create a StructType
inside another StructType
, but it only allows to add a StructField
. I can't find any method to add StructType
to it.
如何为以下字符串表示形式创建StructType
模式?
How to create StructType
schema for the below string representation?
struct<abc:struct<name:string>,pqr:struct<address:string>>
推荐答案
Spark SQL的隐藏功能是使用所谓的 Schema DSL (即,没有很多圆括号等)来定义架构.
There's this hidden feature of Spark SQL to define a schema using so-called Schema DSL (i.e. without many round brackets and alike).
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)
这篇关于如何使用一个或多个StructTypes创建架构(StructType)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!