Kafka事务生成器 - read_committed显示尽管中止的记录

时间:2017-11-21 14:05:33

标签: java apache-kafka kafka-producer-api

我编写了这个简单的程序来测试Kafka中的新事务生成器:

.*

但是当我使用package test; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.clients.producer.ProducerRecord; import java.util.Properties; class kafkatest { public static void main(String[] args) { Properties props = new Properties(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ProducerConfig.CLIENT_ID_CONFIG, "hello-world-producer"); props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true); props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "test"); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer producer = new KafkaProducer(props); producer.initTransactions(); producer.beginTransaction(); producer.send(new ProducerRecord<>("topic", "hello", "world")); producer.flush(); producer.abortTransaction(); producer.close(); } } 消费时,该记录会显示出来:

isolation.level=read_committed

我错过了什么?

1 个答案:

答案 0 :(得分:3)

要将read_committed与控制台使用者一起使用,您需要指定--isolation-level选项:

kafka-console-consumer.sh --bootstrap-server localhost:9092 \
--topic topic --from-beginning --isolation-level=read_committed

此选项默认为read_uncommitted并覆盖您通过--consumer-property传递的值。