Ravi
Ravi

Reputation: 667

How to expand a time range into per-minute intervals in Spark (Scala or Python)?

I have a dataset that has the following structure.

+-------+----------+---------------+---------------+
| tv_id | movie_id |  start_time   |   end_time    |
+-------+----------+---------------+---------------+
| tv123 | movie123 | 02/05/19 3:05 | 02/05/19 3:08 |
| tv234 | movie345 | 02/05/19 3:07 | 02/05/19 3:10 |
+-------+----------+---------------+---------------+

The output that I am trying to get is as below:

+-------+----------+---------------+
| tv_id | movie_id |    minute     |
+-------+----------+---------------+
| tv123 | movie123 | 02/05/19 3:05 |
| tv123 | movie123 | 02/05/19 3:06 |
| tv123 | movie123 | 02/05/19 3:07 |
| tv234 | movie345 | 02/05/19 3:07 |
| tv234 | movie345 | 02/05/19 3:08 |
| tv234 | movie345 | 02/05/19 3:09 |
+-------+----------+---------------+

Detailed Explanation: for tv_id: tv123, the total watch time is 3 minutes (3:08 - 3: 05) same goes for other records as well.

I am trying to use either python / Scala / or SQL to get the result. [ No restriction on the language used] My python code:

df = read_csv('data')
df[minutes_diff] = df['end_time'] - df['start_time']

for i in range(df['minutes_diff']):
    finaldf = df[tv_id] + df[movie_id] + df['start_time'] + df[minutes_diff] + "i"

I am not sure how I can go about it. I am not well versed with Scala flatmap. Some research on StackOverflow pointed to use flatmap, but I am not sure how can I use diff in flatmap inplace of aggregation.

Note: I dont want to open separate thread for SQL and Python, hence combining all of these in the same question. Even a sql solution will be perfectly good for me.

Upvotes: 0

Views: 416

Answers (1)

Leo C
Leo C

Reputation: 22449

Here's a Scala-based solution using a UDF that expands a time range via the java.time API into a per-minute list, which then gets flattened with Spark's built-in explode method:

import org.apache.spark.sql.functions._

val df = Seq(
  ("tv123", "movie123", "02/05/19 3:05", "02/05/19 3:08"),
  ("tv234", "movie345", "02/05/19 3:07", "02/05/19 3:10")
).toDF("tv_id", "movie_id", "start_time", "end_time")

def minuteList(timePattern: String) = udf{ (timeS1: String, timeS2: String) =>
  import java.time.LocalDateTime
  import java.time.format.DateTimeFormatter

  val timeFormat = DateTimeFormatter.ofPattern(timePattern)
  val t1 = LocalDateTime.parse(timeS1, timeFormat)
  val t2 = LocalDateTime.parse(timeS2, timeFormat)

  Iterator.iterate(t1)(_.plusMinutes(1)).takeWhile(_ isBefore t2).
    map(_.format(timeFormat)).
    toList
}

df.
  withColumn("minute_list", minuteList("MM/dd/yy H:mm")($"start_time", $"end_time")).
  withColumn("minute", explode($"minute_list")).
  select("tv_id", "movie_id", "minute").
  show(false)
// +-----+--------+-------------+
// |tv_id|movie_id|minute       |
// +-----+--------+-------------+
// |tv123|movie123|02/05/19 3:05|
// |tv123|movie123|02/05/19 3:06|
// |tv123|movie123|02/05/19 3:07|
// |tv234|movie345|02/05/19 3:07|
// |tv234|movie345|02/05/19 3:08|
// |tv234|movie345|02/05/19 3:09|
// +-----+--------+-------------+

Upvotes: 2

Related Questions