web-dev-qa-db-ja.com

Kafkaパーティションのキー/値の逆シリアル化エラー

キーなしでKafkaトピックに送信すると統合テストに合格しましたが、キーを追加するとシリアル化エラーが発生し始めます。

org.Apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition topic-1 at offset 0

Caused by: org.Apache.kafka.common.errors.SerializationException: Size of data received by IntegerDeserializer is not 4

これは私の送信者クラスです:

public class Sender {
    private static final Logger LOG = LoggerFactory.getLogger(Sender.class);

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void send() {

        String topic = "topic";
        String data = "data";
        String key = "key";

        LOG.info("sending to topic: '{}', key: '{}', data: '{}'", topic, key, data);

        // does not work
        kafkaTemplate.send(topic, key, data);

        // works
        // kafkaTemplate.send(topic, data);

    }
}

これは私の構成です。ここで、キーにStringSerializerを指定します

@Configuration
@EnableKafka
public class Config {

    @Bean
    public Sender sender() {
        return new Sender();
    }

    @Bean
    public Properties properties() {
        return new Properties();
    }

    @Bean
    public Map<String, Object> producerConfigs(Properties properties) {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServers());
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return props;
    }

    @Bean
    public ProducerFactory<String, String> producerFactory(Properties properties) {
        return new DefaultKafkaProducerFactory<>(producerConfigs(properties));
    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate(Properties properties) {
        return new KafkaTemplate<>(producerFactory(properties));
    }

}

問題は私のテストのメッセージリスナーに関連している可能性がありますが、それはまた、全面的に文字列を使用しています

@RunWith(SpringRunner.class)
@SpringBootTest()
@DirtiesContext
public class SenderIT {


    public static final Logger LOG = LoggerFactory.getLogger(SenderIT.class);

    private static String SENDER_TOPIC = "topic";

    @Autowired
    private Sender sender;

    private KafkaMessageListenerContainer<String, String> container;

    private BlockingQueue<ConsumerRecord<String, String>> records;

    @ClassRule
    public static KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, true, SENDER_TOPIC);

    @Before
    public void setUp() throws Exception {

        // set up the Kafka consumer properties
        Map<String, Object> consumerProperties =
            KafkaTestUtils.consumerProps("sender", "false", embeddedKafka);

        consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        // create a Kafka consumer factory
        DefaultKafkaConsumerFactory<String, String> consumerFactory =
            new DefaultKafkaConsumerFactory<String, String>(consumerProperties);

        // set the topic that needs to be consumed
        ContainerProperties containerProperties = new ContainerProperties(SENDER_TOPIC);

        // create a Kafka MessageListenerContainer
        container = new KafkaMessageListenerContainer<>(consumerFactory, containerProperties);

        // create a thread safe queue to store the received message
        records = new LinkedBlockingQueue<>();

        // setup a Kafka message listener
        container.setupMessageListener(new MessageListener<String, String>() {
        @Override
        public void onMessage(ConsumerRecord<String, String> record) {
            LOG.debug("test-listener received message='{}'", record.toString());
            records.add(record);
        }
        });

        // start the container and underlying message listener
        container.start();

        // wait until the container has the required number of assigned partitions
        ContainerTestUtils.waitForAssignment(container, embeddedKafka.getPartitionsPerTopic());
    }

    @After
    public void tearDown() {
        // stop the container
        container.stop();
    }

    @Test
    public void test() throws InterruptedException {

        sender.send();

        // check that the message was received in Kafka
        ConsumerRecord<String, String> kafkaTopicMsg = records.poll(10, TimeUnit.SECONDS);

        LOG.debug("kafka recieved = {}", kafkaTopicMsg);

        assertThat(kafkaTopicMsg).isNotNull();

    }

}

いつものように、どんな助けもいただければ幸いです。

再現するすべてのコードは https://github.com/LewisWatson/kafka-embedded-test/tree/8322621ad4e302d982e5ecd28af9fd314696d85 で入手できます

完全なスタックトレースは https://travis-ci.org/LewisWatson/kafka-embedded-test/builds/273227986 で入手できます。

7
Lewis Watson

ログをさらに検査した後、問題をテストメッセージリスナーに絞り込むことができました。

2017-09-08 09:30:06.845 ERROR 2550 --- [           -C-1] essageListenerContainer$ListenerConsumer : Container exception

org.Apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition topic-1 at offset 0

Caused by: org.Apache.kafka.common.errors.SerializationException: Size of data received by IntegerDeserializer is not 4

https://travis-ci.org/LewisWatson/kafka-embedded-test/builds/273227986#L2961

なんらかの理由でキーが整数であると想定しているようです。

コンシューマファクトリに文字列デシリアライザを明示的に設定すると、問題が修正されました。

// create a Kafka consumer factory
DefaultKafkaConsumerFactory<String, String> consumerFactory =
    new DefaultKafkaConsumerFactory<String, String>(consumerProperties,
        new StringDeserializer(), new StringDeserializer());
11
Lewis Watson