加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 09:52:02 所属栏目:安全 来源:网络整理
导读:我试图在另一个StructType中创建一个StructType,但它只允许添加一个StructField.我找不到任何方法来向其添加StructType. 如何为以下字符串表示创建StructType模式? structabc:structname:string,pqr:structaddress:string 解决方法 Spark SQL的这个隐藏功能
我试图在另一个StructType中创建一个StructType,但它只允许添加一个StructField.我找不到任何方法来向其添加StructType.

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

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

解决方法

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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读