考慮一個包含以上兩列的檔案,并且在商店中有包含不同產品的產品列,我只需要回傳僅在一個商店中的唯一產品并回傳商店名稱。我嘗試了以下方法,但正在尋找有效的解決方案。提前致謝。
store products
walmart eggs,cereals,milk
target toys,eggs,cereals
costco eggs,cereals,milk
val df1 = dataDF.select("prods").agg(collect_list("prods")).collect.toArray
df1(0).getSeq[String](0).toList.map(x => x.split(",")).flatten.groupBy((word: String) => word).mapValues(_.length).filter(x=> x._2==1 ).keys.head
=> this returns toys, then filter that respective store from df. But it doesn't seem efficient .
預期輸出
目標玩具
uj5u.com熱心網友回復:
你可以試試這個:
dataDf
.withColumn("products", split($"products", ",")) // Parse as array
.withColumn("product", explode($"products")) // Explode into rows
.groupBy($"product")
.agg(collect_list($"store").as("stores")) // Get list of stores as array
.filter(size($"stores") === 1) // Where there's only one store selling
.show
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/469538.html
