【发布时间】:2020-04-18 14:32:45
【问题描述】:
我想将我的 DataFrame 保存到 Hive 表中的 Parquet 文件中...但我想按特定地图元素的值(保证存在)对该 DataFrame 进行分区。
例如:
case class Person(name: String, attributes: Map[String, String])
val people = Seq[Person](Person("John", Map("birthDate"->"2019-12-30", "favoriteColor"->"red")),
Person("Lucy", Map("birthDate"->"2019-12-31", "favoriteFood"->"pizza")),
Person("David", Map("birthDate"->"2020-01-01", "favoriteMusic"->"jazz"))).toDF
//pseudo-code, doesn't work
//people.write.format("parquet").partitionBy("attributes[birthDate]").saveAsTable("people")
我可以通过将此值提升到顶级字段并加入(见下文)来解决它,但最好避免这种情况。除了避免连接开销之外,我们的用户还需要查询属性[birthDate],因此直接在该字段上进行分区而不是单独的顶级字段将是有利的。
有没有一种方法可以直接对该值进行分区,而不需要临时的 DFs/joins?
val justNameAndBirthDate = people.select($"name", $"attributes"("birthDate")).withColumnRenamed("attributes[birthDate]", "birthDate")
val newDfWithBirthDate = people.join(justNameAndBirthDate, Seq("name"), "left")
newDfWithBirthDate.write.format("parquet").partitionBy("birthDate").saveAsTable("people")
【问题讨论】:
标签: scala apache-spark apache-spark-sql parquet