io.quarkus.kafka.streams.runtime.devui.KafkaStreamsJsonRPCService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-kafka-streams Show documentation
Show all versions of quarkus-kafka-streams Show documentation
Implement stream processing applications based on Apache Kafka
package io.quarkus.kafka.streams.runtime.devui;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import jakarta.inject.Inject;
import org.apache.kafka.streams.Topology;
import io.smallrye.common.annotation.NonBlocking;
import io.vertx.core.json.JsonObject;
public class KafkaStreamsJsonRPCService {
@Inject
Topology topology;
@NonBlocking
public JsonObject getTopology() {
return parseTopologyDescription(topology.describe() != null ? topology.describe().toString() : "");
}
JsonObject parseTopologyDescription(String topologyDescription) {
final var res = new JsonObject();
final var context = new TopologyParserContext();
Arrays.stream(topologyDescription.split("\n"))
.map(String::trim)
.forEachOrdered(line -> Stream.of(SUB_TOPOLOGY, SOURCE, PROCESSOR, SINK, RIGHT_ARROW)
.filter(itemParser -> itemParser.test(line))
.forEachOrdered(itemParser -> itemParser.accept(context)));
res
.put("describe", topologyDescription)
.put("subTopologies", context.subTopologies)
.put("sources", context.sources)
.put("sinks", context.sinks)
.put("stores", context.stores)
.put("graphviz", context.graphviz.toGraph())
.put("mermaid", context.mermaid.toGraph());
return res;
}
private interface RawTopologyItemParser extends Predicate, Consumer {
}
private static final RawTopologyItemParser SUB_TOPOLOGY = new RawTopologyItemParser() {
private final Pattern subTopologyPattern = Pattern.compile("Sub-topology: (?[0-9]*).*");
private Matcher matcher;
@Override
public boolean test(String line) {
matcher = subTopologyPattern.matcher(line);
return matcher.matches();
}
@Override
public void accept(TopologyParserContext context) {
context.addSubTopology(matcher.group("subTopology"));
}
};
private static final RawTopologyItemParser SOURCE = new RawTopologyItemParser() {
private final Pattern sourcePattern = Pattern
.compile("Source:\\s+(?