简体   繁体   English

Kafka制作人发送无效字符

[英]Kafka producer sending invalid characters

Using following code, I send Elasticsearch documents for indexing. 使用以下代码,我发送Elasticsearch文档以进行索引。 I tried converting basic Objects to JSON and sent via producer. 我尝试将基本对象转换为JSON并通过制作人发送。 However, every message (as checked from the console) appends jibberish characters like - t {"productId":2455 但是,每条消息(从控制台检查)都附加了像 - t {“productId”:2455这样的乱码字符

public boolean sendMessage()
{
    PageRequest page = new PageRequest(0, 1); 
    Product p = product.findByName("Cream", page).getContent().get(0);
    String json = "";
    ObjectMapper mapper = new ObjectMapper();
    try {
        json = mapper.writeValueAsString(p);
    } catch (JsonProcessingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }       
    logger.info("JSON = " + json);

    boolean status =  inputToKafka.send(org.springframework.integration.support.MessageBuilder.withPayload(json).build());
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return status;
}

Outbound configuration 出站配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/integration/kafka http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

    <int:channel id="inputToKafka">
        <int:queue/>
    </int:channel>

    <int-kafka:outbound-channel-adapter
            id="kafkaOutboundChannelAdapter"
            kafka-producer-context-ref="kafkaProducerContext"
            channel="inputToKafka">
        <int:poller fixed-delay="1000" time-unit="MILLISECONDS" receive-timeout="0" task-executor="taskExecutor"/>
    </int-kafka:outbound-channel-adapter>

    <task:executor id="taskExecutor" pool-size="5" keep-alive="120" queue-capacity="500"/>

    <int-kafka:producer-context id="kafkaProducerContext">
        <int-kafka:producer-configurations>
            <int-kafka:producer-configuration broker-list="localhost:9092"
                                              topic="test_topic"
                                              compression-codec="default"/>
        </int-kafka:producer-configurations>
    </int-kafka:producer-context>

    </beans>

Any clue ? 任何线索?

Plugin used: Spring Extension Kafka 使用的插件: Spring Extension Kafka

I faced this issue today and got it resolved by setting correct value-serializer class in the producer configuration as shown below: 我今天遇到了这个问题,并通过在生产者配置中设置正确的value-serializer类来解决它,如下所示:

<int-kafka:producer-configuration
                broker-list="localhost:9092" topic="headers['kafka_topic']"
                key-class-type="java.lang.String" value-class-type="java.lang.String"
                key-serializer="kafkaSerializer" value-serializer="kafkaSerializer"/>

<bean id="kafkaSerializer" class="org.apache.kafka.common.serialization.StringSerializer" />

Those could be tab characters (because of indented JSON) that your console doesn't interpret well. 这些可能是您的控制台无法很好地解释的制表符(因为缩进的JSON)。

If you disable the indentation of the output generated by the object mapper, those characters might go away. 如果禁用对象映射器生成的输出的缩进,则这些字符可能会消失。

try {
    mapper.disable(SerializationFeature.INDENT_OUTPUT);     <---- add this line
    json = mapper.writeValueAsString(p);
} catch (JsonProcessingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}       

Kafka does not do such things. 卡夫卡不做这样的事情。 Debug at your String message which you are sending it to Kafka producer. 调试您发送给Kafka生产者的String消息。 If you are getting this message from URL or HTML form, You may want to decode your message first, before sending to producer. 如果您从URL或HTML表单收到此消息,您可能需要先将消息解码,然后再发送给生产者。

eg URLDecoder.decode(message, "UTF-8") 例如URLDecoder.decode(消息,“UTF-8”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM