Skip to main content

Veli Kafka Client

Project description

Kafka Client for VELI.STORE

Description

This module is used for producing kafka events, subscribing to kafka topics and consuming kafka events.

To remember:

Each kafka topic event has a predefined structure which you can see in velikafkaclient/eventregistration, producer and consumer both have built in validation, meaning that if incorrect object structure is sent to a topic then producer will raise an exception as well as consumer.

How to use Producer/Consumer:

  • Initialize kafka producer:
BOOTSTRAP_SERVERS = "kafka bootstrap servers uri"
kafka_producer: AsyncKafkaEventProducer = AsyncKafkaEventProducer(BOOTSTRAP_SERVERS)
await kafka_producer.start()
  • Produce events to a topic:
from events.base import KafkaEvent
from topics.base import BaseTopic

kafka_event = KafkaEvent()
await kafka_producer.produce_event(BaseTopic, kafka_event)
  • Setup and start consumer
async def base_handler(kafka_event: KafkaEvent):
    print(str(kafka_event))
    
    
bootstrap_servers = BOOTSTRAP_SERVER
consumer = AsyncKafkaConsumer(bootstrap_servers, group_id=GROUP_ID)
consumer.subscribe(BaseTopic, base_handler)

await consumer.start()
await consumer.consume()

How to add topics:

To add a new topic you need three things:

  1. Topic itself
  2. Topic event structure
  3. Register Event structure to a Topic

All changes are done in velikafkaclient library (in the veli_libs repo)

  • To add a topic go to kafka-client/velikafkaclient/topics and add topic like this:
class SomeTopicCollection(KafkaTopic):
    
    USER_REGISTRATIONS = 'user_registrations'
  • To add an event structure go to kafka-client/velikafkaclient/events and create pydantic model for event structure:
class UserRegistrationEvent(KafkaEvent):
    
    id: int
    username: str
    password: str
    username: str
  • To register event structure to a topic go to kafka-client/velikafkaclient/eventregistration.py and add:
kafka_topic_events.register_topic_event_model(KafkaTopic.USER_REGISTRATIONS, UserRegistrationEvent)

Once done follow the instructions in the veli_libs readme to update the library on pyip

How to manage topics with TopicManager:

from velikafkaclient.topicmanager import KafkaTopicManager

client = KafkaTopicManager(BOOTSTRAP_SERVERS)


# Create Topic
client.create_topic('topic_name')


# Create Multiple Topics
topics = ['topic_name_1', 'topic_name_2']
client.create_topics(topics)


# List Topics
print(client.list_topics())


# Delete Topic
client.delete_topic('topic_name')


# Delete Multiple Topics
topics = ['topic_name_1', 'topic_name_2']
client.delete_topics(topics)


# Check if topic exists
print(client.topic_exists('topic_name'))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

velikafkaclient-1.3.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

velikafkaclient-1.3.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file velikafkaclient-1.3.0.tar.gz.

File metadata

  • Download URL: velikafkaclient-1.3.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for velikafkaclient-1.3.0.tar.gz
Algorithm Hash digest
SHA256 dd0d6c072a57e5bbab21d0c85d3e33023a67c257d2f89fbffa747770354935e3
MD5 b6a979a412fee5e94772fb2800ea64fd
BLAKE2b-256 a3ed42c5ca012b96f19c2617d16cb6caa1a03fb46b72f774e8dea18ff8d84bae

See more details on using hashes here.

File details

Details for the file velikafkaclient-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for velikafkaclient-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad42be43a6ffc3fe9b930e4e1e0b7285088f3aeee488f15a1349c4899f53c888
MD5 a4b6d277fa83bcaccc5330d09c5673df
BLAKE2b-256 a3889b8e1a6e1e59b215c6d7ab17955a7023e6288431f4071f1170f99ff8b1a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page