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
相关推荐
喜欢喝果茶.7 分钟前
QOverload<参数列表>::of(&函数名)信号槽
开发语言·qt
亓才孓8 分钟前
[Class类的应用]反射的理解
开发语言·python
努力学编程呀(๑•ี_เ•ี๑)8 分钟前
【在 IntelliJ IDEA 中切换项目 JDK 版本】
java·开发语言·intellij-idea
小镇敲码人17 分钟前
深入剖析华为CANN框架下的Ops-CV仓库:从入门到实战指南
c++·python·华为·cann
island131430 分钟前
CANN GE(图引擎)深度解析:计算图优化管线、内存静态规划与异构任务的 Stream 调度机制
开发语言·人工智能·深度学习·神经网络
坚持就完事了34 分钟前
Java中的集合
java·开发语言
摘星编程36 分钟前
深入理解CANN ops-nn BatchNormalization算子:训练加速的关键技术
python
魔芋红茶38 分钟前
Python 项目版本控制
开发语言·python
lili-felicity44 分钟前
CANN批处理优化技巧:从动态批处理到流水线并行
人工智能·python
一个有梦有戏的人1 小时前
Python3基础:进阶基础,筑牢编程底层能力
后端·python