Search in sources :

Example 46 with KeyValue

use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.

the class CachingSessionStoreTest method shouldFetchAllSessionsWithSameRecordKey.

@Test
public void shouldFetchAllSessionsWithSameRecordKey() {
    final List<KeyValue<Windowed<Bytes>, byte[]>> expected = Arrays.asList(KeyValue.pair(new Windowed<>(keyA, new SessionWindow(0, 0)), "1".getBytes()), KeyValue.pair(new Windowed<>(keyA, new SessionWindow(10, 10)), "2".getBytes()), KeyValue.pair(new Windowed<>(keyA, new SessionWindow(100, 100)), "3".getBytes()), KeyValue.pair(new Windowed<>(keyA, new SessionWindow(1000, 1000)), "4".getBytes()));
    for (KeyValue<Windowed<Bytes>, byte[]> kv : expected) {
        cachingStore.put(kv.key, kv.value);
    }
    // add one that shouldn't appear in the results
    cachingStore.put(new Windowed<>(keyAA, new SessionWindow(0, 0)), "5".getBytes());
    final List<KeyValue<Windowed<Bytes>, byte[]>> results = toList(cachingStore.fetch(keyA));
    verifyKeyValueList(expected, results);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) StreamsTestUtils.verifyWindowedKeyValue(org.apache.kafka.test.StreamsTestUtils.verifyWindowedKeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 47 with KeyValue

use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.

the class CompositeReadOnlyWindowStoreTest method shouldFetchKeyRangeAcrossStores.

@Test
public void shouldFetchKeyRangeAcrossStores() {
    final ReadOnlyWindowStoreStub<String, String> secondUnderlying = new ReadOnlyWindowStoreStub<>(WINDOW_SIZE);
    stubProviderTwo.addStore(storeName, secondUnderlying);
    underlyingWindowStore.put("a", "a", 0L);
    secondUnderlying.put("b", "b", 10L);
    List<KeyValue<Windowed<String>, String>> results = StreamsTestUtils.toList(windowStore.fetch("a", "b", 0, 10));
    assertThat(results, equalTo(Arrays.asList(KeyValue.pair(new Windowed<>("a", new TimeWindow(0, WINDOW_SIZE)), "a"), KeyValue.pair(new Windowed<>("b", new TimeWindow(10, 10 + WINDOW_SIZE)), "b"))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Test(org.junit.Test)

Example 48 with KeyValue

use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.

the class CompositeReadOnlyWindowStoreTest method shouldGetAllAcrossStores.

@Test
public void shouldGetAllAcrossStores() {
    final ReadOnlyWindowStoreStub<String, String> secondUnderlying = new ReadOnlyWindowStoreStub<>(WINDOW_SIZE);
    stubProviderTwo.addStore(storeName, secondUnderlying);
    underlyingWindowStore.put("a", "a", 0L);
    secondUnderlying.put("b", "b", 10L);
    List<KeyValue<Windowed<String>, String>> results = StreamsTestUtils.toList(windowStore.all());
    assertThat(results, equalTo(Arrays.asList(KeyValue.pair(new Windowed<>("a", new TimeWindow(0, WINDOW_SIZE)), "a"), KeyValue.pair(new Windowed<>("b", new TimeWindow(10, 10 + WINDOW_SIZE)), "b"))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Test(org.junit.Test)

Example 49 with KeyValue

use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManager method updateStandbyStates.

List<ConsumerRecord<byte[], byte[]>> updateStandbyStates(final TopicPartition storePartition, final List<ConsumerRecord<byte[], byte[]>> records) {
    final long limit = offsetLimit(storePartition);
    List<ConsumerRecord<byte[], byte[]>> remainingRecords = null;
    final List<KeyValue<byte[], byte[]>> restoreRecords = new ArrayList<>();
    // restore states from changelog records
    final BatchingStateRestoreCallback restoreCallback = getBatchingRestoreCallback(restoreCallbacks.get(storePartition.topic()));
    long lastOffset = -1L;
    int count = 0;
    for (final ConsumerRecord<byte[], byte[]> record : records) {
        if (record.offset() < limit) {
            restoreRecords.add(KeyValue.pair(record.key(), record.value()));
            lastOffset = record.offset();
        } else {
            if (remainingRecords == null) {
                remainingRecords = new ArrayList<>(records.size() - count);
            }
            remainingRecords.add(record);
        }
        count++;
    }
    if (!restoreRecords.isEmpty()) {
        try {
            restoreCallback.restoreAll(restoreRecords);
        } catch (final Exception e) {
            throw new ProcessorStateException(String.format("%sException caught while trying to restore state from %s", logPrefix, storePartition), e);
        }
    }
    // record the restored offset for its change log partition
    restoredOffsets.put(storePartition, lastOffset + 1);
    return remainingRecords;
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) BatchingStateRestoreCallback(org.apache.kafka.streams.processor.BatchingStateRestoreCallback) ArrayList(java.util.ArrayList) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) OffsetCheckpoint(org.apache.kafka.streams.state.internals.OffsetCheckpoint) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) IOException(java.io.IOException)

Example 50 with KeyValue

use of org.apache.kafka.streams.KeyValue in project incubator-rya by apache.

the class KeyValueJoinStateStore method store.

@Override
public void store(final BinaryResult result) {
    requireNonNull(result);
    // The join key prefix is an ordered list of values from the binding set that match the join variables.
    // This is a prefix for every row that holds values for a specific set of join variable values.
    final Side side = result.getSide();
    final VisibilityBindingSet bs = result.getResult();
    final String joinKeyPrefix = makeCommaDelimitedValues(side, joinVars, bs);
    final List<KeyValue<String, VisibilityBindingSet>> values = new ArrayList<>();
    // For each join variable set, we need a start key for scanning,
    final String startKey = joinKeyPrefix + START_RANGE_SUFFIX;
    values.add(new KeyValue<>(startKey, RANGE_MARKER_VALUE));
    // The actual value that was emitted as a result.
    final String valueKey = makeCommaDelimitedValues(side, allVars, bs);
    values.add(new KeyValue<>(valueKey, bs));
    // And the end key for scanning.
    final String endKey = joinKeyPrefix + END_RANGE_SUFFIX;
    values.add(new KeyValue<>(endKey, RANGE_MARKER_VALUE));
    // Write the pairs to the store.
    log.debug("\nStoring the following values: {}\n", values);
    store.putAll(values);
}
Also used : Side(org.apache.rya.streams.kafka.processors.ProcessorResult.BinaryResult.Side) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList)

Aggregations

KeyValue (org.apache.kafka.streams.KeyValue)343 Test (org.junit.Test)268 Properties (java.util.Properties)127 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)123 Windowed (org.apache.kafka.streams.kstream.Windowed)105 ArrayList (java.util.ArrayList)90 KafkaStreams (org.apache.kafka.streams.KafkaStreams)82 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)74 Bytes (org.apache.kafka.common.utils.Bytes)74 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)68 IntegrationTest (org.apache.kafka.test.IntegrationTest)66 Serdes (org.apache.kafka.common.serialization.Serdes)65 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)62 StreamsConfig (org.apache.kafka.streams.StreamsConfig)55 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)53 KStream (org.apache.kafka.streams.kstream.KStream)52 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)46 KTable (org.apache.kafka.streams.kstream.KTable)43 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)42 Consumed (org.apache.kafka.streams.kstream.Consumed)41