如何使用spring从kafka入站消息中提取数据

时间:2015-07-28 05:53:11

标签: spring-integration apache-kafka

我正在用春天建造卡夫卡消费者。我的配置似乎非常简单。正在使用消息并将其保存在文件中。但是,有效载荷是神秘的,我无法获取数据(简短的“hello world”消息)。

这是我访问有效负载时得到的结果(例如,当我在入站kafka和outboud文件中设置变换器bean时:

{test-topic={0=[[B@713c9d72, [B@7d656f90, [B@26bb8c83, [B@4b959d83 [B@5ed74e8e]}}

我的问题是:如何访问实际有效负载(“hellow world”字符串“)?

我的配置是:

<int:channel id="inputFromKafka">
    <int:queue />
</int:channel>
        <int:poller 
        max-messages-per-poll="5" default = "true" fixed-delay="10" time-unit="MILLISECONDS"/>

<int-kafka:inbound-channel-adapter
    id="kafkaInboundChannelAdapter" kafka-consumer-context-ref="consumerContext"
    auto-startup="true" channel="inputFromKafka">

<int-kafka:consumer-context id="consumerContext"
    consumer-timeout="40000" zookeeper-connect="zookeeperConnect">
    <int-kafka:consumer-configurations>
        <int-kafka:consumer-configuration
            group-id="group12" max-messages="5">
            <int-kafka:topic id="test-topic" streams="1" />
        </int-kafka:consumer-configuration>
    </int-kafka:consumer-configurations>
</int-kafka:consumer-context>
<int-kafka:zookeeper-connect id="zookeeperConnect"
    zk-connect="localhost:2181" zk-connection-timeout="6000"
    zk-session-timeout="6000" zk-sync-time="2000" />


<file:outbound-channel-adapter id="filesOut"
    directory="/tmp/fromKafka">
    </file:outbound-channel-adapter>

1 个答案:

答案 0 :(得分:0)

You are seeing raw byte[].

Add...

<bean id="decoder" 
    class="org.springframework.integration.kafka.serializer.common.StringDecoder" />

and

<int-kafka:consumer-configuration
                     value-decoder="decoder"
        ...