【问题标题】:Pyspark 'from_json', data frame return null for the all json valuesPyspark 'from_json',数据框为所有 json 值返回 null
【发布时间】: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


    【解决方案1】:

    对我来说很好......

    # Preparation of test dataset
    
    a = [
        (
            "2020-09-24T08:03:01.633Z",
            "11.21.23.1",
            '{"EventTime":"2020-09-24 13:33:01","Hostname":"abc-cde.india.local","Keywords":-1234}',
        ),
    ]
    
    b = ["Date", "Source IP", "Event Type"]
    
    df = spark.createDataFrame(a, b)
    
    df.show()
    #+--------------------+----------+--------------------+
    #|                Date| Source IP|          Event Type|
    #+--------------------+----------+--------------------+
    #|2020-09-24T08:03:...|11.21.23.1|{"EventTime":"202...|
    #+--------------------+----------+--------------------+
    
    df.printSchema()
    #root
    # |-- Date: string (nullable = true)
    # |-- Source IP: string (nullable = true)
    # |-- Event Type: string (nullable = true)
    
    # Your code executed
    from pyspark.sql.types import *
    
    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.*")
    
    json_converted_df.show()
    #+-------------------+-------------------+--------+
    #|          EventTime|           Hostname|Keywords|
    #+-------------------+-------------------+--------+
    #|2020-09-24 13:33:01|abc-cde.india.local|   -1234|
    #+-------------------+-------------------+--------+
    
    json_converted_df.printSchema()
    #root
    # |-- EventTime: string (nullable = true)
    # |-- Hostname: string (nullable = true)
    # |-- Keywords: integer (nullable = true)
    

    【讨论】:

    • @Adhi 出了什么问题?因为,显然,根本没有问题。
    • 嗨@Steven,我不知道出了什么问题......顺便说一下,你能告诉我......在将json字符串转换为另一个DF时返回空值的通常原因是什么?跨度>
    • @Adhi json_schema 输入错误。如果您将Hostname 类型替换为IntegerType,您将得到所有NULL
    猜你喜欢
    • 2019-04-28
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2016-12-28
    • 2022-01-18
    • 2021-07-02
    • 2021-02-09
    • 2019-01-27
    相关资源
    最近更新 更多