kafkaproxy


Namekafkaproxy JSON
Version 0.0.1 PyPI version JSON
download
home_page
Summarykafka consumer and producer proxy
upload_time2023-02-11 08:52:52
maintainer
docs_urlNone
authorhsz
requires_python
licenseMIT License
keywords kafka proxy consumer producer
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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`生产者

```python
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`消费者

```python
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`生产者

```python
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`消费者

```python
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.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "kafkaproxy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "kafka,proxy,consumer,producer",
    "author": "hsz",
    "author_email": "hsz1273327@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/46/44/68385244bf893607cde666994d41a72ba4ecd50ab62e5048a1e0fcad1ecf/kafkaproxy-0.0.1.tar.gz",
    "platform": null,
    "description": "# kafkaproxy\n\nkafka\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7684\u4ee3\u7406\u5de5\u5177.\n\n\u4ee3\u7406\u5bf9\u8c61\u7528\u4e8e\u63a8\u8fdf\u521d\u59cb\u5316.\u6211\u4eec\u53ef\u4ee5\u5728\u9700\u8981\u7684\u5730\u65b9\u7528\u4ee3\u7406\u5bf9\u8c61\u7684\u5168\u5c40\u53d8\u91cf\u76f4\u63a5\u7f16\u5199\u903b\u8f91,\u907f\u514d\u88ab\u4ee3\u7406\u7684\u5bf9\u8c61\u6765\u56de\u5728\u51fd\u6570\u95f4\u4f20\u9012.\n\n## \u7279\u6027\n\n+ \u652f\u6301\u4ee3\u7406`kafka-python`,`aiokafka`\u548c`confluent-kafka`\u7684\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u5bf9\u8c61.\n+ \u63d0\u4f9b\u7edf\u4e00\u901a\u7528\u7684\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u63a5\u53e3\u5305\u88c5\n\n## \u5b89\u88c5\n\n+ \u53ea\u5b89\u88c5\u672c\u9879\u76ee\u4e0d\u5b89\u88c5\u88ab\u4ee3\u7406\u5bf9\u8c61\u7684\u4f9d\u8d56: `pip install kafkaproxy`\n+ \u5b89\u88c5\u672c\u9879\u76ee\u540c\u65f6\u786e\u5b9a\u8981\u4ee3\u7406\u7684\u5bf9\u8c61\u5305\u4e3a`kafka-python`: `pip install kafkaproxy[kafka]`\n+ \u5b89\u88c5\u672c\u9879\u76ee\u540c\u65f6\u786e\u5b9a\u8981\u4ee3\u7406\u7684\u5bf9\u8c61\u5305\u4e3a`aiokafka`: `pip install kafkaproxy[aio]`\n+ \u5b89\u88c5\u672c\u9879\u76ee\u540c\u65f6\u786e\u5b9a\u8981\u4ee3\u7406\u7684\u5bf9\u8c61\u5305\u4e3a`confluent-kafka`: `pip install kafkaproxy[confluent]`\n\n## \u4f7f\u7528\n\n\u672c\u9879\u76ee\u652f\u6301\u4ee3\u74063\u79cdkafka\u6a21\u5757\u4e2d\u7684\u5bf9\u5e94\u6a21\u5757,\u4f7f\u7528\u679a\u4e3e`KafkaType`\u4e2d\u7684\u53d6\u503c\u5728\u8c03\u7528\u65b9\u6cd5`initialize_from_addresses`\u521d\u59cb\u5316\u65f6\u6307\u5b9a.\n\u4ee3\u7406\u5bf9\u8c61\u9664\u4e86\u539f\u6837\u4ee3\u7406\u5bf9\u8c61\u5916\u8fd8\u63d0\u4f9b\u4e86\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7684\u7edf\u4e00\u901a\u7528\u63a5\u53e3\u5305\u88c5.\n\u7531\u4e8e\u5bf9\u5e94\u7684\u65b9\u6cd5\u662f\u52a8\u6001\u7ed1\u5b9a\u7684,\u56e0\u6b64\u5982\u679c\u9700\u8981\u4ed6\u4eec\u7684typehints\u53ef\u4ee5\u7528`typing.cast`\u5c06\u4ee3\u7406\u5bf9\u8c61\u8f6c\u5316\u4e3a\u5bf9\u5e94\u7684\u534f\u8bae\u5bf9\u8c61\n\n+ \u540c\u6b65\u63a5\u53e3\u751f\u4ea7\u8005\u4f7f\u7528`ProducerProtocol`\n+ \u5f02\u6b65\u63a5\u53e3\u751f\u4ea7\u8005\u4f7f\u7528`AioProducerProtocol`\n+ \u540c\u6b65\u63a5\u6d88\u8d39\u4ea7\u8005\u4f7f\u7528`ConsumerProtocol`\n+ \u5f02\u6b65\u63a5\u6d88\u8d39\u4ea7\u8005\u4f7f\u7528`AioConsumerProtocol`\n\n> \u4ee3\u7406`kafka-python`\u6216`confluent-kafka`\u751f\u4ea7\u8005\n\n```python\nfrom kafkaproxy import ProducerProxy, KafkaType, ProducerProtocol\nfrom typing import cast\nimport time\nkafkap = ProducerProxy()\n\n\ndef run() -> None:\n    p = cast(ProducerProtocol, kafkap)\n    with p.mount() as cli:\n        for i in range(10):\n            cli.publish(\"topic1\", f\"send {i}\")\n            time.sleep(0.1)\n\n\n# kafkap.initialize_from_addresses(\"localhost:9094\", kafka_type=KafkaType.ConfluentKafka, acks=\"all\")\nkafkap.initialize_from_addresses(\"localhost:9094\", kafka_type=KafkaType.Kafka)\ntry:\n    print(\"start publishing\")\n    run()\nfinally:\n    print(\"stoped\")\n```\n\n> \u4ee3\u7406`kafka-python`\u6216`confluent-kafka`\u6d88\u8d39\u8005\n\n```python\nfrom kafkaproxy import ConsumerProxy, KafkaType, ConsumerProtocol\nfrom typing import cast\n\nkafkac = ConsumerProxy()\n\n\ndef run() -> None:\n    c = cast(ConsumerProtocol, kafkac)\n    with c.watch() as g:\n        for record in g:\n            print(record.value)\n\n\n# kafkac.initialize_from_addresses(\"localhost:9094\", \"topic1\", group_id=\"test2\", kafka_type=KafkaType.Kafka)\nkafkac.initialize_from_addresses(\"localhost:9094\", \"topic1\", group_id=\"test2\", kafka_type=KafkaType.ConfluentKafka)\ntry:\n    print(\"start watching\")\n    run()\nfinally:\n    print(\"stoped\")\n\n```\n\n> \u4ee3\u7406`aiokafka`\u751f\u4ea7\u8005\n\n```python\nimport asyncio\nfrom kafkaproxy import ProducerProxy, KafkaType, AioProducerProtocol\nfrom typing import cast\n\nkafkap = ProducerProxy()\n\n\nasync def run() -> None:\n    p = cast(AioProducerProtocol, kafkap)\n    async with p.mount() as cli:\n        for i in range(10):\n            await cli.publish(\"topic1\", f\"send {i}\")\n            await asyncio.sleep(0.1)\n\n\nasync def main() -> None:\n    kafkap.initialize_from_addresses(\"localhost:9094\", kafka_type=KafkaType.AioKafka, acks=\"all\")\n    await run()\n\n\ntry:\n    print(\"start watching\")\n    asyncio.run(main())\nfinally:\n    print(\"stoped\")\n\n```\n\n> \u4ee3\u7406`aiokafka`\u6d88\u8d39\u8005\n\n```python\nimport asyncio\nfrom kafkaproxy import ConsumerProxy, KafkaAutoOffsetReset, KafkaType, AioConsumerProtocol\nfrom typing import cast\n\nkafkac = ConsumerProxy()\n\n\nasync def run() -> None:\n    c = cast(AioConsumerProtocol, kafkac)\n    async with c.watch() as g:\n        async for record in g:\n            print(record.value)\n\n\nasync def main() -> None:\n    kafkac.initialize_from_addresses(\"localhost:9094\", \"topic1\", group_id=\"test2\", kafka_type=KafkaType.AioKafka, auto_offset_reset=KafkaAutoOffsetReset.earliest)\n    await run()\n\n\ntry:\n    print(\"start watching\")\n    asyncio.run(main())\nfinally:\n    print(\"stoped\")\n\n```\n# v0.0.1\n\n\u9879\u76ee\u521b\u5efa\nMIT License\n\nCopyright (c) 2023 Python-Tools\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "kafka consumer and producer proxy",
    "version": "0.0.1",
    "split_keywords": [
        "kafka",
        "proxy",
        "consumer",
        "producer"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9c1498291da991a8d474e9f528bf251fb855b4ceac412a4b90305e74a8bbc73d",
                "md5": "74488144fe331bbf3ac8117a3c330633",
                "sha256": "de6e166f0121676126fc293347e7a1bd637b943a4d7baf9c3f511d5ab75d954a"
            },
            "downloads": -1,
            "filename": "kafkaproxy-0.0.1-py3.11.egg",
            "has_sig": false,
            "md5_digest": "74488144fe331bbf3ac8117a3c330633",
            "packagetype": "bdist_egg",
            "python_version": "0.0.1",
            "requires_python": null,
            "size": 10162,
            "upload_time": "2023-02-11T08:52:51",
            "upload_time_iso_8601": "2023-02-11T08:52:51.612670Z",
            "url": "https://files.pythonhosted.org/packages/9c/14/98291da991a8d474e9f528bf251fb855b4ceac412a4b90305e74a8bbc73d/kafkaproxy-0.0.1-py3.11.egg",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fad55f07224bbe91b57227739c3781f83ffe1ab2dbacffb45a9a563e92162da3",
                "md5": "ef48eaf243c9f0c8f663e17c10bb63a6",
                "sha256": "b58e39ec053794c580a26bd5a51247e594508bfef527ac09b010f211e3075ae5"
            },
            "downloads": -1,
            "filename": "kafkaproxy-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ef48eaf243c9f0c8f663e17c10bb63a6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 11185,
            "upload_time": "2023-02-11T08:52:49",
            "upload_time_iso_8601": "2023-02-11T08:52:49.973925Z",
            "url": "https://files.pythonhosted.org/packages/fa/d5/5f07224bbe91b57227739c3781f83ffe1ab2dbacffb45a9a563e92162da3/kafkaproxy-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "464468385244bf893607cde666994d41a72ba4ecd50ab62e5048a1e0fcad1ecf",
                "md5": "a503fd3d2a0ad500ef5be137dc6130bf",
                "sha256": "b9cc08ac68d8c69c97e4e21daa80f8c8d32e970d67acc2f3b7529f71fbd5c04f"
            },
            "downloads": -1,
            "filename": "kafkaproxy-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "a503fd3d2a0ad500ef5be137dc6130bf",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9869,
            "upload_time": "2023-02-11T08:52:52",
            "upload_time_iso_8601": "2023-02-11T08:52:52.950630Z",
            "url": "https://files.pythonhosted.org/packages/46/44/68385244bf893607cde666994d41a72ba4ecd50ab62e5048a1e0fcad1ecf/kafkaproxy-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-11 08:52:52",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "kafkaproxy"
}
        
hsz
Elapsed time: 0.04448s