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

The following examples show how to use org.apache.kafka.streams.state.KeyValueIterator#close() . 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: 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 2
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 3
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 4
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 5
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();
        }
    };
}