search

Home  >  Q&A  >  body text

How to send json data to kafka using Java

On the Internet, I only see some news about Java producing STRING type.
According to Java's producer class, you can customize the type of message sent, such as producer.send(new KeyedMessage<String, HashMap<String, String>>(topic,message);
But it runs like this An error will be reported. The error is as follows. Please give an expert's answer:
Exception in thread "Thread-4" java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String

at kafka.serializer.StringEncoder.toBytes(Encoder.scala:46)
at kafka.producer.async.DefaultEventHandler$$anonfun$serialize.apply(DefaultEventHandler.scala:130)
at kafka.producer.async.DefaultEventHandler$$anonfun$serialize.apply(DefaultEventHandler.scala:125)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.WrappedArray.foreach(WrappedArray.scala:34)
at kafka.producer.async.DefaultEventHandler.serialize(DefaultEventHandler.scala:125)
at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:52)
at kafka.producer.Producer.send(Producer.scala:77)
at kafka.javaapi.producer.Producer.send(Producer.scala:33)
at com.Model.Producer.kafkaProducer.run(kafkaProducer.java:35)
我想大声告诉你我想大声告诉你2751 days ago1215

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-17 10:07:08

    Document, just serialize it into String

    reply
    0
  • Cancelreply