python cobnfluent kafka transaction事务

官网

事务 API(Transactional API)

事务生产者基于 幂等生产者(idempotent producer) 运行,并在与消费者隔离级别设置为 read_committed 时,为 Apache Kafka 提供完整的 Exactly-Once Semantics(EOS)


Transactional Producer 操作原理

Transactional Producer 在幂等生产者之上提供全事务支持。

当与事务感知消费者(isolation.level=read_committed)一起使用时,它可以确保完全一次语义。


配置 Producer

通过设置 transactional.id 为在应用程序中唯一的标识符来启用 Producer 的事务支持。

该 ID 用于将来自先前实例的陈旧事务进行 fence(隔离),这通常会在故障或崩溃后发生。


阶段 API
初始化事务 producer.init_transactions()
开始事务 producer.begin_transaction()
生产消息 producer.produce(...)
提交 offset producer.send_offsets_to_transaction(...)
提交事务 producer.commit_transaction()
回滚事务 producer.abort_transaction()
  1. 生产者配置事务
python 复制代码
from confluent_kafka import Producer,Consumer
import json


class Transaction:
    
    def __init__(self) -> None:
        self.conf = {'bootstrap.servers': f'192.168.220.130:30500',"transactional.id":"12345"}
        self.produce = Producer(self.conf)

    def sendmessage(self):
        message = {"name":"4","age":100}
        
        self.produce.init_transactions()
        
        self.produce.begin_transaction()
        
        try:
            self.produce.produce("batchtopic",json.dumps(message))
            self.produce.commit_transaction()
        except:
            self.produce.abort_transaction()
  1. 消费者配置
python 复制代码
class TransactionConsumer:
    
    def __init__(self) -> None:
        self.conf = {
                    'bootstrap.servers': f'192.168.220.130:30500',
                    'isolation.level':'read_committed',
                    'group.id': "123",
                    'enable.auto.commit': False,
                    "heartbeat.interval.ms":3000,
                    'session.timeout.ms': 30000,
                    'max.poll.interval.ms':30000,
                    'auto.offset.reset': 'earliest',
                    'compression.type': 'gzip',
                    'message.max.bytes': 10485760
                    }
        self.consumer = Consumer(self.conf)

        self.consumer.subscribe(["batchtopic"])

    # consumer message from kafka and generate xml file 
    def consumerMessage(self):
        while 1:
            msg = self.consumer.poll(1)
            if msg == None:
                continue
            if not msg.error() is None:
                print (msg.error())
                continue

            else:
                try:
                    value = json.loads(msg.value())
                    print(value)
                except Exception as e:
                    print("error")
    def close(self):
        try:
            self.cons.close()
        except Exception as e:
            pass
相关推荐
神仙别闹3 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
Y幽谷客3 小时前
Python加载本地大模型(Qwen3.5 8B)
python·大模型
伞伞悦读3 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
线上放牧人3 小时前
Windows删除图标缓存
windows·python·pyqt
qq_5470261794 小时前
Python 变量和简单数据类型
python
智搜广告5 小时前
智搜广告:科技行业AI回答优化公司如何破局
大数据·python·elasticsearch·geo
IpdataCloud5 小时前
AI智能体调用工具怎么核验来源IP?归属地、网络类型与代理风险识别(含Python代码)
数据库·python·tcp/ip
2601_966949656 小时前
只拿到股票代码还不够:量化程序到底还需要哪些标的信息?——从数据建模开始理解股票元数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
蒸鱼Yuzheng6 小时前
Python 游戏测试开发怎么准备:日志解析、接口校验、并发与可维护性
自动化测试·python·测试开发·面试题·游戏测试
m0_734571766 小时前
深入理解5g <十八>传输块tbsize的计算
开发语言·5g