簡體   English   中英

Spark Java API中的數據集操作

[英]Dataset Manipulation in Spark Java API

我在下面有一個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   |

有人能幫助我嗎?

要將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"))

希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM