我有以下数据框
df
:
+-------------------+--------+--------------------+
| id| name| type| url|
+-------------------+--------+--------------------+
| 1| NT Note| aaaa| null|
| 1| NT Note| aaaa|http:
| 1| NT Note| aaaa|http:
| 1| NT Note| aaaa| null|
| 1| NT Note| aaaa| null|
| 2| ABC| bbbb| null|
| 2| ABC| bbbb| null|
| 2| ABC| bbbb| null|
| 2| ABC| bbbb| null|
+-------------------+--------+--------------------+
我指派最频繁的
url
和
type
每个节点的值:
def windowSpec = Window.partitionBy("id", "url", "type")
val result = df.withColumn("count", count("url").over(windowSpec))
.orderBy($"count".desc)
.groupBy("id")
.agg(
first("url").as("URL"),
first("type").as("Typel")
)
但事实上我需要
网址
+-------------------+--------+--------------------+
| id| name| type| url|
+-------------------+--------+--------------------+
| 1| NT Note| aaaa|http://www.teleab...|
| 2| ABC| bbbb| null|
+-------------------+--------+--------------------+
现在我得到下面显示的输出,因为
null
对于记录id更频繁
1
+-------------------+--------+--------------------+
| id| name| type| url|
+-------------------+--------+--------------------+
| 1| NT Note| aaaa| null|
| 2| ABC| bbbb| null|
+-------------------+--------+--------------------+