Java Code Examples for org.apache.kafka.streams.state.ReadOnlyKeyValueStore#all()

The following examples show how to use org.apache.kafka.streams.state.ReadOnlyKeyValueStore#all() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KafkaStorageHttpService.java    From zipkin-storage-kafka with Apache License 2.0 6 votes vote down vote up
@Get("/serviceNames")
@ProducesJson
public JsonNode getServiceNames() {
  try {
    if (!storage.traceSearchEnabled) return MAPPER.createArrayNode();
    ReadOnlyKeyValueStore<String, String> store = storage.getTraceStorageStream()
        .store(SERVICE_NAMES_STORE_NAME, QueryableStoreTypes.keyValueStore());
    ArrayNode array = MAPPER.createArrayNode();
    try (KeyValueIterator<String, String> all = store.all()) {
      all.forEachRemaining(keyValue -> array.add(keyValue.value));
    }
    return array;
  } catch (InvalidStateStoreException e) {
    LOG.debug("State store is not ready", e);
    throw e;
  }
}
 
Example 2
Source File: KafkaStorageHttpService.java    From zipkin-storage-kafka with Apache License 2.0 6 votes vote down vote up
@Get("/autocompleteTags")
@ProducesJson
public JsonNode getAutocompleteTags() {
  try {
    if (!storage.traceSearchEnabled) return MAPPER.createArrayNode();
    ReadOnlyKeyValueStore<String, Set<String>> autocompleteTagsStore =
        storage.getTraceStorageStream().store(AUTOCOMPLETE_TAGS_STORE_NAME,
            QueryableStoreTypes.keyValueStore());
    ArrayNode array = MAPPER.createArrayNode();
    try (KeyValueIterator<String, Set<String>> all = autocompleteTagsStore.all()) {
      all.forEachRemaining(keyValue -> array.add(keyValue.key));
    }
    return array;
  } catch (InvalidStateStoreException e) {
    LOG.debug("State store is not ready", e);
    throw e;
  }
}
 
Example 3
Source File: MetricsResource.java    From kafka-streams-example with Apache License 2.0 6 votes vote down vote up
/**
 * Query local state store to extract metrics
 *
 * @return local Metrics
 */
private Metrics getLocalMetrics() {
    HostInfo thisInstance = GlobalAppState.getInstance().getHostPortInfo();
    KafkaStreams ks = GlobalAppState.getInstance().getKafkaStreams();

    String source = thisInstance.host() + ":" + thisInstance.port();
    Metrics localMetrics = new Metrics();

    ReadOnlyKeyValueStore<String, Double> averageStore = ks
            .store(storeName,
                    QueryableStoreTypes.<String, Double>keyValueStore());

    LOGGER.log(Level.INFO, "Entries in store {0}", averageStore.approximateNumEntries());
    KeyValueIterator<String, Double> storeIterator = averageStore.all();

    while (storeIterator.hasNext()) {
        KeyValue<String, Double> kv = storeIterator.next();
        localMetrics.add(source, kv.key, String.valueOf(kv.value));

    }
    LOGGER.log(Level.INFO, "Local store state {0}", localMetrics);
    return localMetrics;
}
 
Example 4
Source File: InteractiveQueryServer.java    From kafka-streams-in-action with Apache License 2.0 5 votes vote down vote up
private String getKeyValuesAsJson(String store) {
    ReadOnlyKeyValueStore<String, Long> readOnlyKeyValueStore = kafkaStreams.store(store, QueryableStoreTypes.keyValueStore());
    List<KeyValue<String, Long>> keyValues = new ArrayList<>();
    try (KeyValueIterator<String, Long> iterator = readOnlyKeyValueStore.all()) {
        while (iterator.hasNext()) {
            keyValues.add(iterator.next());
        }
    }
    return gson.toJson(keyValues);
}
 
Example 5
Source File: StreamUtils.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
public static <K, V> List<KeyValue<K, V>> listFromStore(KafkaStreams streams, String storeName) {
    final ReadOnlyKeyValueStore<K, V> store = streams.store(
        storeName, QueryableStoreTypes.keyValueStore());

    try (final KeyValueIterator<K, V> all = store.all()) {
        List<KeyValue<K, V>> result = new ArrayList<>();
        while (all.hasNext()) {
            result.add(all.next());
        }
        return result;
    }
}
 
Example 6
Source File: StreamUtils.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
public static <K, V> NavigableMap<K, V> mapFromStore(KafkaStreams streams, String storeName) {
    final ReadOnlyKeyValueStore<K, V> store = streams.store(
        storeName, QueryableStoreTypes.keyValueStore());

    try (final KeyValueIterator<K, V> all = store.all()) {
        NavigableMap<K, V> result = new TreeMap<>();
        while (all.hasNext()) {
            KeyValue<K, V> next = all.next();
            result.put(next.key, next.value);
        }
        return result;
    }
}