Search in sources :

Example 81 with KeyValue

use of org.apache.kafka.streams.KeyValue in project kafka by apache.

the class ReadOnlyWindowStoreStub method fetchAll.

@Override
public KeyValueIterator<Windowed<K>, V> fetchAll(final Instant timeFrom, final Instant timeTo) {
    if (!open) {
        throw new InvalidStateStoreException("Store is not open");
    }
    final List<KeyValue<Windowed<K>, V>> results = new ArrayList<>();
    for (final long now : data.keySet()) {
        if (!(now >= timeFrom.toEpochMilli() && now <= timeTo.toEpochMilli())) {
            continue;
        }
        final NavigableMap<K, V> kvMap = data.get(now);
        if (kvMap != null) {
            for (final Entry<K, V> entry : kvMap.entrySet()) {
                results.add(new KeyValue<>(new Windowed<>(entry.getKey(), new TimeWindow(now, now + windowSize)), entry.getValue()));
            }
        }
    }
    final Iterator<KeyValue<Windowed<K>, V>> iterator = results.iterator();
    return new KeyValueIterator<Windowed<K>, V>() {

        @Override
        public void close() {
        }

        @Override
        public Windowed<K> peekNextKey() {
            throw new UnsupportedOperationException("peekNextKey() not supported in " + getClass().getName());
        }

        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public KeyValue<Windowed<K>, V> next() {
            return iterator.next();
        }
    };
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Windowed(org.apache.kafka.streams.kstream.Windowed) InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator)

Example 82 with KeyValue

use of org.apache.kafka.streams.KeyValue in project kafka by apache.

the class RocksDBStoreTest method shouldPutAll.

@Test
public void shouldPutAll() {
    final List<KeyValue<Bytes, byte[]>> entries = new ArrayList<>();
    entries.add(new KeyValue<>(new Bytes(stringSerializer.serialize(null, "1")), stringSerializer.serialize(null, "a")));
    entries.add(new KeyValue<>(new Bytes(stringSerializer.serialize(null, "2")), stringSerializer.serialize(null, "b")));
    entries.add(new KeyValue<>(new Bytes(stringSerializer.serialize(null, "3")), stringSerializer.serialize(null, "c")));
    rocksDBStore.init((StateStoreContext) context, rocksDBStore);
    rocksDBStore.putAll(entries);
    rocksDBStore.flush();
    assertEquals("a", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "1")))));
    assertEquals("b", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "2")))));
    assertEquals("c", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "3")))));
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 83 with KeyValue

use of org.apache.kafka.streams.KeyValue in project kafka by apache.

the class RocksDBStoreTest method shouldRestoreThenDeleteOnRestoreAll.

@Test
public void shouldRestoreThenDeleteOnRestoreAll() {
    final List<KeyValue<byte[], byte[]>> entries = getKeyValueEntries();
    rocksDBStore.init((StateStoreContext) context, rocksDBStore);
    context.restore(rocksDBStore.name(), entries);
    assertEquals("a", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "1")))));
    assertEquals("b", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "2")))));
    assertEquals("c", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "3")))));
    entries.clear();
    entries.add(new KeyValue<>("2".getBytes(UTF_8), "b".getBytes(UTF_8)));
    entries.add(new KeyValue<>("3".getBytes(UTF_8), "c".getBytes(UTF_8)));
    entries.add(new KeyValue<>("1".getBytes(UTF_8), null));
    context.restore(rocksDBStore.name(), entries);
    try (final KeyValueIterator<Bytes, byte[]> iterator = rocksDBStore.all()) {
        final Set<String> keys = new HashSet<>();
        while (iterator.hasNext()) {
            keys.add(stringDeserializer.deserialize(null, iterator.next().key.get()));
        }
        assertThat(keys, equalTo(Utils.mkSet("2", "3")));
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 84 with KeyValue

use of org.apache.kafka.streams.KeyValue in project kafka by apache.

the class RocksDBStoreTest method shouldNotThrowWhenRestoringOnMissingHeaders.

@Test
public void shouldNotThrowWhenRestoringOnMissingHeaders() {
    final List<KeyValue<byte[], byte[]>> entries = getChangelogRecordsWithoutHeaders();
    final Properties props = StreamsTestUtils.getStreamsConfig();
    props.put(StreamsConfig.ROCKSDB_CONFIG_SETTER_CLASS_CONFIG, MockRocksDbConfigSetter.class);
    props.put(InternalConfig.IQ_CONSISTENCY_OFFSET_VECTOR_ENABLED, true);
    dir = TestUtils.tempDirectory();
    context = new InternalMockProcessorContext<>(dir, Serdes.String(), Serdes.String(), new StreamsConfig(props));
    rocksDBStore.init((StateStoreContext) context, rocksDBStore);
    context.restore(rocksDBStore.name(), entries);
    assertThat(rocksDBStore.getPosition(), is(Position.emptyPosition()));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Properties(java.util.Properties) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 85 with KeyValue

use of org.apache.kafka.streams.KeyValue in project kafka by apache.

the class RocksDBStoreTest method shouldRestoreAll.

@Test
public void shouldRestoreAll() {
    final List<KeyValue<byte[], byte[]>> entries = getKeyValueEntries();
    rocksDBStore.init((StateStoreContext) context, rocksDBStore);
    context.restore(rocksDBStore.name(), entries);
    assertEquals("a", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "1")))));
    assertEquals("b", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "2")))));
    assertEquals("c", stringDeserializer.deserialize(null, rocksDBStore.get(new Bytes(stringSerializer.serialize(null, "3")))));
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) Test(org.junit.Test)

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