- Top-N aggregation
- Retractions / corrections
- Push ingest
Summary
Suppose we have a game where players compete in matches against one another and get assigned an overall match score. In this example we will see:- How to create a dataset that represents a leaderboard of top-scoring players in the game using “Top-N” class of queries
- How such dataset tracks the entire history of changes
- And how “Top-N” queries manifest into a mechanism called retractions and corrections.
Steps
Getting Started
To follow this example checkoutkamu-cli repository and navigate into examples/leaderboard sub-directory.
Create a temporary kamu in that folder using:
Scores Dataset
We start with theplayer_scores dataset that will ingest the scores of all players who played a match. It will have a very simple schema:
match_time: Timestampmatch_id: Int64player_id: Stringscore: Int64
Leaderboard Dataset
Having all the historical scores we could already build a leaderboard using batch computations as simple as:leaderboard dataset to process all the data that is already in player-scores and check the results:
Introducing Retractions
Suppose we played another match with scores like these:leaderboard dataset:
op column:
- We still see the history of Alice and Bob being ranked 1 and 2 initially
- Then we see a “retraction” (
-R) record that says “Bob is no longer ranked 2nd” - And an “append” (
+A) record that says “Charlie is now ranked 2nd”
offset: 4) how both records are retracted and replaced by new values:
So, why not Batch?
So what do we gain from having a changelog stream with retractions? Why not run a batch query every once in a while to get the current leaderboard? The answer is: automation and infinite composability. As your game evolves, you might want to start rewarding people at top spots with prizes. You may also want to reset the leaderboard each month to make people prove themselves again. All this logic can be expressed as additional steps in the streaming data pipeline. If you write services that batch-query data at specific points in time and distribute rewards imagine what happens:- if some player is caught cheating and has to be retroactively stripped of its match scores (input retraction)
- if score ingestion pipeline goes down for a day and people who did in fact get top scores end up not accounted for during the reward distribution (backfill)
- if your game is very high-stakes and your have to prove during an audit how the rewards were issued (provenance).
If you haven’t already - make sure to check out the Stock Market Trading example that introduces the - another really important mechanism related to streams.