![](/img/trans.png)
[英]How to add a column to the existing DataFrame and using window function to add specific rows in the new column using Scala/Spark 2.2
[英]How add new column based on existing column in spark scala
光环
我已经完成了在Apache Spark中使用Mllib ALS建立推荐并输出的建议
user | product | rating
1 | 20 | 0.002
1 | 30 | 0.001
1 | 10 | 0.003
2 | 20 | 0.002
2 | 30 | 0.001
2 | 10 | 0.003
但是我需要根据评分来更改数据结构,像这样:
user | product | rating | number_rangking
1 | 10 | 0.003 | 1
1 | 20 | 0.002 | 2
1 | 30 | 0.001 | 3
2 | 10 | 0.002 | 1
2 | 20 | 0.001 | 2
2 | 30 | 0.003 | 3
我怎样才能做到这一点? 也许任何人都可以给我一个线索...
谢谢
所有你需要的是一个窗口的功能取决于细节,你选择使用rank
或rowNumber
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions.rank
val w = Window.partitionBy($"user").orderBy($"rating".desc)
df.select($"*", rank.over(w).alias("number_rangking")).show
// +----+-------+------+---------------+
// |user|product|rating|number_rangking|
// +----+-------+------+---------------+
// | 1| 10| 0.003| 1|
// | 1| 20| 0.002| 2|
// | 1| 30| 0.001| 3|
// | 2| 10| 0.003| 1|
// | 2| 20| 0.002| 2|
// | 2| 30| 0.001| 3|
// +----+-------+------+---------------+
使用普通的RDD,您可以groupByKey
,本地处理和flatMap
:
rdd
// Convert to PairRDD
.map{case (user, product, rating) => (user, (product, rating))}
.groupByKey
.flatMap{case (user, vals) => vals.toArray
.sortBy(-_._2) // Sort by rating
.zipWithIndex // Add index
// Yield final values
.map{case ((product, rating), idx) => (user, product, rating, idx + 1)}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.