【问题标题】:Monotonically increasing ID based on column [duplicate]基于列单调递增的ID [重复]
【发布时间】:2018-06-15 21:02:14
【问题描述】:

我正在尝试向我的 spark DF 添加一个新列。我了解可以使用以下代码:

df.withColumn("row",monotonically_increasing_id)

但我的用例是:

输入 DF:

col value
  1
  2
  2
  3
  3
  3

输出DF:

col_value      identifier
  1               1
  2               1
  2               2
  3               1
  3               2
  3               3

关于使用 monotonically_increasing 或 rowWithUniqueIndex 获得此信息的任何建议。

【问题讨论】:

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


    【解决方案1】:

    根据您的要求,一种方法是使用row_number Window 函数:

    import org.apache.spark.sql.functions._
    import org.apache.spark.sql.expressions.Window
    
    val df = Seq(
      1, 2, 2, 3, 3, 3
    ).toDF("col_value")
    
    val window = Window.partitionBy("col_value").orderBy("col_value")
    df.withColumn("identifier", row_number().over(window)).
      orderBy("col_value").
      show
    // +---------+----------+
    // |col_value|identifier|
    // +---------+----------+
    // |        1|         1|
    // |        2|         1|
    // |        2|         2|
    // |        3|         1|
    // |        3|         2|
    // |        3|         3|
    // +---------+----------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      相关资源
      最近更新 更多