我无法理解为什么在使用 StructType() 和 MapType() 更改以下代码中的 json 列时获取 null
data = [(1, '''{"a": '1'}''')]
df = spark.createDataFrame(data, ("key", "value"))
df.show()
print()
#below lien changes the data of value column to maptype
df1=df.withColumn('data',from_json(df.value,MapType(StringType(),StringType())))
df1.show()
#below line does not changes the data of value column into maptpe
schema = StructType([StructField("a", MapType(StringType(),StringType()))])
df2=df.withColumn('data',from_json(df.value, schema))
df2.show()
# df.withColumn('data',from_json(df.value,MapType(StringType(),StringType()))) produces output
+---+----------+--------+
|key| value| data|
+---+----------+--------+
| 1|{"a": '1'}|{a -> 1}|
+---+----------+--------+
#df2=df.withColumn('data',from_json(df.value, schema)) produces output
+---+----------+------+
|key| value| data|
+---+----------+------+
| 1|{"a": '1'}|{null}|
+---+----------+------+