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

Rah*_*rma 5 scala apache-spark apache-spark-sql

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

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

struct<abc:struct<name:string>,pqr:struct<address:string>>
Run Code Online (Sandbox Code Playgroud)

Jac*_*ski 7

Spark SQL的这个隐藏功能是使用所谓的Schema 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)
Run Code Online (Sandbox Code Playgroud)