kafka producer的serializer

今天遇到问题,kafka的producer是的类型,却始终报错,
【kafka producer的serializer】

[2015-01-14 15:10:22,837] ERROR Error serializing message for topic haidafu (kafka.producer.async.DefaultEventHandler) java.lang.ClassCastException: java.lang.String cannot be cast to [B


找了半天问题,从网上搜到解决办法,原来kafka的producer有两种serializer,需要和producer的类型配合使用,特转载到这里
原文链接,点击打开链接


Kafka错误java.lang.String cannot be cast to [B

向kafka发送数据,默认支持String和byte[]2种类型,如何支持呢?serializer是关键。kafka默认包括kafka.serializer.StringEncoder
和kafka.serializer.DefaultEncoder 2个类,分别支持String和二进制。在创建Producer时,需要配置参数props.put("serializer.class", "kafka.serializer.StringEncoder");

KeyedMessage将需要发送的进行封装,根据定义的serializer.class,定义不同的KeyedMessage

如果需要发送字符串,方式如下:

import java.util.Properties;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

//创建Producer
Properties props = new Properties();
props.put("metadata.broker.list", "192.168.1.1:9092,192.168.1.2:9092 ");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("producer.type", "sync");
//props.put("reconnect.time.interval.ms", 5*1000);
props.put("request.required.acks", "1");
//props.put("compression.codec", "gzip");

ProducerConfig config = new ProducerConfig(props);
Producer producer = new Producer(config);

//发送数据

String message = "hello message";
//KeyedMessage 第一个String是key的类型,第二个String是value类型
//可以用key来进行Hash,发送message到不同的分区

KeyedMessage keymsg = new KeyedMessage("mytopic",message);
//这里的KeyedMessage没有key值

producer.send(keymsg);


发送二进制消息,方式类似,需要修改serializer.class,和key.serializer.class配置

import java.util.Properties;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;



//创建Producer
Properties props = new Properties();
props.put("metadata.broker.list", "192.168.1.1:9092,192.168.1.2:9092 ");
props.put("serializer.class", "kafka.serializer.DefaultEncoder");
//key的类型需要和serializer保持一致,如果key是String,则需要配置为kafka.serializer.StringEncoder,如果不配置,默认为kafka.serializer.DefaultEncoder,即二进制格式
props.put("key.serializer.class", "kafka.serializer.StringEncoder");
props.put("producer.type", "sync");
//props.put("reconnect.time.interval.ms", 5*1000);
props.put("request.required.acks", "1");
//props.put("compression.codec", "gzip");

ProducerConfig config = new ProducerConfig(props);
Producer producer = new Producer(config);

//发送数据

String message = "hello message";
//KeyedMessage 第一个String是key的类型,第二个byte[]是value类型
//可以用key来进行Hash,发送message到不同的分区

KeyedMessage keymsg = new KeyedMessage("mytopic",message.getBytes());
//这里的KeyedMessage没有key值

producer.send(keymsg);

如果serializer.class或key.serializer.class配置不正确,就会报如下错误

java.lang.ClassCastException: java.lang.String cannot be cast to [B
at kafka.serializer.DefaultEncoder.toBytes(Encoder.scala:34)
at kafka.producer.async.DefaultEventHandler$$anonfun$serialize$1.apply(DefaultEventHandler.scala:128)
at kafka.producer.async.DefaultEventHandler$$anonfun$serialize$1.apply(DefaultEventHandler.scala:125)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233)
at scala.collection.Iterator$class.foreach(Iterator.scala:772)
at scala.collection.JavaConversions$JIteratorWrapper.foreach(JavaConversions.scala:573)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:73)
at scala.collection.JavaConversions$JListWrapper.foreach(JavaConversions.scala:615)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:233)
at scala.collection.JavaConversions$JListWrapper.map(JavaConversions.scala:615)
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:76)
at kafka.javaapi.producer.Producer.send(Producer.scala:42)


    推荐阅读