.as-console-wrapper { max-height: 100% !important; top: 0; }
我正在尝试更改数据框列中嵌套JSON属性的类型。嵌套属性中的更改已被视为新列,从而产生重复列。对于顶级属性(Timestamp),更改正常工作,但对于嵌套的属性(a.b.c)则没有。有关这个问题的任何想法吗?。
答案 0 :(得分:1)
因为你的专栏是结构类型&您需要在同一层次结构中再次构建它。因为它不是假设,它认为你正在重写结构。 输入:
{"a": {"b": {"c": "1.31", "d": "1.11"}}, "TimeStamp": "2017-02-18", "id":1}
{"a": {"b": {"c": "2.31", "d": "2.22"}}, "TimeStamp": "2017-02-18", "id":1}
val lines2 = spark.read.json("/home/kiran/km/km_hadoop/data/data_nested_struct_col2.json")
lines2.printSchema()
val df2 = lines2.withColumn("a", struct(
struct(
lines2("a.b.c").cast(DoubleType).as("c"),
lines2("a.b.d").as("d")
).as("b")))
.withColumn("TimeStamp", lines2("TimeStamp").cast(DateType))
df2.printSchema()
这是两个模式之前和之后的输出。后:
root
|-- TimeStamp: string (nullable = true)
|-- a: struct (nullable = true)
| |-- b: struct (nullable = true)
| | |-- c: string (nullable = true)
| | |-- d: string (nullable = true)
|-- id: long (nullable = true)
root
|-- TimeStamp: date (nullable = true)
|-- a: struct (nullable = false)
| |-- b: struct (nullable = false)
| | |-- c: double (nullable = true)
| | |-- d: string (nullable = true)
|-- id: long (nullable = true)
我希望很清楚。