Java Code Examples for org.apache.kafka.streams.state.KeyValueIterator#hasNext()

The following examples show how to use org.apache.kafka.streams.state.KeyValueIterator#hasNext() . 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: StockPerformancePunctuator.java    From kafka-streams-in-action with Apache License 2.0 6 votes vote down vote up
@Override
public void punctuate(long timestamp) {
    KeyValueIterator<String, StockPerformance> performanceIterator = keyValueStore.all();

    while (performanceIterator.hasNext()) {
        KeyValue<String, StockPerformance> keyValue = performanceIterator.next();
        String key = keyValue.key;
        StockPerformance stockPerformance = keyValue.value;

        if (stockPerformance != null) {
            if (stockPerformance.priceDifferential() >= differentialThreshold ||
                    stockPerformance.volumeDifferential() >= differentialThreshold) {
                context.forward(key, stockPerformance);
            }
        }
    }
}
 
Example 2
Source File: CogroupingMethodHandleProcessor.java    From kafka-streams-in-action with Apache License 2.0 6 votes vote down vote up
public void cogroup(long timestamp) {
    KeyValueIterator<String, Tuple<List<ClickEvent>, List<StockTransaction>>> iterator = tupleStore.all();

    while (iterator.hasNext()) {
        KeyValue<String, Tuple<List<ClickEvent>, List<StockTransaction>>> cogrouping = iterator.next();

        if (cogrouping.value != null && (!cogrouping.value._1.isEmpty() || !cogrouping.value._2.isEmpty())) {
            List<ClickEvent> clickEvents = new ArrayList<>(cogrouping.value._1);
            List<StockTransaction> stockTransactions = new ArrayList<>(cogrouping.value._2);

            context().forward(cogrouping.key, Tuple.of(clickEvents, stockTransactions));
            cogrouping.value._1.clear();
            cogrouping.value._2.clear();
            tupleStore.put(cogrouping.key, cogrouping.value);
        }
    }
    iterator.close();
}
 
Example 3
Source File: CogroupingPunctuator.java    From kafka-streams-in-action with Apache License 2.0 6 votes vote down vote up
@Override
public void punctuate(long timestamp) {
    KeyValueIterator<String, Tuple<List<ClickEvent>, List<StockTransaction>>> iterator = tupleStore.all();

    while (iterator.hasNext()) {
        KeyValue<String, Tuple<List<ClickEvent>, List<StockTransaction>>> cogrouped = iterator.next();
        // if either list contains values forward results
        if (cogrouped.value != null && (!cogrouped.value._1.isEmpty() || !cogrouped.value._2.isEmpty())) {
            List<ClickEvent> clickEvents = new ArrayList<>(cogrouped.value._1);
            List<StockTransaction> stockTransactions = new ArrayList<>(cogrouped.value._2);

            context.forward(cogrouped.key, Tuple.of(clickEvents, stockTransactions));
            // empty out the current cogrouped results
            cogrouped.value._1.clear();
            cogrouped.value._2.clear();
            tupleStore.put(cogrouped.key, cogrouped.value);
        }
    }
    iterator.close();
}
 
Example 4
Source File: StockPerformanceMultipleValuesTransformer.java    From kafka-streams-in-action with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public KeyValue<String, List<KeyValue<String, StockPerformance>>> punctuate(long timestamp) {
    List<KeyValue<String, StockPerformance>> stockPerformanceList = new ArrayList<>();
    KeyValueIterator<String, StockPerformance> performanceIterator = keyValueStore.all();
    while (performanceIterator.hasNext()) {
        KeyValue<String, StockPerformance> keyValue = performanceIterator.next();
        StockPerformance stockPerformance = keyValue.value;

        if (stockPerformance != null) {
            if (stockPerformance.priceDifferential() >= differentialThreshold ||
                    stockPerformance.volumeDifferential() >= differentialThreshold) {
                stockPerformanceList.add(keyValue);
            }
        }
    }
    return stockPerformanceList.isEmpty() ? null : KeyValue.pair(null, stockPerformanceList);
}
 
Example 5
Source File: StockPerformanceMetricsTransformer.java    From kafka-streams-in-action with Apache License 2.0 6 votes vote down vote up
private void doPunctuate(long timestamp) {
    KeyValueIterator<String, StockPerformance> performanceIterator = keyValueStore.all();

    while (performanceIterator.hasNext()) {
        KeyValue<String, StockPerformance> keyValue = performanceIterator.next();
        String key = keyValue.key;
        StockPerformance stockPerformance = keyValue.value;

        if (stockPerformance != null) {
            if (stockPerformance.priceDifferential() >= differentialThreshold ||
                    stockPerformance.volumeDifferential() >= differentialThreshold) {
                processorContext.forward(key, stockPerformance);
            }
        }
    }
}
 
