【发布时间】:2021-10-13 14:13:59
【问题描述】:
问题:
- 我的模式转换为 json 是否正确?
- 如何传递 json 文件为 spark.read() 提供架构
我将以下架构硬编码到 python 脚本中,这非常适合我的代码:
schema1 = StructType([
StructField("computer_name", StringType()),
StructField("owner_id", StringType())])
我想将模式从 json 文件中的脚本中移出,所以我进行了以下转换:
{"StructType":[
{"fields":[
{"metadata":{},"name":"computer_name","nullable":true,"type":"string"},
{"metadata":{},"name":"owner_id","nullable":true,"type":"string"},
"type":"struct"}
]
}
我想读取一个文件并使用 json 文件提供我自己的架构:
df=spark.read.option("header", "true").schema(schema_json_file).load(file_names)
这将引发以下错误:
ERROR: schema should be StructType or string
【问题讨论】:
标签: python json pyspark schema