【问题标题】:from_json returns null in Apache Spark 3.0from_json 在 Apache Spark 3.0 中返回 null
【发布时间】:2021-08-31 04:53:11
【问题描述】:

我有一个字符串类型的 pyspark 列,其中包含一个字典数组。

     x = {"a":1,"b":[{"type":"abc","unitValue":"4.4"}]}

我想将字符串转换为 struct 数组,但是在这样做的同时,新列中的字段被填充为 null。

Databricks 运行时 - 8.3(包括 Apache Spark 3.1.1、Scala 2.12)

我的数据框看起来像:

     from pyspark.sql.functions import *
     from pyspark.sql.types import *

     inputSchema = StructType([StructField("a",StringType(),True),
                         StructField("b",StringType(),True)])

     jsonStruct = StructType([StructField("type",StringType(),True),
                         StructField("unitValue",StringType(),True)])
     
     df = spark.createDataFrame(data =[x],schema = inputSchema).show()

     +---+---------------------------+
     |  a|                   b       |
     +---+---------------------------+
     |  1|[{type=abc, unitValue=4.4}]|
     +---+---------------------------+

     df.printSchema()
     root
     |-- a: string (nullable = true)
     |-- b: string (nullable = true)

我正在使用 from_json 函数来实现相同的功能,但值被填充为 null

     df1 = df.withColumn("newvalue",from_json(col("b"),jsonStruct,{"mode" : "PERMISSIVE"}))
     display(df1)
     
     +---+----------------------------+----------------------------------+
     |  a|                   b        |    newvalue                      |
     +---+----------------------------+----------------------------------+
     |  1|[{type=abc, unitValue=xyz}] |{"type": null, "unitValue": null} |
     +---+----------------------------+----------------------------------+

谁能帮帮我

【问题讨论】:

    标签: pyspark databricks azure-databricks aws-databricks


    【解决方案1】:

    b 列中的 JSON 结构不正确。创建数据框后,: 将被 = 取代。

    您必须在声明变量本身时将b 的类型设为字符串,或者您必须使用regexp_replace() 将= 替换为:

    x = {"a":1,"b":'[{"type":"abc","unitValue":"4.4"}]'} 
    

    并且您需要更改 JSON 架构,如下所示。

    jsonStruct = ArrayType(StructType([
           StructField("type",StringType(),True), 
           StructField("unitValue",StringType(),True)]),True)
    

    【讨论】:

      【解决方案2】:

      此问题特定于 spark 3.0.0 及更高版本。 Databricks 问题链接:https://kb.databricks.com/scala/from-json-null-spark3.html 我也找到了解决方法。

           inputSchema = StructType([StructField("a",StringType(),True),StructField("b",StringType(),True)])
      
           jsonStruct = ArrayType(StructType([StructField("type",StringType(),True),StructField("unitValue",StringType(),True)]),True)
      
           x = {"a":1,"b":'[{"type":"abc","unitValue":"xyz"}]'}
      
           df = spark.createDataFrame(data =[x],schema = schema)
      
           df = df.withColumn("b",regexp_replace('b', '=', ':').cast(StringType()))
      
           df1 = df.withColumn("newvalue",from_json(col("b"),jsonStruct,{"mode" : "PERMISSIVE"}))
      
           display(df1)
      
           +---+----------------------------+----------------------------------+
           |  a|                   b        |    newvalue                      |
           +---+----------------------------+----------------------------------+
           |  1|[{type=abc, unitValue=xyz}] |{"type": "abc", "unitValue": "xyz"} |
           +---+----------------------------+----------------------------------+
      

      【讨论】:

        猜你喜欢
        • 2019-01-27
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-27
        • 2020-03-30
        • 2017-12-13
        相关资源
        最近更新 更多