Spark Delta Lake 合并上的分区修剪

pro*_*add 6 apache-spark delta-lake

我正在使用 Delta Lake ("io.delta" %% "delta-core" % "0.4.0") 并合并到 foreachBatch 中,如下所示:

foreachBatch { (s, batchid) =>
        deltaTable.alias("t")
          .merge(
            s.as("s"),
            "s.eventid = t.eventid and t.categories in ('a1', 'a2')")
          .whenMatched("s.eventtime < t.eventtime").updateAll()
          .whenNotMatched().insertAll()
          .execute()
      }
Run Code Online (Sandbox Code Playgroud)

增量表按类别进行分区。如果我添加分区过滤器,例如“and t.categories in ('a1', 'a2')”,从火花图中我可以看到输入不是整个表。我认为它做了分区修剪。但是,如果我这样做:“s.eventid = t.eventid and t.categories=s.categories”,它仍然会加载增量表中的所有数据。我希望它能够自动感知应该去哪些分区进行连接,类似于下推。是否可以在不指定特定分区值的情况下进行分区修剪?我还尝试添加 ("spark.databricks.optimizer.dynamicPartitionPruning","true") 但不起作用。

谢谢

Nik*_*iya 3

你可以通过两种方式传递这个信息。一种是传递值的静态方式,另一种是在合并语句中动态设置分区。

  1. 传递分区值的静态方式。
val categoriesList = List("a1", "a2")  
val catergoryPartitionList  = categoriesList.mkString("','")

foreachBatch { (s, batchid) =>
    deltaTable.alias("t")
      .merge(
        s.as("s"),
        "s.eventid = t.eventid and t.categories in ('$catergoryPartitionList')")
      .whenMatched("s.eventtime < t.eventtime").updateAll()
      .whenNotMatched().insertAll()
      .execute()
  }
Run Code Online (Sandbox Code Playgroud)
  1. 将类别传递给 Merge 语句的动态方式如下:
val selectedCategories = deltaTable.select("categories").dropDuplicates()
  
val categoriesList = selectedCategories.map(_.getString(0)).collect()

val catergoryPartitionList  = categoriesList.mkString("','")

foreachBatch { (s, batchid) =>
    deltaTable.alias("t")
      .merge(
        s.as("s"),
        "s.eventid = t.eventid and t.categories in ('$catergoryPartitionList')")
      .whenMatched("s.eventtime < t.eventtime").updateAll()
      .whenNotMatched().insertAll()
      .execute()
  }
Run Code Online (Sandbox Code Playgroud)