I have a dataframe that contains an "id" column and a "publication" column. The "id" column contains duplicates, and represents a researcher. The "publication" column contains some information about an academic work the researcher published.
I want to transform this dataframe to collect the publications into an array, reducing the number of rows. I can do this using groupBy and collect_list. This would make it so that the "id" column would only contain unique values.
myDataframe
.groupBy("id")
.agg(
collect_list("publication").as("publications")
).select("id", "publications")
However, for my purposes, this is too much data for one row. I want to limit the number of publications that are collected, and split the data up across multiple rows.
Let's my dataframe looks like this, where id of 1 appears in 10 rows:
| id | publication |
| ----| -------------- |
| 1 | "foobar" |
| 1 | "foobar" |
| 1 | "foobar" |
| 1 | "foobar" |
| 1 | "foobar" |
| 1 | "foobar" |
| 2 | "foobar" |
| 1 | "foobar" |
| 1 | "foobar" |
| 1 | "foobar" |
| 1 | "foobar" |
I want to groupBy id and collect publication into a list, but limit this to a maximum of 5 publications per group:
| id | publication |
| ----| -------------- |
| 1 | ["foobar",...] |
| 1 | ["foobar",...] |
| 2 | ["foobar"] |
How would I accomplish this in spark scala?