复杂类型的模式演化

时间:2019-04-04 05:21:36

标签: apache-spark parquet orc schema-migration

在Spark中,arrays(复杂类型)中的structs的架构演化状态如何?

我知道对于常规简单类型的ORC或Parquet,都可以正常工作(添加新列),但是到目前为止,我找不到所需的文档。

我的用例是具有与此类似的结构:

user_id,date,[{event_time, foo, bar, baz, tag1, tag2, ... future_tag_n}, ...]

而且我希望能够向数组中的结构添加新字段。

使用Map(键值)复杂类型会导致效率低下吗?在那里,我至少可以确定添加新字段(标签)会很灵活。

编辑

case class BarFirst(baz:Int, foo:String)
case class BarSecond(baz:Int, foo:String, moreColumns:Int, oneMore:String)
case class BarSecondNullable(baz:Int, foo:String, moreColumns:Option[Int], oneMore:Option[String])
case class Foo(i:Int, date:String, events:Seq[BarFirst])
case class FooSecond(i:Int, date:String, events:Seq[BarSecond])
case class FooSecondNullable(i:Int, date:String, events:Seq[BarSecondNullable])
val dfInitial = Seq(Foo(1, "2019-01-01", Seq(BarFirst(1, "asdf")))).toDF
dfInitial.printSchema
dfInitial.show

root
 |-- i: integer (nullable = false)
 |-- date: string (nullable = true)
 |-- events: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- baz: integer (nullable = false)
 |    |    |-- foo: string (nullable = true)


scala> dfInitial.show
+---+----------+----------+
|  i|      date|    events|
+---+----------+----------+
|  1|2019-01-01|[[1,asdf]]|
+---+----------+----------+

dfInitial.write.partitionBy("date").parquet("my_df.parquet")

tree my_df.parquet
my_df.parquet
├── _SUCCESS
└── date=2019-01-01
    └── part-00000-fd77f730-6539-4b51-b680-b7dd5ffc04f4.c000.snappy.parquet


val evolved = Seq(FooSecond(2, "2019-01-02", Seq(BarSecond(1, "asdf", 11, "oneMore")))).toDF
evolved.printSchema
evolved.show

scala> evolved.printSchema
root
 |-- i: integer (nullable = false)
 |-- date: string (nullable = true)
 |-- events: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- baz: integer (nullable = false)
 |    |    |-- foo: string (nullable = true)
 |    |    |-- moreColumns: integer (nullable = false)
 |    |    |-- oneMore: string (nullable = true)


scala> evolved.show
+---+----------+--------------------+
|  i|      date|              events|
+---+----------+--------------------+
|  1|2019-01-02|[[1,asdf,11,oneMo...|
+---+----------+--------------------+

import org.apache.spark.sql._
evolved.write.mode(SaveMode.Append).partitionBy("date").parquet("my_df.parquet")
my_df.parquet
├── _SUCCESS
├── date=2019-01-01
│   └── part-00000-fd77f730-6539-4b51-b680-b7dd5ffc04f4.c000.snappy.parquet
└── date=2019-01-02
    └── part-00000-64e65d05-3f33-430e-af66-f1f82c23c155.c000.snappy.parquet

val df = spark.read.parquet("my_df.parquet")
df.printSchema
scala> df.printSchema
root
 |-- i: integer (nullable = true)
 |-- events: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- baz: integer (nullable = true)
 |    |    |-- foo: string (nullable = true)
 |-- date: date (nullable = true)

其他列缺失!为什么?

df.show
df.as[FooSecond].collect // AnalysisException: No such struct field moreColumns in baz, foo
df.as[FooSecondNullable].collect // AnalysisException: No such struct field moreColumns in baz, foo

针对火花2.2.3_2.11和2.4.2_2.12评估了此行为。

1 个答案:

答案 0 :(得分:0)

在编辑后(如上所述)之后执行代码时,架构合并关闭并且不加载新列。启用架构合并时:

val df = spark.read.option("mergeSchema", "true").parquet("my_df.parquet")
scala> df.printSchema
root
 |-- i: integer (nullable = true)
 |-- events: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- baz: integer (nullable = true)
 |    |    |-- foo: string (nullable = true)
 |    |    |-- moreColumns: integer (nullable = true)
 |    |    |-- oneMore: string (nullable = true)
 |-- date: date (nullable = true)

df.as [FooSecond] .collect //显然失败NullPointerException必须使用选项 df.as [FooSecondNullable] .collect //正常工作

现在正在使用配置单元

evolved.write.mode(SaveMode.Append).partitionBy("date").saveAsTable("my_df")

似乎可以正常工作(也不例外),但是当尝试读回数据时:

spark.sql("describe my_df").show(false)
+-----------------------+---------------------------------+-------+
|col_name               |data_type                        |comment|
+-----------------------+---------------------------------+-------+
|i                      |int                              |null   |
|events                 |array<struct<baz:int,foo:string>>|null   |
|date                   |string                           |null   |
|# Partition Information|                                 |       |
|# col_name             |data_type                        |comment|
|date                   |string                           |null   |
+-----------------------+---------------------------------+-------+

仅使用基本类型而不是结构数组时:

val first = Seq(Foo(1, "2019-01-01")).toDF
first.printSchema
first.write.partitionBy("dt").saveAsTable("df")
val evolved = Seq(FooEvolved(1,2, "2019-01-02")).toDF
evolved.printSchema
evolved.write.mode(SaveMode.Append).partitionBy("dt").saveAsTable("df")
evolved.write.mode(SaveMode.Append).partitionBy("dt").saveAsTable("df")
org.apache.spark.sql.AnalysisException: The column number of the existing table default.df(struct<first:int,dt:string>) doesn't match the data schema(struct<first:int,second:int,dt:string>);

有明确的错误消息 问题:是否仍可以在Hive中发展模式?还是需要对模式进行手动调整?

结论

支持结构数组的架构演化,但是在读取文件时必须打开合并选项,并且似乎仅在不使用Hive的情况下直接读取文件时才可以使用。

从配置单元读取数据时,仅返回旧模式,就像在写新列时似乎被静默删除一样。

Schema evolution in parquet format(手动创建视图,这是拼写不受支持的模式演变(可以重命名,更改数据类型)的另一个好处)看起来很有趣,因为将merge-schema选项设置为true会占用大量资源,并且适用于Hadoop上的所有SQL引擎。