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
相关推荐
欧阳x天几秒前
STL详解(九)—— stack和queue的模拟实现
开发语言·c++
xqqxqxxq1 分钟前
洛谷算法1-1 模拟与高精度(NOIP经典真题解析)java(持续更新)
java·开发语言·算法
沐知全栈开发7 分钟前
Rust 函数
开发语言
乔江seven7 分钟前
【python轻量级Web框架 Flask 】2 构建稳健 API:集成 MySQL 参数化查询与 DBUtils 连接池
前端·python·mysql·flask·web
2301_8107301025 分钟前
python第四次作业
数据结构·python·算法
马剑威(威哥爱编程)26 分钟前
Libvio.link爬虫技术解析:搞定反爬机制
爬虫·python
zhougl99634 分钟前
Java 枚举类(enum)详解
java·开发语言·python
yong999036 分钟前
基于势能原理的圆柱齿轮啮合刚度计算MATLAB程序实现
开发语言·matlab
恋爱绝缘体138 分钟前
Java语言提供了八种基本类型。六种数字类型【函数基数噶】
java·python·算法
serve the people41 分钟前
python环境搭建 (三) FastAPI 与 Flask 对比
python·flask·fastapi