Search in sources :

Example 86 with KeyValueStore

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

the class KeyValueStoreMaterializerTest method shouldCreateBuilderThatBuildsStoreWithCachingDisabled.

@Test
public void shouldCreateBuilderThatBuildsStoreWithCachingDisabled() {
    final MaterializedInternal<String, String, KeyValueStore<Bytes, byte[]>> materialized = new MaterializedInternal<>(Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("store").withCachingDisabled(), nameProvider, storePrefix);
    final KeyValueStoreMaterializer<String, String> materializer = new KeyValueStoreMaterializer<>(materialized);
    final StoreBuilder<KeyValueStore<String, String>> builder = materializer.materialize();
    final KeyValueStore<String, String> store = builder.build();
    final WrappedStateStore logging = (WrappedStateStore) ((WrappedStateStore) store).wrappedStore();
    assertThat(logging, instanceOf(ChangeLoggingKeyValueBytesStore.class));
}
Also used : WrappedStateStore(org.apache.kafka.streams.state.internals.WrappedStateStore) ChangeLoggingKeyValueBytesStore(org.apache.kafka.streams.state.internals.ChangeLoggingKeyValueBytesStore) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) MaterializedInternal(org.apache.kafka.streams.kstream.internals.MaterializedInternal) KeyValueStoreMaterializer(org.apache.kafka.streams.kstream.internals.KeyValueStoreMaterializer) Test(org.junit.Test)

Example 87 with KeyValueStore

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

the class KeyValueStoreMaterializerTest method shouldCreateBuilderThatBuildsMeteredStoreWithCachingAndLoggingEnabled.

@Test
public void shouldCreateBuilderThatBuildsMeteredStoreWithCachingAndLoggingEnabled() {
    final MaterializedInternal<String, String, KeyValueStore<Bytes, byte[]>> materialized = new MaterializedInternal<>(Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("store"), nameProvider, storePrefix);
    final KeyValueStoreMaterializer<String, String> materializer = new KeyValueStoreMaterializer<>(materialized);
    final StoreBuilder<KeyValueStore<String, String>> builder = materializer.materialize();
    final KeyValueStore<String, String> store = builder.build();
    final WrappedStateStore caching = (WrappedStateStore) ((WrappedStateStore) store).wrappedStore();
    final StateStore logging = caching.wrappedStore();
    assertThat(store, instanceOf(MeteredKeyValueBytesStore.class));
    assertThat(caching, instanceOf(CachedStateStore.class));
    assertThat(logging, instanceOf(ChangeLoggingKeyValueBytesStore.class));
}
Also used : WrappedStateStore(org.apache.kafka.streams.state.internals.WrappedStateStore) MeteredKeyValueBytesStore(org.apache.kafka.streams.state.internals.MeteredKeyValueBytesStore) CachedStateStore(org.apache.kafka.streams.state.internals.CachedStateStore) WrappedStateStore(org.apache.kafka.streams.state.internals.WrappedStateStore) CachedStateStore(org.apache.kafka.streams.state.internals.CachedStateStore) StateStore(org.apache.kafka.streams.processor.StateStore) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) Bytes(org.apache.kafka.common.utils.Bytes) ChangeLoggingKeyValueBytesStore(org.apache.kafka.streams.state.internals.ChangeLoggingKeyValueBytesStore) MaterializedInternal(org.apache.kafka.streams.kstream.internals.MaterializedInternal) KeyValueStoreMaterializer(org.apache.kafka.streams.kstream.internals.KeyValueStoreMaterializer) Test(org.junit.Test)

Example 88 with KeyValueStore

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

the class KeyValueStoreMaterializerTest method shouldCreateBuilderThatBuildsStoreWithCachingAndLoggingDisabled.

