elasticsearch,apache-kafka,Java,Spring,elasticsearch,Apache Kafka" /> elasticsearch,apache-kafka,Java,Spring,elasticsearch,Apache Kafka" />

Java 卡夫卡制作人发送无效字符

Java 卡夫卡制作人发送无效字符,java,spring,elasticsearch,apache-kafka,Java,Spring,elasticsearch,Apache Kafka,使用以下代码,我发送Elasticsearch文档进行索引。我尝试将基本对象转换为JSON并通过producer发送。但是,每条消息(从控制台检查)都会附加jibberish字符,如-��T�{“productId”:2455 出站配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:

使用以下代码,我发送Elasticsearch文档进行索引。我尝试将基本对象转换为JSON并通过producer发送。但是,每条消息(从控制台检查)都会附加jibberish字符,如-��T�{“productId”:2455

出站配置

 <?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>

有线索吗


使用的插件:

这些可能是您的控制台无法很好解释的制表符(因为缩进的JSON)

如果禁用对象映射器生成的输出缩进,这些字符可能会消失

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

mapper.disable(SerializationFeature.INDENT_OUTPUT);Kafka不会执行此类操作。在将其发送给Kafka producer的字符串消息中进行调试。 如果您是从URL或HTML表单获取此消息,您可能需要先对消息进行解码,然后再发送给制作人


e、 g.urldecker.decode(消息,“UTF-8”)

我今天遇到了这个问题,通过在producer配置中设置正确的值序列化程序类解决了它,如下所示:

<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" />


感谢@Val的响应,但即使在添加了给定的行之后,也会返回相同的输出。还有什么想法吗?在生成JSON后有没有办法设置断点(例如在
logger.info()上)查看JSON字符串中的内容?您的Kafka使用者是否也会看到坏字符,还是仅在控制台中?在使用者端获取坏字符,这将返回JSON解析错误。
logger.info()
为变量json返回正确的字符串。我猜这些字符是在构建有效负载后追加的。您是否有来自使用者端的堆栈跟踪告诉我们有关json解析错误的更多信息?
05/Oct/2015:15:54:29:374+0530[main]error o.e.kafka.consumer.MessageHandler-失败消息#1:MapperParsingException[分析失败];嵌套:JsonParseException[意外字符(','(代码172)):在[源代码]处应为有效值(数字、字符串、数组、对象、'true'、'false'或'null')[B@4dd7edb0;行:1,列:2]];
<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" />