Difference with the previous value. kslqDB

34 views
Skip to first unread message

Jose García

unread,
Jan 22, 2020, 2:57:58 AM1/22/20
to ksqldb-users


I'm trying to get this with ksqlDB but I don't know how to do it:



I was trying to make an example with ksqlDB

Based on two properties, in this case "sensor_id" and "user_id".

I calculated the variation of the "value" field, when both attributes match.

// TOPIC KAFKA

```
{"timestamp": 1000000000, "user_id": "AAAAA", "sensor_id": "sensor_1111", "value": 10000}
{"timestamp": 1555550000, "user_id": "AAAAA", "sensor_id": "sensor_1111", "value": 22000}

{"timestamp": 1666660000, "user_id": "AAAAA", "sensor_id": "sensor_2222", "value": 22000}
{"timestamp": 1777770000, "user_id": "AAAAA", "sensor_id": "sensor_2222", "value": 25000}

{"timestamp": 1666660000, "user_id": "BBBBB", "sensor_id": "sensor_2222", "value": 30000}
{"timestamp": 1777770000, "user_id": "BBBBB", "sensor_id": "sensor_2222", "value": 40000}

```

When that two values coincide, I calculated the difference with the previous one.

And the elapsed time.

// RESULT

```
{"timestamp": 1555550000, "last_timestamp": 1000000000, "user_id": "AAAAA", "sensor_id": "sensor_1111", "value": 12000}
{"timestamp": 1777770000, "last_timestamp": 1666660000, "user_id": "AAAAA", "sensor_id": "sensor_2222", "value": 3000}
{"timestamp": 1777770000, "last_timestamp": 1666660000, "user_id": "BBBBB", "sensor_id": "sensor_2222", "value": 10000}

```



Can someone help me or give me some advice to do it.

Thank you.

Vinoth Chandar

unread,
Feb 4, 2020, 2:45:48 PM2/4/20
to ksqldb-users
From the example, it seems like you want to obtain a stream of differences, for a given user_id/sensor_id combination? 

Thinking out loud, would a custom udaf help?  https://docs.ksqldb.io/en/latest/concepts/functions/#udafs 
Reply all
Reply to author
Forward
0 new messages