/** * Performs a range query on a KeyValue Store and converts the results into a List of * {@link KeyValueBean} * @param storeName The store to query * @param rangeFunction The range query to run, i.e., all, from(start, end) * @return List of {@link KeyValueBean} */ private List<KeyValueBean> rangeForKeyValueStore(final String storeName, final Function<ReadOnlyKeyValueStore<String, Long>, KeyValueIterator<String, Long>> rangeFunction) { // Get the KeyValue Store final ReadOnlyKeyValueStore<String, Long> store = streams.store(storeName, QueryableStoreTypes.keyValueStore()); final List<KeyValueBean> results = new ArrayList<>(); // Apply the function, i.e., query the store final KeyValueIterator<String, Long> range = rangeFunction.apply(store); // Convert the results while (range.hasNext()) { final KeyValue<String, Long> next = range.next(); results.add(new KeyValueBean(next.key, next.value)); } return results; }
private KeyValue<K, V> nextStoreValue(final KS nextStoreKey) { final KeyValue<KS, VS> next = storeIterator.next(); if (!next.key.equals(nextStoreKey)) { throw new IllegalStateException("Next record key is not the peeked key value; this should not happen"); } return deserializeStorePair(next); }
@Override public KeyValue<K, V> next() { if (!hasNext()) { throw new NoSuchElementException(); } return current.next(); }
public KeyValue<Bytes, byte[]> next() { if (!hasNext()) { throw new NoSuchElementException(); } return currentIterator.next(); }
private static void printStateStoreKeyValueIterator(final KeyValueIterator<String, VisibilityBindingSet> rangeIt) { log.info("----------------"); while (rangeIt.hasNext()) { final KeyValue<String, VisibilityBindingSet> keyValue = rangeIt.next(); log.info(keyValue.key + " :::: " + keyValue.value); } log.info("----------------\n\n"); if (rangeIt != null) { rangeIt.close(); } } }
@Override public KeyValue<Windowed<Bytes>, byte[]> next() { final KeyValue<Bytes, byte[]> next = bytesIterator.next(); return KeyValue.pair(SessionKeySchema.from(next.key), next.value); } }
@Override public synchronized boolean hasNext() { if (!open) { throw new InvalidStateStoreException(String.format("Store %s has closed", storeName)); } if (next != null) { return true; } if (!underlying.hasNext()) { return false; } next = underlying.next(); return true; }
@Override public Map<ClusterKey, ClusterValue> getAllClusters() { Map<ClusterKey, ClusterValue> clusterKeyValueMap = new HashMap<>(); try (KeyValueIterator<ClusterKey, ClusterValue> clusterKeyValueIterator = store.all()) { log.debug("Approximate Num. of Entries in Infra Table-{}", store.approximateNumEntries()); while (clusterKeyValueIterator.hasNext()) { KeyValue<ClusterKey, ClusterValue> next = clusterKeyValueIterator.next(); clusterKeyValueMap.put(next.key, next.value); } } catch (Exception e) { throw new IllegalStateException("Infra Manager State Store not initialized ", e); } return clusterKeyValueMap; }
public static <K, V> Map<K, V> mapFromStore(KafkaStreams streams, String storeName) { final ReadOnlyKeyValueStore<K, V> store = streams.store( storeName, QueryableStoreTypes.keyValueStore()); final KeyValueIterator<K, V> all = store.all(); Map<K, V> result = new HashMap<>(); while (all.hasNext()) { KeyValue<K, V> next = all.next(); result.put(next.key, next.value); } return result; } }
public static <K, V> List<KeyValue<K, V>> listFromStore(KafkaStreams streams, String storeName) { final ReadOnlyKeyValueStore<K, V> store = streams.store( storeName, QueryableStoreTypes.keyValueStore()); final KeyValueIterator<K, V> all = store.all(); List<KeyValue<K, V>> result = new ArrayList<>(); while (all.hasNext()) { result.add(all.next()); } return result; }
@Override public KeyValue<Windowed<K>, V> next() { final KeyValue<Windowed<Bytes>, byte[]> next = iter.next(); return KeyValue.pair(windowedKey(next.key), serdes.valueFrom(next.value)); }
@Override public void punctuate(long streamTime) { KeyValueIterator<String, StockTransactionSummary> it = summaryStore.all(); long currentTime = System.currentTimeMillis(); while (it.hasNext()) { StockTransactionSummary summary = it.next().value; if (summary.updatedWithinLastMillis(currentTime, 11000)) { this.context.forward(summary.tickerSymbol, summary); } } }
@Override public KeyValue<Long, V> next() { final KeyValue<Bytes, byte[]> next = bytesIterator.next(); final long timestamp = WindowKeySchema.extractStoreTimestamp(next.key.get()); final V value = serdes.valueFrom(next.value); return KeyValue.pair(timestamp, value); }
private AGG fetchPrevious(final Bytes rawKey, final Window window) { try (final KeyValueIterator<Windowed<Bytes>, byte[]> iterator = bytesStore.findSessions(rawKey, window.start(), window.end())) { if (!iterator.hasNext()) { return null; } return serdes.valueFrom(iterator.next().value); } }
@Override public KeyValue<Windowed<K>, V> next() { final KeyValue<Bytes, byte[]> next = bytesIterator.next(); return KeyValue.pair(SessionKeySchema.from(next.key.get(), serdes.keyDeserializer(), serdes.topic()), serdes.valueFrom(next.value)); }
@Override public KeyValue<Windowed<K>, V> next() { final KeyValue<Bytes, byte[]> next = bytesIterator.next(); final long timestamp = WindowKeySchema.extractStoreTimestamp(next.key.get()); final K key = WindowKeySchema.extractStoreKey(next.key.get(), serdes); final V value = serdes.valueFrom(next.value); return KeyValue.pair( new Windowed<>(key, WindowKeySchema.timeWindowForSize(timestamp, windowSize)), value ); }
@Override public KeyValue<K, V> next() { if (!hasNext()) { throw new NoSuchElementException(); } final Bytes nextCacheKey = cacheIterator.hasNext() ? cacheIterator.peekNextKey() : null; final KS nextStoreKey = storeIterator.hasNext() ? storeIterator.peekNextKey() : null; if (nextCacheKey == null) { return nextStoreValue(nextStoreKey); } if (nextStoreKey == null) { return nextCacheValue(nextCacheKey); } final int comparison = compare(nextCacheKey, nextStoreKey); if (comparison > 0) { return nextStoreValue(nextStoreKey); } else if (comparison < 0) { return nextCacheValue(nextCacheKey); } else { // skip the same keyed element storeIterator.next(); return nextCacheValue(nextCacheKey); } }
@Override public boolean hasNext(final KeyValueIterator<Bytes, ?> iterator) { while (iterator.hasNext()) { final Bytes bytes = iterator.peekNextKey(); final Bytes keyBytes = Bytes.wrap(WindowKeySchema.extractStoreKeyBytes(bytes.get())); final long time = WindowKeySchema.extractStoreTimestamp(bytes.get()); if ((binaryKeyFrom == null || keyBytes.compareTo(binaryKeyFrom) >= 0) && (binaryKeyTo == null || keyBytes.compareTo(binaryKeyTo) <= 0) && time >= from && time <= to) { return true; } iterator.next(); } return false; } };
@Override public boolean hasNext() { // skip over items deleted from cache, and corresponding store items if they have the same key while (cacheIterator.hasNext() && isDeletedCacheEntry(cacheIterator.peekNext())) { if (storeIterator.hasNext()) { final KS nextStoreKey = storeIterator.peekNextKey(); // advance the store iterator if the key is the same as the deleted cache key if (compare(cacheIterator.peekNextKey(), nextStoreKey) == 0) { storeIterator.next(); } } cacheIterator.next(); } return cacheIterator.hasNext() || storeIterator.hasNext(); }
@Override public boolean hasNext(final KeyValueIterator<Bytes, ?> iterator) { while (iterator.hasNext()) { final Bytes bytes = iterator.peekNextKey(); final Windowed<Bytes> windowedKey = SessionKeySchema.from(bytes); if ((binaryKeyFrom == null || windowedKey.key().compareTo(binaryKeyFrom) >= 0) && (binaryKeyTo == null || windowedKey.key().compareTo(binaryKeyTo) <= 0) && windowedKey.window().end() >= from && windowedKey.window().start() <= to) { return true; } iterator.next(); } return false; } };