配置kafka以发送自定义类型数据

时间:2014-07-09 13:57:20

标签: thrift producer-consumer apache-kafka

我是kafka的新手

我使用官方示例创建了一个生产者和消费者greoup,虽然我想从生产者和消费者那里发送thrift bundle来获取bundle并存储在bundle数组中。

我已将生产者代码编写为

KeyedMessage<String, Bundle> data = new KeyedMessage<String, Bundle>("bundles", "Bundle", bundle); 
        producer.send(data);

但在消费者方面,我有

Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
    topicCountMap.put(topic, new Integer(NO_OF_THREADS));
    Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
    List<KafkaStream<byte[], byte[]>> messageStreams = consumerMap.get(topic);

我可以让kafka使用者将数据作为包类型而不是byte []数组获取。

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法将值直接解码为Bundle类型:

public interface kafka.javaapi.consumer.ConsumerConnector {
  ...
  public <K,V> Map<String, List<KafkaStream<K,V>>> 
     createMessageStreams(
         Map<String, Integer> topicCountMap, Decoder<K> keyDecoder, Decoder<V> valueDecoder);

在这种情况下,您需要使用相应的Thrift API实现自己的valueDecoder类型Decoder<Bundle>

请参阅Kafka文档中的High Level Consumer API说明。