scala—如何使用spark将列添加到json字段

xlpyo6sf  于 2021-05-19  发布在  Spark
关注(0)|答案(0)|浏览(410)

我想在中添加一列 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          |
//+--------+-------------+

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题