Example 6
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 7
Source File: MonitorProcessorSupplier.java    From DBus with Apache License 2.0 5 votes vote down vote up
@Override
public void punctuate(long timestamp) {
    CuratorFramework curator = CuratorContainer.getInstance().getCurator();
    KeyValueIterator<String, String> iter = store.all();
    while (iter.hasNext()) {
        KeyValue<String, String> entry = iter.next();
        try {
            curator.setData().forPath(entry.key, entry.value.getBytes());
        } catch (Exception e) {
        }
    }
}
 
Example 8
Source File: CustomerStore.java    From cqrs-manager-for-distributed-reactive-services with Apache License 2.0 5 votes vote down vote up
public List<Customer> getCustomers() {
    List<Customer> customers = new ArrayList<>();
    KeyValueIterator<UUID, Map> iterator = store.all();
    while (iterator.hasNext()) {
        KeyValue<UUID, Map> entry = iterator.next();
        logger.debug("getCustomers iterator entry: {}", entry);
        customers.add(new Customer(entry.value));
    }
    iterator.close();
    return customers;
}
 
Example 9
Source File: StockSummaryProcessor.java    From kafka-streams with Apache License 2.0 5 votes vote down vote up
@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);
        }
    }
}
 
Example 10
Source File: KeyValueJoinStateStore.java    From rya with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 11
Source File: KeyValueStoreGrpcImplLocalDispatcher.java    From apicurio-registry with Apache License 2.0 4 votes vote down vote up
private <K, V> void drainToKeyValue(String storeName, KeyValueIterator<K, V> iter, StreamObserver<io.apicurio.registry.streams.distore.proto.KeyValue> responseObserver) {
    while (iter.hasNext()) {
        KeyValue<K, V> wkv = iter.next();
        drainToKeyValue(storeName, wkv, responseObserver);
    }
}
 
Example 12
Source File: KeyValueJoinStateStore.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public CloseableIterator<VisibilityBindingSet> getJoinedValues(final BinaryResult result) {
    requireNonNull(result);

    // Get an iterator over the values that start with the join variables for the other side.
    final Side otherSide = result.getSide() == Side.LEFT ? Side.RIGHT : Side.LEFT;
    final VisibilityBindingSet bs = result.getResult();
    final String joinKeyPrefix = makeCommaDelimitedValues(otherSide, joinVars, bs, joinVars.size());

    final String startKey = joinKeyPrefix + START_RANGE_SUFFIX;
    final String endKey = joinKeyPrefix + END_RANGE_SUFFIX;

    final KeyValueIterator<String, VisibilityBindingSet> rangeIt = store.range(startKey, endKey);

    // Return a CloseableIterator over the range's value fields, skipping the start and end entry.
    return new CloseableIterator<VisibilityBindingSet>() {

        private Optional<VisibilityBindingSet> next = null;

        @Override
        public boolean hasNext() {
            // If the iterator has not been initialized yet, read a value in.
            if(next == null) {
                next = readNext();
            }

            // Return true if there is a next value, otherwise false.
            return next.isPresent();
        }

        @Override
        public VisibilityBindingSet next() {
            // If the iterator has not been initialized yet, read a value in.
            if(next == null) {
                next = readNext();
            }

            // It's illegal to call next() when there is no next value.
            if(!next.isPresent()) {
                throw new IllegalStateException("May not invoke next() when there is nothing left in the Iterator.");
            }

            // Update and return the next value.
            final VisibilityBindingSet ret = next.get();
            log.debug("\nReturning: {}", ret);
            next = readNext();
            return ret;
        }

        private Optional<VisibilityBindingSet> readNext() {
            // Check to see if there's anything left in the iterator.
            if(!rangeIt.hasNext()) {
                return Optional.empty();
            }

            // Read a candidate key/value pair from the iterator.
            KeyValue<String, VisibilityBindingSet> candidate = rangeIt.next();

            // If we are initializing, then the first thing we must read is a start of range marker.
            if(next == null) {
                if(!candidate.key.endsWith(START_RANGE_SUFFIX)) {
                    throw new IllegalStateException("The first key encountered must be a start of range key.");
                }
                log.debug("Read the start of range markers.\n");

                // Read a new candidate to skip this one.
                if(!rangeIt.hasNext()) {
                    throw new IllegalStateException("There must be another entry after the start of range key.");
                }
                candidate = rangeIt.next();
            }

            // If that value is an end of range key, then we are finished. Otherwise, return it.
            else if(candidate.key.endsWith(END_RANGE_SUFFIX)) {
                log.debug("Read the end of range marker.\n");

                // If there are more messages, that's a problem.
                if(rangeIt.hasNext()) {
                    throw new IllegalStateException("The end of range marker must be the last key in the iterator.");
                }

                return Optional.empty();
            }

            // Otherwise we found a new value.
            return Optional.of( candidate.value );
        }

        @Override
        public void close() throws Exception {
            rangeIt.close();
        }
    };
}