KafkaStreams如何实例化ConsumerRecordFactory?

时间:2018-11-15 14:14:03

标签: apache-kafka-streams inferred-type

我正尝试使用Kafka Streams提供的ConsumerRecordFactory(主要遵循confluent doc)来测试流式应用程序,这是我到目前为止的代码:

// Properties of the application
Properties streamsConfiguration = new Properties();

// Give the Streams application a unique name.  The name must be unique in the Kafka cluster
streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "testing_application");
streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummyserver:2181");

// Create the topology builder
StreamsBuilder builder = new StreamsBuilder();

// Run it on the test driver
TopologyTestDriver testDriver = new TopologyTestDriver(builder.build(), streamsConfiguration);

// Feed input data
ConsumerRecordFactory<String, Integer> factory = new ConsumerRecordFactory<>(
        "input-topic",
        new StringSerializer(),
        new IntegerSerializer()
);

// Create a test record
ConsumerRecordFactory<byte[], byte[]> record = factory.create("key", 42L);

我的问题是,当我编译代码时,出现以下错误:

Error:(70, 52) java: reference to create is ambiguous
  both method create(K,V,long) in org.apache.kafka.streams.test.ConsumerRecordFactory
  and method create(java.lang.String,V,long) in org.apache.kafka.streams.test.ConsumerRecordFactory match

因此,我了解到kafka流定义了通用方法create(K,V,long),并且当我使用非通用类型创建工厂时,会创建一个与第一个方法冲突的新方法。

我的问题是我应该如何例化我的ConsumerRecordFactory

我尝试使用ConsumerRecordFactory<Object, Integer>使我的工厂更通用,但是推断的类型不匹配。而且我找不到其他示例,融合的github存储库kafka-streams-examples似乎没有使用ConsumerRecordFactory,并且this SO answer似乎正在使用与文档相同的代码。

(我知道问题更多的是Java,而不是kafka流,但我认为用apache-kafka-streams进行标记是接触习惯ConsumerRecordFactory的人们的好方法)

1 个答案:

答案 0 :(得分:1)

以下代码中存在一些问题:

// Feed input data ConsumerRecordFactory<String, Integer> factory = new ConsumerRecordFactory<>(
        "input-topic",
        new StringSerializer(),
        new IntegerSerializer() );

// Create a test record
ConsumerRecordFactory<byte[], byte[]> record = factory.create("key", 42L);
  1. 您在ConsumerRecordFactory中将valueType定义为 Integer ,但是在create()方法中,您正在传递 Long 类型值。
  2. factory.create()返回一个ConsumerRecord而不是ConsumerRecordFactory

关于方法的歧义,你是对的。因此,请避免使用该问题,请使用以下命令:

ConsumerRecordFactory<String, Integer> factory = new ConsumerRecordFactory<>( 
        new StringSerializer(),
        new IntegerSerializer()
);
// Use ConsumerRecord here instead of ConsumerRecordFactory
ConsumerRecord<byte[], byte[]> record = factory.create("input-topic","key", 42);