【发布时间】:2020-10-06 09:24:44
【问题描述】:
我有以下日志,其中包含文本和 json 字符串
2020-09-24T08:03:01.633Z 11.21.23.1 {"EventTime":"2020-09-24 13:33:01","Hostname":"abc-cde.india.local","Keywords":-1234}
为上述日志创建了 DF,如下所示
| Date |Source IP | Event Type
|2020-09-24|11.21.23.1 | {"EventTime":"202|
用于将 json 字符串转换为另一个数据帧的 crated 模式
json_schema = StructType([
StructField("EventTime", StringType()),
StructField("Hostname", StringType()),
StructField("Keywords", IntegerType())
])
json_converted_df= df.select(F.from_json(F.col('Event Type'), json_schema).alias("data")).select("data.*").show()
但数据框为所有新的 json 架构重新运行 null
+---------+--------+--------
|EventTime|Hostname|Keywords|
+---------+--------+--------
| null| null|null |
+---------+--------+--------
如何解决这个问题?
【问题讨论】:
标签: python-3.x pyspark apache-spark-sql