private List<KeyValue<Windowed<String>, Long>> fetchSessionsFromLocalStore(final String userId, final ReadOnlySessionStore<String, Long> playEventsPerSession) { final List<KeyValue<Windowed<String>, Long>> results = new ArrayList<>(); try (final KeyValueIterator<Windowed<String>, Long> iterator = playEventsPerSession.fetch(userId)) { iterator.forEachRemaining(results::add); } return results; }
/** * 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 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 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() { return iter.hasNext(); }
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 KeyValueIterator<Windowed<K>, V> fetch(final K key) { Objects.requireNonNull(key, "key can't be null"); final List<ReadOnlySessionStore<K, V>> stores = storeProvider.stores(storeName, queryableStoreType); for (final ReadOnlySessionStore<K, V> store : stores) { try { final KeyValueIterator<Windowed<K>, V> result = store.fetch(key); if (!result.hasNext()) { result.close(); } else { return result; } } catch (final InvalidStateStoreException ise) { throw new InvalidStateStoreException("State store [" + storeName + "] is not available anymore" + " and may have been migrated to another instance; " + "please re-discover its location from the state metadata."); } } return KeyValueIterators.emptyIterator(); }
@Override public void close() throws Exception { rangeIt.close(); } };
@Override public Windowed<K> peekNextKey() { return windowedKey(iter.peekNextKey()); } }
@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() { return bytesIterator.hasNext(); }
@Override public KeyValue<K, V> next() { if (!hasNext()) { throw new NoSuchElementException(); } return current.next(); }
@Override public void close() { if (current != null) { current.close(); current = null; } }
@Override public Bytes peekNextKey() { if (!hasNext()) { throw new NoSuchElementException(); } return currentIterator.peekNextKey(); }
@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; } };
/** Saves the `lastAvgs` map to the state store. * * {@inheritDoc} */ @Override public void punctuate(long timestamp) { // Print the contents of the state store. state.all().forEachRemaining( kv -> System.out.println(System.currentTimeMillis() + " PUNCTUATE: " + kv.key + ", " + kv.value)); // Foward the new average. state.all().forEachRemaining( kv -> context.forward(kv.key, kv.value, "FAST-sink")); } // Close punctuate.
@Override public boolean hasNext() { return bytesIterator.hasNext(); }
public KeyValue<Bytes, byte[]> next() { if (!hasNext()) { throw new NoSuchElementException(); } return currentIterator.next(); }
@Override public void close() { bytesIterator.close(); } }