@Test
public void shouldCreateBuilderThatBuildsStoreWithCachingAndLoggingDisabled() {
    final MaterializedInternal<String, String, KeyValueStore<Bytes, byte[]>> materialized = new MaterializedInternal<>(Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("store").withCachingDisabled().withLoggingDisabled(), nameProvider, storePrefix);
    final KeyValueStoreMaterializer<String, String> materializer = new KeyValueStoreMaterializer<>(materialized);
    final StoreBuilder<KeyValueStore<String, String>> builder = materializer.materialize();
    final KeyValueStore<String, String> store = builder.build();
    final StateStore wrapped = ((WrappedStateStore) store).wrappedStore();
    assertThat(wrapped, not(instanceOf(CachedStateStore.class)));
    assertThat(wrapped, not(instanceOf(ChangeLoggingKeyValueBytesStore.class)));
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) WrappedStateStore(org.apache.kafka.streams.state.internals.WrappedStateStore) WrappedStateStore(org.apache.kafka.streams.state.internals.WrappedStateStore) CachedStateStore(org.apache.kafka.streams.state.internals.CachedStateStore) StateStore(org.apache.kafka.streams.processor.StateStore) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) MaterializedInternal(org.apache.kafka.streams.kstream.internals.MaterializedInternal) KeyValueStoreMaterializer(org.apache.kafka.streams.kstream.internals.KeyValueStoreMaterializer) Test(org.junit.Test)

Example 89 with KeyValueStore

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

the class KTableMapValuesTest method testQueryableKTable.

@Test
public void testQueryableKTable() {
    final StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    KTable<String, String> table1 = builder.table(topic1, consumed);
    KTable<String, Integer> table2 = table1.mapValues(new ValueMapper<CharSequence, Integer>() {

        @Override
        public Integer apply(CharSequence value) {
            return value.charAt(0) - 48;
        }
    }, Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>as("anyName").withValueSerde(Serdes.Integer()));
    MockProcessorSupplier<String, Integer> proc2 = new MockProcessorSupplier<>();
    table2.toStream().process(proc2);
    doTestKTable(builder, topic1, proc2);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Test(org.junit.Test)

Example 90 with KeyValueStore

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

the class CachingKeyValueStoreTest method createKeyValueStore.

@SuppressWarnings("unchecked")
@Override
protected <K, V> KeyValueStore<K, V> createKeyValueStore(final ProcessorContext context) {
    final StoreBuilder storeBuilder = Stores.keyValueStoreBuilder(Stores.persistentKeyValueStore("cache-store"), (Serde<K>) context.keySerde(), (Serde<V>) context.valueSerde()).withCachingEnabled();
    final KeyValueStore<K, V> store = (KeyValueStore<K, V>) storeBuilder.build();
    final CacheFlushListenerStub<K, V> cacheFlushListener = new CacheFlushListenerStub<>();
    final CachedStateStore inner = (CachedStateStore) ((WrappedStateStore) store).wrappedStore();
    inner.setFlushListener(cacheFlushListener, false);
    store.init(context, store);
    return store;
}
Also used : Serde(org.apache.kafka.common.serialization.Serde) StoreBuilder(org.apache.kafka.streams.state.StoreBuilder) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore)

Aggregations

KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)133 Test (org.junit.Test)101 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)54 KeyValue (org.apache.kafka.streams.KeyValue)49 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)47 Properties (java.util.Properties)37 Bytes (org.apache.kafka.common.utils.Bytes)36 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)32 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)29 KafkaStreams (org.apache.kafka.streams.KafkaStreams)28 Serdes (org.apache.kafka.common.serialization.Serdes)26 Materialized (org.apache.kafka.streams.kstream.Materialized)25 StreamsConfig (org.apache.kafka.streams.StreamsConfig)24 IntegrationTest (org.apache.kafka.test.IntegrationTest)21 KTable (org.apache.kafka.streams.kstream.KTable)20 Consumed (org.apache.kafka.streams.kstream.Consumed)19 StateStore (org.apache.kafka.streams.processor.StateStore)17 ReadOnlyKeyValueStore (org.apache.kafka.streams.state.ReadOnlyKeyValueStore)17 TestUtils (org.apache.kafka.test.TestUtils)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16