Spark Json - применить схему с nullable = false - PullRequest
0 голосов
/ 26 мая 2020

Я пытаюсь применить nullable = false для моего файла Json. Он всегда показывает значение по умолчанию как nullable = true. написал свою схему.

val carsSchema = StructType(Array(
    StructField("Name", StringType),
    StructField("Miles_per_Gallon", DoubleType,nullable = false),
    StructField("Cylinders", LongType),
    StructField("Displacement", DoubleType),
    StructField("Horsepower", LongType),
    StructField("Weight_in_lbs", LongType),
    StructField("Acceleration", DoubleType),
    StructField("Year", StringType),
    StructField("Origin", StringType)))

df.show ()

root
 |-- Name: string (nullable = true)
 |-- Miles_per_Gallon: double (nullable = true)
 |-- Cylinders: long (nullable = true)
 |-- Displacement: double (nullable = true)
 |-- Horsepower: long (nullable = true)
 |-- Weight_in_lbs: long (nullable = true)
 |-- Acceleration: double (nullable = true)
 |-- Year: string (nullable = true)
 |-- Origin: string (nullable = true)

После некоторого исследования и преобразования в RDD, а затем применить к DF, используя приведенный ниже код.

val jsonRDD = spark.sparkContext.textFile(carsDataWithErrorjsonfile)
  val carDF = spark.read
            //.format("json")
          //.option("inferSchema", true)
          .schema(carsSchema)
          .option("mode","permisive") //failFast,permisive,dropMalformed,
          .json(jsonRDD)

enter image description here

он работает как положено. Но IDE показывает, что метод, переданный как rdd to json, устарел. У вас есть альтернативный вариант для установки nullable = false.

образец набора данных

{"Name":"chevrolet chevelle malibu", "Miles_per_Gallon":18, "Cylinders":8, "Displacement":307, "Horsepower":130, "Weight_in_lbs":3504, "Acceleration":12, "Year":"1970-01-01", "Origin":"USA"}
...