pl.allegro.tech.hermes.common.kafka.JsonToAvroMigrationKafkaNamesMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hermes-common Show documentation
Show all versions of hermes-common Show documentation
Fast and reliable message broker built on top of Kafka.
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()));
}
};
}