【问题标题】:spark with column and aggregate function dropping other columns in the dataset火花与列和聚合函数删除数据集中的其他列
【发布时间】:2022-01-05 16:57:02
【问题描述】:

我有以下数据框,我已将以下数据框按idtxnIddate 分组

+---------+--------------+-------+------------+---------+------+------+
|       id|         txnId|account|        date|      idl|  type|amount|
+---------+--------------+-------+------------+---------+------+------+
|      153|   0000004512 |  30095|    11272020|       30| debit|  1000|
|      153|   0000004512 |  30096|    11272020|        0|credit|   200|
|      145|   0000004513 |  30095|    11272020|        0| debit|  4000|
|      135|   0000004512 |  30096|    11272020|        0|credit|  2000|
|      153|   0000004512 |  30097|    11272020|        0| debit|  1000|
|      145|   0000004514 |  30094|    11272020|        0| debit|  1000|
+---------+--------------+-------+------------+---------+------+------+

所以分组后的输出是

+---------+--------------+-------+------------+---------+------+------+
|       id|         txnId|account|        date|      idl|  type|amount|
+---------+--------------+-------+------------+---------+------+------+
|      153|    0000004512|  30095|    11272020|       30| debit|  1000|
|      153|    0000004512|  30096|    11272020|        0|credit|   200|
|      153|    0000004512|  30097|    11272020|        0| debit|  1000|
|      153|    0000004512|  30097|    11272020|        0|credit|   500|
|      145|    0000004513|  30095|    11272020|        0| debit|  4000|
|      145|    0000004514|  30094|    11272020|        0| debit|  1000|
|      135|    0000004512|  30096|    11272020|        0|credit|  2000|
+---------+--------------+-------+------------+---------+------+------+

我需要在数据框中添加第三和第四列,这样它就是该组的贷方或借方类型的总金额,输出应如下所示

+---------+--------------+-------+-----------+---------+------+------+-----------+----------+
|       id|         txnId|account|       date|      idl|  type|amount|totalcredit|totaldebit|
+---------+--------------+-------+-----------+---------+------+------+-----------+----------+
|      153|    0000004512|  30095|   11272020|       30| debit|  1000|          0|      2000|
|      153|    0000004512|  30096|   11272020|        0|credit|   200|        700|         0|
|      153|    0000004512|  30097|   11272020|        0| debit|  1000|          0|      2000|
|      153|    0000004512|  30097|   11272020|        0|credit|   500|        700|         0|
|      145|    0000004513|  30095|   11272020|        0| debit|  4000|          0|      4000|
|      145|    0000004514|  30094|   11272020|        0|credit|  1000|       1000|         0|
|      135|    0000004512|  30096|   11272020|        0|credit|  2000|       2000|         0|
+---------+--------------+-------+-----------+---------+------+------+-----------+----------+

我已经编写了以下代码来添加新列

Dataset <Row> df3 = df2.where(df2.col("type").equalTo("credit"))
    .groupBy("type")
    .agg(sum("amount")).withColumnRenamed("sum(amount)", "totalcredit");

但它正在从数据集中删除其他列,我如何保留数据集中的其他列?。

【问题讨论】:

    标签: java apache-spark apache-spark-sql


    【解决方案1】:

    您想在由id 分区的窗口上使用条件总和聚合:

    import org.apache.spark.sql.expressions.Window;
    import org.apache.spark.sql.expressions.WindowSpec;
    
    import static org.apache.spark.sql.functions.*;
    
    
    WindowSpec w = Window.partitionBy("id");
    
    Dataset <Row> df3 = df2.withColumn(
        "totalcredit",
        when(
            col("type").equalTo("credit"),
            sum(when(col("type").equalTo("credit"), col("amount"))).over(w)
        ).otherwise(0)
    ).withColumn(
        "totaldebit",
        when(
            col("type").equalTo("debit"),
            sum(when(col("type").equalTo("debit"), col("amount"))).over(w)
        ).otherwise(0)
    );
    
    
    df3.show();
    
    //+---+-----+-------+--------+---+------+------+-----------+----------+
    //| id|txnId|account|    date|idl|  type|amount|totalcredit|totaldebit|
    //+---+-----+-------+--------+---+------+------+-----------+----------+
    //|145| 4513|  30095|11272020|  0| debit|  4000|          0|      5000|
    //|145| 4514|  30094|11272020|  0| debit|  1000|          0|      5000|
    //|135| 4512|  30096|11272020|  0|credit|  2000|       2000|         0|
    //|153| 4512|  30095|11272020| 30| debit|  1000|          0|      2000|
    //|153| 4512|  30096|11272020|  0|credit|   200|        700|         0|
    //|153| 4512|  30097|11272020|  0| debit|  1000|          0|      2000|
    //|153| 4512|  30097|11272020|  0|credit|   500|        700|         0|
    //+---+-----+-------+--------+---+------+------+-----------+----------+
    

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2018-01-16
      • 2017-06-05
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 2018-11-30
      相关资源
      最近更新 更多