【发布时间】:2023-01-20 04:45:11
【问题描述】:
使用自定义模式读取 JSON 时,它会为我提供所有 NULL 值。我知道原因(因为实际数据类型与自定义模式类型不匹配)但我不知道如何修复它(除了读取它 with open 方法)。我想让你阅读 JSON 模块。
spark = SparkSession \
.builder \
.appName("JSON test") \
.getOrCreate()
schema = StructType([StructField("_links", MapType(StringType(), MapType(StringType(), StringType()))),
StructField("identifier", StringType()),
StructField("enabled", BooleanType()),
StructField("family", StringType()),
StructField("categories", ArrayType(StringType())),
StructField("groups", ArrayType(StringType())),
StructField("parent", StringType()),
StructField("values", MapType(StringType(), ArrayType(MapType(StringType(), StringType())))),
StructField("created", StringType()),
StructField("updated", StringType()),
StructField("associations", MapType(StringType(), MapType(StringType(), ArrayType(StringType())))),
StructField("quantified_associations", MapType(StringType(), IntegerType())),
StructField("metadata", MapType(StringType(), StringType()))])
df = spark.read.format("json") \
.schema(schema) \
.load(f'/mnt/bronze/products/**/*.json')
df.display()
【问题讨论】:
标签: json apache-spark pyspark