[英]“ClassCastException: kafka.message.Message cannot be cast to java.lang.String” on kafka Producer
I'm trying to write some simple producer to that writes messages to kafka I've downloaded kafka0.7 我正在尝试编写一些简单的生产者以向kafka发送消息,我已经下载了kafka0.7
Run the kafka server, producer and consumer 运行kafka服务器,生产者和使用者
> bin/kafka-server-start.sh config/server.properties
> bin/kafka-console-consumer.sh --zookeeper zooserver:2181 --topic test --from-beginning
> bin/kafka-console-producer.sh --zookeeper zooserver:2181 --topic test
And everything working! 一切正常!
Then I've created simple consumer , and it is worked also. 然后,我创建了简单的Consumer,它也可以工作。
SimpleConsumer consumer = new SimpleConsumer("172.21.110.134", 9092, 10000, 1024000);
…
ByteBufferMessageSet messages = consumer.fetch(fetchRequest);
System.out.println("consumed: " + Utils.toString(msg.message().payload(), "UTF-8"));
But the following producer always throws an error 但是以下生产者总是抛出错误
ProducerConfig config = new ProducerConfig(props);
…
Producer<String, Message> producer = new Producer<String, Message>(config);
producer.send(new ProducerData<String, Message>("test", new Message("Hello World".getBytes())));
Getting the following exception 得到以下异常
Exception in thread "main" java.lang.ClassCastException: kafka.message.Message cannot be cast to java.lang.String
at kafka.serializer.StringEncoder.toMessage(Encoder.scala:30)
at kafka.producer.ProducerPool$$anonfun$send$1$$anonfun$3$$anonfun$apply$1.apply(ProducerPool.scala:107)
I'm using in java code the maven dependencies 我在Java代码中使用的Maven依赖项
<dependency>
<groupId>com.twitter</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.9.2</version>
</dependency>
Please help, what Am I doing wrong? 请帮忙,我做错了什么? Why Can't I write via simple java code? 为什么我不能通过简单的Java代码编写?
The problem is that you have configured wrong serializer, this should be 问题是您配置了错误的序列化程序,这应该是
props.put("serializer.class", "kafka.serializer.DefaultEncoder");
Not "StringEncoder" 不是“ StringEncoder”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.