【问题标题】:Spark monotonically_increasing_id() gives consecutive ids for all the partitionsSpark monotonically_increasing_id() 为所有分区提供连续的 id
【发布时间】:2021-04-21 19:28:42
【问题描述】:

我在 Spark 中有一个数据框 df,看起来像这样:

val df  = (1 to 10).toList.toDF()

当我检查分区数时,我看到我有 10 个分区:

df.rdd.getNumPartitions
res0: Int = 10

现在我生成一个 ID 列:

val dfWithID = df.withColumn("id", monotonically_increasing_id())
dfWithID.show()

+-----+---+
|value| id|
+-----+---+
|    1|  0|
|    2|  1|
|    3|  2|
|    4|  3|
|    5|  4|
|    6|  5|
|    7|  6|
|    8|  7|
|    9|  8|
|   10|  9|
+-----+---+

虽然我有 10 个分区,但所有生成的 id 都是连续的。然后我重新分区数据框:

val dfp = df.repartition(10)
val dfpWithID = dfp.withColumn("id", monotonically_increasing_id())
dfpWithID.show()

+-----+-----------+
|value|         id|
+-----+-----------+
|   10|          0|
|    1| 8589934592|
|    7|17179869184|
|    5|25769803776|
|    4|42949672960|
|    9|42949672961|
|    2|51539607552|
|    8|60129542144|
|    6|68719476736|
|    3|77309411328|
+-----+-----------+

现在我得到了不再连续的 id。根据 Spark 文档,它应该将分区 ID 放在高 31 位,在这两种情况下我都有 10 个分区。为什么调用repartition()后才添加分区ID?

【问题讨论】:

    标签: scala apache-spark partition


    【解决方案1】:

    我认为这是因为初始数据框中的所有数据都驻留在单个分区中,其他 9 个为空。

    为此,请使用此处给出的答案:Apache Spark: Get number of records per partition

    【讨论】:

    • 感谢您的回答,但我已经尝试过了。似乎所有分区都有 1 条记录。
    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2021-10-12
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多