Git Product home page Git Product logo

samsa's Introduction

samsa ๐Ÿž

A Python implementation of the state stores concept within the Kafka Streams API

Whats in a name?

Gregor Samsa was the unfortunate soul in the Kafka classic 'The Metamorphosis (1915)' who woke up one day as a giant insect.

Storage options

This uses SQLite by default but also supports RocksDB by specifying store='rocksdb' in the PersistentConsumer init.

Installing the RocksDB client

brew install cmake if you haven't got it already

git clone https://github.com/twmht/python-rocksdb.git --recursive -b pybind11 python setup.py install inside the python-rocksdb directory.

You can run rocksdb locally on OSX with brew install rocksdb. It's not a DB in the traditional sense, but really just a super fast way to store stuff on disk (as fast as that really is!).

Example

The following is a pretty simple way of pulling data from kafka, and saving it to a local store. The default is sqlite but rocksdb is also possible. The example below:

  1. Initialises a consumer with a table called 'node_status'
  2. Queries the local store for the received key.
  3. Compares the received value with a saved value in the local store.
  4. It then saves the message to the store for future computation.
from samsa import PersistentConsumer

def loop():
    topic = "nodes.status"
    with PersistentConsumer(
            [topic], table_name="node_status", group_id=KAFKA_GROUP_ID,
            bootstrap_servers="localhost:9092"
    ) as store:
        while True:
            msg = store.consume(process_callback)
            if msg:
                key = msg.key().decode("utf-8")
                value = json.loads(msg.value().decode("utf-8"))["data"]["status"]
                
                status = store.query(key)
                if status != value:
                    print(f"Status has changed from {status} to {value} for {key}!")
                else:
                    print("No status change, nothing to see here!")
                store.save(key, value)
                
if __name__ == "__main__":
    loop()                

samsa's People

Contributors

tombasche avatar

Watchers

 avatar

Forkers

echelon9

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.