类型不匹配:无法转换为Map <string,consumerrecords <string,supplier>&gt;到ConsumerRecords <string,supplier>

时间:2017-06-06 09:40:54

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

当我在eclipse中尝试运行SupplierConsumer类时,我遇到了这些错误。这是我的代码:

public class SupplierConsumer{

    public static void main(String[] args) throws Exception{

            String topicName = "SupplierTopic";
            String groupName = "SupplierTopicGroup";

            Properties props = new Properties();
            props.put("bootstrap.servers", "localhost:9092,localhost:9093");
            props.put("group.id", groupName);
            props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
            props.put("value.deserializer", "SupplierDeserializer");


            KafkaConsumer<String, Supplier> consumer = new KafkaConsumer<>(props);
            consumer.subscribe(Arrays.asList(topicName));

            while (true){
                    ConsumerRecords<String, Supplier> records = consumer.poll(100);
                    for (ConsumerRecord<String, Supplier> record : records){
                            System.out.println("Supplier id= " + String.valueOf(record.value().getID()) + " Supplier  Name = " + record.value().getName() + " Supplier Start Date = " + record.value().getStartDate().toString());
                    }
            }
    }
}
  1. 类型不匹配:无法从List<String>转换为String
  2. 类型不匹配:无法从Map<String,ConsumerRecords<String,Supplier>>转换为ConsumerRecords<String,Supplier>

1 个答案:

答案 0 :(得分:0)

我认为你无意中在你的类路径中包含了一些kafka-clients库的预发布版本。我在v0.8.2-beta中找到了与你的编译错误匹配的签名:

http://supergsego.com/apache/kafka/0.8.2-beta/java-doc/org/apache/kafka/clients/consumer/KafkaConsumer.html

如果您确保使用kafka-clients jar的发行版质量版本(v0.9或更高版本),则应编译代码。