Time between events in a multi-topic event stream

We are processing a stream of events on different topics. We want to compute, for each event, how much time has passed since the previous event on the same topic. The issue is that the events do not appear in the order of their timestamps at input! With Pathway there is no need to worry about that!

First we use Debezium to synchronize the input data from a database table with the following columns:

  • timestamp - logical date-time when the event happened
  • topic_id - topic in question
  • message - message content.
import pathway as pw

events =
        "": "$GROUP_NAME",
        "bootstrap.servers": "",
        "": "6000",

Then we build a sorted index of the events from the table. Pathway provides a build_sorted_index to sort a table according to its key column: in this case we are going to sort according to the timestamps of the events. In addition, each topic is mapped to an instance field, which allows us to work on different streams simultaneously. We immediately use the index to sort the table (that is, extract prev and next pointers).

sorted_events = pw.indexing.sort_from_index(
        events +, instance=events.topic_id)

Finally, we process events in order of their timestamps at input.

differences = events.having(sorted_events.prev).select(
    delta=events.timestamp - pw.this.timestamp
        "host": "localhost",
        "port": "5432",
        "dbname": "transactions",
        "user": "pathway",
        "password": "my_password",

Przemek Uznański

Data Structures and Optimization Expert