Skip to main content

kafkaproxy

kafka生产者和消费者的代理工具.

代理对象用于推迟初始化.我们可以在需要的地方用代理对象的全局变量直接编写逻辑,避免被代理的对象来回在函数间传递.

特性

  • 支持代理kafka-python,aiokafka和confluent-kafka的生产者消费者对象.
  • 提供统一通用的生产者消费者接口包装

安装

  • 只安装本项目不安装被代理对象的依赖: pip install kafkaproxy
  • 安装本项目同时确定要代理的对象包为kafka-python: pip install kafkaproxy[kafka]
  • 安装本项目同时确定要代理的对象包为aiokafka: pip install kafkaproxy[aio]
  • 安装本项目同时确定要代理的对象包为confluent-kafka: pip install kafkaproxy[confluent]

使用

本项目支持代理3种kafka模块中的对应模块,使用枚举KafkaType中的取值在调用方法initialize_from_addresses初始化时指定. 代理对象除了原样代理对象外还提供了生产者和消费者的统一通用接口包装. 由于对应的方法是动态绑定的,因此如果需要他们的typehints可以用typing.cast将代理对象转化为对应的协议对象

  • 同步接口生产者使用ProducerProtocol
  • 异步接口生产者使用AioProducerProtocol
  • 同步接消费产者使用ConsumerProtocol
  • 异步接消费产者使用AioConsumerProtocol

代理kafka-python或confluent-kafka生产者

from kafkaproxy import ProducerProxy, KafkaType, ProducerProtocol
from typing import cast
import time
kafkap = ProducerProxy()


def run() -> None:
    p = cast(ProducerProtocol, kafkap)
    with p.mount() as cli:
        for i in range(10):
            cli.publish("topic1", f"send {i}")
            time.sleep(0.1)


# kafkap.initialize_from_addresses("localhost:9094", kafka_type=KafkaType.ConfluentKafka, acks="all")
kafkap.initialize_from_addresses("localhost:9094", kafka_type=KafkaType.Kafka)
try:
    print("start publishing")
    run()
finally:
    print("stoped")

代理kafka-python或confluent-kafka消费者

from kafkaproxy import ConsumerProxy, KafkaType, ConsumerProtocol
from typing import cast

kafkac = ConsumerProxy()


def run() -> None:
    c = cast(ConsumerProtocol, kafkac)
    with c.watch() as g:
        for record in g:
            print(record.value)


# kafkac.initialize_from_addresses("localhost:9094", "topic1", group_id="test2", kafka_type=KafkaType.Kafka)
kafkac.initialize_from_addresses("localhost:9094", "topic1", group_id="test2", kafka_type=KafkaType.ConfluentKafka)
try:
    print("start watching")
    run()
finally:
    print("stoped")

代理aiokafka生产者

import asyncio
from kafkaproxy import ProducerProxy, KafkaType, AioProducerProtocol
from typing import cast

kafkap = ProducerProxy()


async def run() -> None:
    p = cast(AioProducerProtocol, kafkap)
    async with p.mount() as cli:
        for i in range(10):
            await cli.publish("topic1", f"send {i}")
            await asyncio.sleep(0.1)


async def main() -> None:
    kafkap.initialize_from_addresses("localhost:9094", kafka_type=KafkaType.AioKafka, acks="all")
    await run()


try:
    print("start watching")
    asyncio.run(main())
finally:
    print("stoped")

代理aiokafka消费者

import asyncio
from kafkaproxy import ConsumerProxy, KafkaAutoOffsetReset, KafkaType, AioConsumerProtocol
from typing import cast

kafkac = ConsumerProxy()


async def run() -> None:
    c = cast(AioConsumerProtocol, kafkac)
    async with c.watch() as g:
        async for record in g:
            print(record.value)


async def main() -> None:
    kafkac.initialize_from_addresses("localhost:9094", "topic1", group_id="test2", kafka_type=KafkaType.AioKafka, auto_offset_reset=KafkaAutoOffsetReset.earliest)
    await run()


try:
    print("start watching")
    asyncio.run(main())
finally:
    print("stoped")

v0.0.1

项目创建 MIT License

Copyright (c) 2023 Python-Tools

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for kafkaproxy 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kafkaproxy 0.0.1
File Size Uploaded Provenance Actions
kafkaproxy-0.0.1.tar.gz 9.9 kB Without provenance Details for kafkaproxy-0.0.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for kafkaproxy 0.0.1
File Interpreter ABI Platform Actions
kafkaproxy-0.0.1-py3.11.egg Legacy Egg format - - Details for kafkaproxy-0.0.1-py3.11.egg
kafkaproxy-0.0.1-py3-none-any.whl Python 3 none any Details for kafkaproxy-0.0.1-py3-none-any.whl

Total release size: 31.2 kB

Release files / kafkaproxy-0.0.1.tar.gz

Download URL kafkaproxy-0.0.1.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b9cc08ac68d8c69c97e4e21daa80f8c8d32e970d67acc2f3b7529f71fbd5c04f
BLAKE2b-256 checksum
How to use checksums
464468385244bf893607cde666994d41a72ba4ecd50ab62e5048a1e0fcad1ecf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / kafkaproxy-0.0.1-py3.11.egg

Download URL kafkaproxy-0.0.1-py3.11.egg
Size 10.2 kB
Tags Egg
SHA-256 checksum
How to use checksums
de6e166f0121676126fc293347e7a1bd637b943a4d7baf9c3f511d5ab75d954a
BLAKE2b-256 checksum
How to use checksums
9c1498291da991a8d474e9f528bf251fb855b4ceac412a4b90305e74a8bbc73d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / kafkaproxy-0.0.1-py3-none-any.whl

Download URL kafkaproxy-0.0.1-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b58e39ec053794c580a26bd5a51247e594508bfef527ac09b010f211e3075ae5
BLAKE2b-256 checksum
How to use checksums
fad55f07224bbe91b57227739c3781f83ffe1ab2dbacffb45a9a563e92162da3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Security for kafkaproxy 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

3 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page