排序后的配置单元表在火花合并后再次排序

时间:2019-04-05 10:18:01

标签: scala apache-spark hive apache-spark-sql

我使用以下参数从 spark 2.3.1 开始spark-shell:

  • --master='local[*]'
  • --executor-memory=6400M
  • --driver-memory=60G
  • --conf spark.sql.autoBroadcastJoinThreshold=209715200
  • --conf spark.sql.shuffle.partitions=1000
  • --conf spark.local.dir=/data/spark-temp
  • --conf spark.driver.extraJavaOptions='-Dderby.system.home=/data/spark-catalog/'

然后使用排序和存储桶创建两个配置单元表

第一个表格名称-table1

第二个表名-table2

val storagePath = "path_to_orc"
val storage = spark.read.orc(storagePath)
val tableName = "table1"

sql(s"DROP TABLE IF EXISTS $tableName")
storage.select($"group", $"id").write.bucketBy(bucketsCount, "id").sortBy("id").saveAsTable(tableName)

(与表2相同的代码)

我希望当我将此表中的任何一个与另一个df连接时,查询计划中不会出现不必要的Exchange步骤

然后我关闭广播以使用SortMergeJoin

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", 1)

我要点df

val sample = spark.read.option("header", "true).option("delimiter", "\t").csv("path_to_tsv")

val m = spark.table("table1")
sample.select($"col" as "id").join(m, Seq("id")).explain()

== Physical Plan ==
*(4) Project [id#24, group#0]
+- *(4) SortMergeJoin [id#24], [id#1], Inner
   :- *(2) Sort [id#24 ASC NULLS FIRST], false, 0
   :  +- Exchange hashpartitioning(id#24, 1000)
   :     +- *(1) Project [col#21 AS id#24]
   :        +- *(1) Filter isnotnull(col#21)
   :           +- *(1) FileScan csv [col#21] Batched: false, Format: CSV, Location: InMemoryFileIndex[file:/samples/sample-20K], PartitionFilters: [], PushedFilters: [IsNotNull(col)], ReadSchema: struct<col:string>
   +- *(3) Project [group#0, id#1]
      +- *(3) Filter isnotnull(id#1)
         +- *(3) FileScan parquet default.table1[group#0,id#1] Batched: true, Format: Parquet, Location: InMemoryFileIndex[file:/data/table1], PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema: struct<group:string,id:string>

但是当我在加入之前对两个表使用联合

val m2 = spark.table("table2")
val mUnion = m union m2
sample.select($"col" as "id").join(mUnion, Seq("id")).explain()

== Physical Plan ==
*(6) Project [id#33, group#0]
+- *(6) SortMergeJoin [id#33], [id#1], Inner
   :- *(2) Sort [id#33 ASC NULLS FIRST], false, 0
   :  +- Exchange hashpartitioning(id#33, 1000)
   :     +- *(1) Project [col#21 AS id#33]
   :        +- *(1) Filter isnotnull(col#21)
   :           +- *(1) FileScan csv [col#21] Batched: false, Format: CSV, Location: InMemoryFileIndex[file:/samples/sample-20K], PartitionFilters: [], PushedFilters: [IsNotNull(col)], ReadSchema: struct<col:string>
   +- *(5) Sort [id#1 ASC NULLS FIRST], false, 0
      +- Exchange hashpartitioning(id#1, 1000)
         +- Union
            :- *(3) Project [group#0, id#1]
            :  +- *(3) Filter isnotnull(id#1)
            :     +- *(3) FileScan parquet default.membership_g043_append[group#0,id#1] Batched: true, Format: Parquet, Location: InMemoryFileIndex[file:/data/table1], PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema: struct<group:string,id:string>
            +- *(4) Project [group#4, id#5]
               +- *(4) Filter isnotnull(id#5)
                  +- *(4) FileScan parquet default.membership_g042[group#4,id#5] Batched: true, Format: Parquet, Location: InMemoryFileIndex[file:/data/table2], PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema: struct<group:string,id:string>

在这种情况下,出现了排序和分区(步骤5)

如何在不进行排序和交换的情况下合并两个配置单元表

1 个答案:

答案 0 :(得分:1)

据我所知,spark在加入时不考虑排序,而仅考虑分区。因此,为了获得有效的联接,必须按同一列进行分区。这是因为排序不能保证具有相同键的记录最终位于同一分区中。 Spark必须确保将具有相同值的所有键从多个数据帧中改组到同一分区和同一执行程序上。