簡體   English   中英

如何訪問行並能夠使用scala在spark數據幀中來回移動

[英]how to access the rows and able to move back and forth in spark dataframes using scala

我正在嘗試解決以下問題:

輸入數據框:

+------+------+------------+
|emp   |Group |Spouse      |
+------+------+------------+
|John  |L1    |Dana        |
|Mary  |L1    |Rick        |
|Harry |L3    |Dana        |
|Linda |L3    |Harry       |
|Jim   |L3    |Mary        |
+------+------+------------+

邏輯:雇員的配偶不能與雇員屬於同一組(必須從列表中刪除那些配偶的行)

輸出數據幀:

+------+------+------------+
|Emp   |Group |Spouse      |
+------+------+------------+
|John  |L1    |Dana        |
|Mary  |L1    |Rick        |
|Linda |L3    |Harry       |
|Jim   |L3    |Mary        |
+------+------+------------+

有人可以幫我解決這個問題嗎?

它會是你的解決方案嗎?

df.createOrReplaceTempView("empl")

sql("select a.emp, a.Group, a.Spouse from empl a where exists (select 1 from empl b where a.Spouse = b.emp and a.Group <> b.Group)").show

根據配偶和組列將數據框左連接到自身,然后過濾加入的 DF 中的非空內容,如果不需要,則刪除加入的 DF。 像這樣的東西:

df.join(df.select($"emp".as("s_e"), $"Spouse".as("s_s"), $"Group".as("s_g")),
    $"emp" <=> $"s_s" && $"Group" <=> $"s_g", "left").
  filter($"s_e".isNull).
  drop($"s_e", $"s_s", $"s_g")

暫無
暫無

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

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