Skip to main content

Convert Kafka client configs between confluent-kafka and aiokafka

Project description

kafkaesq

A library for parsing Kafka configurations for all Python libraries.

Kafka client libraries in Python don't agree on how a config should be spelled. confluent-kafka takes librdkafka-style dicts with dotted keys and loosely typed values, while aiokafka and kafka-python take snake_case constructor kwargs with native Python types. kafkaesq converts between them, in both directions, so one config can drive all of these clients.

pip install kafkaesq

No runtime dependencies — you don't need either Kafka library installed to convert configs.

Usage

confluent-kafka → aiokafka

from kafkaesq import confluent_to_aiokafka

conf = {
    "bootstrap.servers": "localhost:9092",
    "group.id": "billing",
    "security.protocol": "sasl_ssl",
    "sasl.mechanism": "scram-sha-256",
    "sasl.username": "user",
    "sasl.password": "secret",
    "enable.auto.commit": "false",
    "auto.offset.reset": "smallest",   # librdkafka alias
}

kwargs = confluent_to_aiokafka(conf)
# {'bootstrap_servers': 'localhost:9092', 'group_id': 'billing',
#  'security_protocol': 'SASL_SSL', 'sasl_mechanism': 'SCRAM-SHA-256',
#  'sasl_plain_username': 'user', 'sasl_plain_password': 'secret',
#  'enable_auto_commit': False, 'auto_offset_reset': 'earliest'}

from aiokafka import AIOKafkaConsumer
consumer = AIOKafkaConsumer("payments", **kwargs)

Values are normalized along the way: string booleans and integers become real bool/int, librdkafka offset-reset aliases (smallest, largest, ...) are translated, acks=-1 becomes "all", compression.type="none" becomes None, and librdkafka ssl.* file options are folded into a ready-made ssl_context.

aiokafka → confluent-kafka

from kafkaesq import aiokafka_to_confluent

conf = aiokafka_to_confluent(
    bootstrap_servers=["a:9092", "b:9092"],
    group_id="billing",
    enable_auto_commit=False,
    compression_type=None,
)
# {'bootstrap.servers': 'a:9092,b:9092', 'group.id': 'billing',
#  'enable.auto.commit': False, 'compression.type': 'none'}

from confluent_kafka import Consumer
consumer = Consumer(conf)

A plain dict of kwargs works too: aiokafka_to_confluent(kwargs_dict).

confluent-kafka ↔ kafka-python

confluent_to_kafka_python and kafka_python_to_confluent work the same way. kafka-python shares aiokafka's kwarg names for everything in the mapping table, but takes SSL file paths directly, so librdkafka ssl.* options map one-to-one instead of folding into an ssl_context:

from kafkaesq import confluent_to_kafka_python

kwargs = confluent_to_kafka_python({
    "bootstrap.servers": "localhost:9092",
    "security.protocol": "SSL",
    "ssl.ca.location": "/etc/ssl/ca.pem",
    "ssl.certificate.location": "/etc/ssl/client.pem",
    "ssl.key.location": "/etc/ssl/client.key",
})
# {'bootstrap_servers': 'localhost:9092', 'security_protocol': 'SSL',
#  'ssl_cafile': '/etc/ssl/ca.pem', 'ssl_certfile': '/etc/ssl/client.pem',
#  'ssl_keyfile': '/etc/ssl/client.key'}

from kafka import KafkaConsumer
consumer = KafkaConsumer("payments", **kwargs)

Keys that can't be converted

Some options only exist on one side (callbacks like error_cb, librdkafka internals like statistics.interval.ms, aiokafka's value_deserializer, ...). By default these are dropped with a KafkaesqWarning; choose stricter or quieter behavior with on_unmapped:

confluent_to_aiokafka(conf, on_unmapped="raise")   # UnmappedConfigError
confluent_to_aiokafka(conf, on_unmapped="ignore")  # drop silently

Development

pip install -e .[dev]
pytest

The suite includes integration tests that feed converted configs to the real confluent_kafka, aiokafka, and kafka-python client constructors (no broker needed — all three validate option names at construction time). They skip automatically unless the clients are installed:

pip install -e .[dev,integration]
pytest

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

kafkaesq-0.1.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

kafkaesq-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file kafkaesq-0.1.0.tar.gz.

File metadata

  • Download URL: kafkaesq-0.1.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kafkaesq-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6d166e3f6c2db36676d6071919049fba1f96d672057a9c7a3a2a0eb496d51cbc
MD5 b9b2165a13a13a45828af9efdb702116
BLAKE2b-256 56995d977bb7647ca026eee251fb257febdf1259f3ca9eaad274516fb9b1e3df

See more details on using hashes here.

Provenance

The following attestation bundles were made for kafkaesq-0.1.0.tar.gz:

Publisher: publish.yml on wbarnha/kafkaesq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kafkaesq-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: kafkaesq-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kafkaesq-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0951be93c98523e764d9deff7ecbd45c16ae1100098056e467874a657a27bc8
MD5 f1896296f9d62f7740454778671eeca9
BLAKE2b-256 c82e6f898dcc3f55e5fe41bde7c27903be03dd931f31bc2c66a13b8318eb7afd

See more details on using hashes here.

Provenance

The following attestation bundles were made for kafkaesq-0.1.0-py3-none-any.whl:

Publisher: publish.yml on wbarnha/kafkaesq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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