【问题标题】:Dataset Manipulation in Spark Java APISpark Java API 中的数据集操作
【发布时间】:2018-05-09 09:44:53
【问题描述】:

我在下面有一个Dataset DS1。我想使用 Spark Java API 构建 DS2。

DS1:

+---------+------------+------------+
|  account|    amount  |    type    |
+---------+------------+------------+
| c1      |      100   |      D     |
| c1      |      200   |      C     |
| c2      |      500   |      C     |

DS2:

amount1 是 DS1 amount,其中 type = Damount2 是 DS1 amount,其中 type = C

+---------+------------+------------+
|  account|    amount1 |   amount2  |
+---------+------------+------------+
| c1      |      100   |      200   |
| c2      |      0     |      500   |

有人可以帮帮我吗?

【问题讨论】:

  • 预期的数据帧与解释的逻辑不匹配

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


【解决方案1】:

要将ds1 转换为预期格式的ds2,您可以使用以下代码-

val ds2 = ds1
           .withColumn("amount1", when($"type" === "D", $"amount").otherwise(0))
           .withColumn("amount2", when($"type" === "C", $"amount").otherwise(0))
           .select("account", "amount1", "amount2")
           .groupBy($"account")
           .agg(Map("amount1" -> "sum", "amount2" -> "sum"))

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 2018-12-19
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2019-02-11
    • 2016-07-27
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多