You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@kafka.apache.org by Yulia Stolin <yu...@amobee.com> on 2013/06/20 12:37:39 UTC

ClassCastException on Producer

Hi,

I'm trying to write some simple producer to that writes messages to kafka
I've downloaded kafka0.7

Run the kafka server, producer and consumer

Ø   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.
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())));

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 the following maven dependencies
     <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>

What am I missing?


Re: ClassCastException on Producer

Posted by Jun Rao <ju...@gmail.com>.
If you want to use the StringEncoder, you should instantiate the producer
as Producer<String, String>.

Thanks,

Jun


On Thu, Jun 20, 2013 at 3:37 AM, Yulia Stolin <yu...@amobee.com> wrote:

> Hi,
>
> I'm trying to write some simple producer to that writes messages to kafka
> I've downloaded kafka0.7
>
> Run the kafka server, producer and consumer
>
> Ø   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.
> 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())));
>
> 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 the following maven dependencies
>      <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>
>
> What am I missing?
>
>