我有一个sparkscalaDataframe,它有四个值:(id,day,val,order)。我想创建一个新的dataframe,包含以下列:(id,day,value_list:list(val1,val2,…,valn)),其中val1到valn按asc order value排序。
例如:
(50, 113, 1, 1),
(50, 113, 1, 3),
(50, 113, 2, 2),
(51, 114, 1, 2),
(51, 114, 2, 1),
(51, 113, 1, 1)
将变成:
((51,113),List(1))
((51,114),List(2, 1)
((50,113),List(1, 2, 1))
我很接近,但不知道在我把数据汇总成一个列表后该怎么办。我不知道如何按int的顺序排列每个值列表:
import org.apache.spark.sql.Row
val testList = List((50, 113, 1, 1), (50, 113, 1, 3), (50, 113, 2, 2), (51, 114, 1, 2), (51, 114, 2, 1), (51, 113, 1, 1))
val testDF = sqlContext.sparkContext.parallelize(testList).toDF("id1", "id2", "val", "order")
val rDD1 = testDF.map{case Row(key1: Int, key2: Int, val1: Int, val2: Int) => ((key1, key2), List((val1, val2)))}
val rDD2 = rDD1.reduceByKey{case (x, y) => x ++ y}
输出如下所示:
((51,113),List((1,1)))
((51,114),List((1,2), (2,1)))
((50,113),List((1,3), (1,1), (2,2)))
下一步是生产:
((51,113),List((1,1)))
((51,114),List((2,1), (1,2)))
((50,113),List((1,1), (2,2), (1,3)))
2条答案
按热度按时间n53p2ov01#
你只需要在Map上标出你的
RDD
使用sortBy
:d8tt03nd2#