All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.allegro.tech.hermes.common.kafka.JsonToAvroMigrationKafkaNamesMapper Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
package pl.allegro.tech.hermes.common.kafka;

import pl.allegro.tech.hermes.api.ContentType;
import pl.allegro.tech.hermes.api.Topic;

import java.util.function.Function;

public class JsonToAvroMigrationKafkaNamesMapper extends NamespaceKafkaNamesMapper {

    public JsonToAvroMigrationKafkaNamesMapper(String namespace, String namespaceSeparator) {
        super(namespace, namespaceSeparator);
    }

    public KafkaTopics toKafkaTopics(Topic topic) {
        KafkaTopic primary = mapToKafkaTopic.andThen(appendNamespace).andThen(appendContentTypeSuffix).apply(topic);

        if (topic.wasMigratedFromJsonType()) {
            KafkaTopic secondary = mapToJsonKafkaTopic.andThen(appendNamespace).andThen(appendContentTypeSuffix).apply(topic);
            return new KafkaTopics(primary, secondary);
        }
        return new KafkaTopics(primary);
    }

    private final Function mapToJsonKafkaTopic = it ->
            new KafkaTopic(KafkaTopicName.valueOf(it.getQualifiedName()), ContentType.JSON);

    private final Function appendContentTypeSuffix = kafkaTopic -> {
        switch (kafkaTopic.contentType()) {
            case JSON:
                return kafkaTopic;
            case AVRO:
                return new KafkaTopic(KafkaTopicName.valueOf(kafkaTopic.name().asString() + "_avro"), kafkaTopic.contentType());
            default:
                throw new IllegalStateException(String.format("Unknown content type '%s'", kafkaTopic.contentType()));
        }
    };
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy