当用spark.read.parquet("filepath").as[MyClass]时候,如果被读取的文件schema变了,比如增加了两列,这个时候直接读取会报错,那么有一种变通的方法就是spark.read.parquet("filepath").map(e=> val field =
if (e.isNullAt(e.fieldIndex("field"))) null else e.getAs[String]("
field")
MyClass})这种形式,也就是通过判断字段存在不存在做处理,否则会直接报异常
org.apache.spark.sql.AnalysisException: No such struct field 更简便的方法是加上option.通过schema合并即可
spark.read.option("mergeSchema", "true").parquet(xxx).as[MyClass]ref:http://spark.apache.org/docs/latest/sql-programming-guide.html#schema-merging
本文介绍在使用 Spark 读取 Parquet 文件时如何处理文件 schema 的变更,包括增加字段等情况。提供了两种方法来避免因 schema 变更导致的读取失败:一种是通过 map 函数进行字段存在的判断;另一种更为简便的方法是在读取时添加 option 参数以启用 schema 合并。
1505

被折叠的 条评论
为什么被折叠?



