scala - 如何使用火花将列添加到json字段中
问题描述
我想在tt
字段中添加一列。
我尝试了以下方法,但它不正确。我能怎么做?
val schema = StructType(
Array(
StructField("tt", StructType(
Array(
StructField("c1", StringType),
StructField("c2", StringType)
)
))
)
)
val df = Seq(
"""
|{"tt":
| {
| "c1":"F6",
| "c2":"mo"
| }
|}
""".stripMargin
).toDS
val dff = spark.read.schema(schema).json(df)
dff.printSchema()
//root
// |-- tt: struct (nullable = true)
// | |-- c1: string (nullable = true)
// | |-- c2: string (nullable = true)
dff.withColumn("tt.tmp", lit("123")).show(false)
//+--------+-------------+
//|tt |tt.tmp |
//+--------+-------------+
//|[F6, mo]|123 |
//+--------+-------------+
解决方案
推荐阅读
- python - 在Django中渲染页面后在后台执行代码
- android - 是否可以通过 Activity 从片段中调用 ViewModel 方法,还是一种不好的做法?
- c++ - 合并两个数组后得到不正确的数组大小
- php - 在 Laravel 中,我的表列被连接覆盖,我该如何解决?
- arrays - 是否可以重新排列具有恒定内存开销的数组?
- mongodb - JEST:Expect() 仅数组中的唯一元素
- java - 如何计算android studio中的admob广告点击并存储在sharedprefernce中?
- javascript - 将两个回调合并为一个返回
- mongodb - mongodb:未指定身份验证机制
- unix - 如果 2 个位置在 unix 中匹配,如何从一个位置切换到另一个位置