3

I have a dataframe similarly to:

+---+-----+-----+
|key|thing|value|
+---+-----+-----+
| u1|  foo|    1|
| u1|  foo|    2|
| u1|  bar|   10|
| u2|  foo|   10|
| u2|  foo|    2|
| u2|  bar|   10|
+---+-----+-----+

And want to get a result of:

+---+-----+---------+----+
|key|thing|sum_value|rank|
+---+-----+---------+----+
| u1|  bar|       10|   1|
| u1|  foo|        3|   2|
| u2|  foo|       12|   1|
| u2|  bar|       10|   2|
+---+-----+---------+----+

Currently, there is code similarly to:

val df = Seq(("u1", "foo", 1), ("u1", "foo", 2), ("u1", "bar", 10), ("u2", "foo", 10), ("u2", "foo", 2), ("u2", "bar", 10)).toDF("key", "thing", "value")

 // calculate sums per key and thing
 val aggregated = df.groupBy("key", "thing").agg(sum("value").alias("sum_value"))

 // get topk items per key
 val k = lit(10)
 val topk = aggregated.withColumn("rank", rank over  Window.partitionBy("key").orderBy(desc("sum_value"))).filter('rank < k)

However, this code is very inefficient. A window function generates a total order of items and causes a gigantic shuffle.

How can I calculate top-k items more efficiently? Maybe using approximate functions i.e. sketches similarly to https://datasketches.github.io/ or https://spark.apache.org/docs/latest/ml-frequent-pattern-mining.html

Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
  • It seems that because your shuffled files are created by two fields, and your window is using only one, it´s needed to perform two shuffles. Maybe grouping by key, and aggregate with a custom UDAF in the first grouping and cache can avoid the second shuffle in the window operation. – Emiliano Martinez May 23 '19 at 08:22

2 Answers2

5

This is a classical algorithm of recommender systems.

case class Rating(thing: String, value: Int) extends Ordered[Rating] {
  def compare(that: Rating): Int = -this.value.compare(that.value)
}

case class Recommendation(key: Int, ratings: Seq[Rating]) {
  def keep(n: Int) = this.copy(ratings = ratings.sorted.take(n))
}

val TOPK = 10

df.groupBy('key)
  .agg(collect_list(struct('thing, 'value)) as "ratings")
  .as[Recommendation]
  .map(_.keep(TOPK))

You can also check the source code at:

  • Spotify Big Data Rosetta Code / TopItemsPerUser.scala, several solutions here for Spark or Scio
  • Spark MLLib / TopByKeyAggregator.scala, considered the best practice when using their recommendation algorithm, it looks like their examples still uses RDD though.
import org.apache.spark.mllib.rdd.MLPairRDDFunctions._

sc.parallelize(Array(("u1", ("foo", 1)), ("u1", ("foo", 2)), ("u1", ("bar", 10)), ("u2", ("foo", 10)),
  ("u2", ("foo", 2)), ("u2", ("bar", 10))))
  .topByKey(10)(Ordering.by(_._2))

Michel Hua
  • 1,614
  • 2
  • 23
  • 44
0

RDD`s to the rescue

aggregated.as[(String, String, Long)].rdd.groupBy(_._1).map{ case (thing, it) => (thing, it.map(e=> (e._2, e._3)).toList.sortBy(sorter => sorter._2).take(1))}.toDF.show
+---+----------+
| _1|        _2|
+---+----------+
| u1| [[foo,3]]|
| u2|[[bar,10]]|
+---+----------+

This can most likely be improved using the suggestion from the comment. I.e. when not starting out from aggregated, but rather df. This could look similar to:

df.as[(String, String, Long)].rdd.groupBy(_._1).map{case (thing, it) => {
      val aggregatedInner = it.groupBy(e=> (e._2)).mapValues(events=> events.map(value => value._3).sum)
      val topk = aggregatedInner.toArray.sortBy(sorter=> sorter._2).take(1)
      (thing, topk)
    }}.toDF.show
Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
  • Better yet : use reduce by key (it should allow for map side reducing, diminishing yet again the amount of data shuffled at the groupByKey stage) ? – GPI May 23 '19 at 14:32
  • Can you send an answer? A quick try in replacing `.rdd.groupBy` with `.rdd.reduceByKey` failed. – Georg Heiler May 23 '19 at 14:34
  • But the top-k operation (`take(1)`) does not seem to be commutative. So I am not sure how to add `reduceByKey` – Georg Heiler May 23 '19 at 14:41
  • Sorry, I had not understood the question well enough. – GPI May 23 '19 at 15:21