/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.confluent.examples.streams.interactivequeries;
import org.apache.kafka.common.serialization.Serializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.state.StreamsMetadata;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Looks up StreamsMetadata from KafkaStreams and converts the results
* into Beans that can be JSON serialized via Jersey.
*/
public class MetadataService {
private final KafkaStreams streams;
public MetadataService(final KafkaStreams streams) {
this.streams = streams;
}
/**
* Get the metadata for all of the instances of this Kafka Streams application
* @return List of {@link HostStoreInfo}
*/
public List<HostStoreInfo> streamsMetadata() {
// Get metadata for all of the instances of this Kafka Streams application
final Collection<StreamsMetadata> metadata = streams.allMetadata();
return mapInstancesToHostStoreInfo(metadata);
}
/**
* Get the metadata for all instances of this Kafka Streams application that currently
* has the provided store.
* @param store The store to locate
* @return List of {@link HostStoreInfo}
*/
public List<HostStoreInfo> streamsMetadataForStore(final String store) {
// Get metadata for all of the instances of this Kafka Streams application hosting the store
final Collection<StreamsMetadata> metadata = streams.allMetadataForStore(store);
return mapInstancesToHostStoreInfo(metadata);
}
/**
* Find the metadata for the instance of this Kafka Streams Application that has the given
* store and would have the given key if it exists.
* @param store Store to find
* @param key The key to find
* @return {@link HostStoreInfo}
*/
public <K> HostStoreInfo streamsMetadataForStoreAndKey(final String store,
final K key,
final Serializer<K> serializer) {
// Get metadata for the instances of this Kafka Streams application hosting the store and
// potentially the value for key
final StreamsMetadata metadata = streams.metadataForKey(store, key, serializer);
if (metadata == null) {
throw new NotFoundException();
}
return new HostStoreInfo(metadata.host(),
metadata.port(),
metadata.stateStoreNames());
}
private List<HostStoreInfo> mapInstancesToHostStoreInfo(
final Collection<StreamsMetadata> metadatas) {
return metadatas.stream().map(metadata -> new HostStoreInfo(metadata.host(),
metadata.port(),
metadata.stateStoreNames()))
.collect(Collectors.toList());
}
}