You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@kafka.apache.org by Ramesh K <kr...@gmail.com> on 2014/12/02 11:12:21 UTC
How to send serialized object in Kafka Producer
Hi,
I have written the basic program to send String or byte[] messages to
consumer from producer by using java & Kafka 0.8.1 .
It Works perfectly.But i wanted to send serialized object(Java Bean Object).
Is it possible to send the serialized object from producer to consumer?
if possible, please share the ideas/samples.
Thanks in advance..
Ramesh Kasi
Re: How to send serialized object in Kafka Producer
Posted by Guozhang Wang <wa...@gmail.com>.
You need to write your own serializer (e.g. BeanSerializer extends
Encoder[T]) for bean object and specify that in:
properties.put("serializer.class","BeanSerializer")
Guozhang
Re: How to send serialized object in Kafka Producer
Posted by Ramesh K <kr...@gmail.com>.
Dear Guozhang,
Sorry for the late reply.
Please see the sample code which i written to send the bean object.
import java.util.Properties;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
public class KafkaProducerBean {
final static String TOPIC = "*alarm*";
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("metadata.broker.list","x.x.x.227:9092");
properties.put("serializer.class","TestBean");
ProducerConfig producerConfig = new ProducerConfig(properties);
kafka.javaapi.producer.Producer<String,TestBean> producer = new
kafka.javaapi.producer.Producer<String, TestBean>(producerConfig);
TestBean tb = new TestBean();
tb.setName("Olive");
tb.setAge(25);
KeyedMessage<String, TestBean> message =new KeyedMessage<String,
TestBean>(*TOPIC*, tb);
producer.send(message);
producer.close();
}
}
Error :
Exception in thread "main" java.lang.NoSuchMethodException:
TestBean.<init>(kafka.utils.VerifiableProperties)
Thanks
Ramesh K
On Wed, Dec 3, 2014 at 1:03 AM, Guozhang Wang <wa...@gmail.com> wrote:
> Ramesh,
>
> Which producer are you using in 0.8.1? kafka.api.producer or
> org.apache.kafka.clients.producer?
>
> Guozhang
>
> On Tue, Dec 2, 2014 at 2:12 AM, Ramesh K <kr...@gmail.com> wrote:
>
> > Hi,
> >
> > I have written the basic program to send String or byte[] messages to
> > consumer from producer by using java & Kafka 0.8.1 .
> > It Works perfectly.But i wanted to send serialized object(Java Bean
> > Object).
> >
> > Is it possible to send the serialized object from producer to consumer?
> >
> > if possible, please share the ideas/samples.
> >
> > Thanks in advance..
> > Ramesh Kasi
> >
>
>
>
> --
> -- Guozhang
>
Re: How to send serialized object in Kafka Producer
Posted by Guozhang Wang <wa...@gmail.com>.
Ramesh,
Which producer are you using in 0.8.1? kafka.api.producer or
org.apache.kafka.clients.producer?
Guozhang
On Tue, Dec 2, 2014 at 2:12 AM, Ramesh K <kr...@gmail.com> wrote:
> Hi,
>
> I have written the basic program to send String or byte[] messages to
> consumer from producer by using java & Kafka 0.8.1 .
> It Works perfectly.But i wanted to send serialized object(Java Bean
> Object).
>
> Is it possible to send the serialized object from producer to consumer?
>
> if possible, please share the ideas/samples.
>
> Thanks in advance..
> Ramesh Kasi
>
--
-- Guozhang