【发布时间】:2016-11-09 05:18:24
【问题描述】:
我正在尝试用 java 向 Kafka 发送消息。我的项目是通过maven依赖使用kafka_2.9.2-0.8.1.1.jar:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>0.8.1.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
我遇到的问题是我的制片人。我想发送一个 KeyedMessage 并通过这里的文档:http://kafka.apache.org/documentation.html#producerapi 它声明 producer.send 方法应该采用 KeyedMessage 作为其参数。当我检查 producer.send 调用可用的选项时,它只允许 ProducerData 对象作为可接受的参数。
我的代码是这样设置的:
private String topic;
private String key;
private Properties props = new Properties();
private Producer<String,String> producer;
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("zk.connect", "127.0.0.1:2181");
//create the producer
producer = new Producer<String, String>(new ProducerConfig(props));
String topic = "test";
String key = "test_key";
String message = "test_msg";
KeyedMessage<String, String> data = new KeyedMessage<String, String>(topic, key, message);
producer.send(data) <---- ERROR is here, send method not allowed param of KeyedMessage
实际的错误是这样的:
The method send(ProducerData<String,String>) in the type Producer<String,String> is not applicable for the arguments (KeyedMessage<String,String>)
【问题讨论】:
标签: java apache-kafka