Search in sources :

Example 11 with KeyValueBytesStoreSupplier

use of org.apache.kafka.streams.state.KeyValueBytesStoreSupplier in project kafka by apache.

the class MockProcessorContextStateStoreTest method parameters.

public static Stream<Arguments> parameters() {
    final List<Boolean> booleans = asList(true, false);
    final List<Arguments> values = new ArrayList<>();
    for (final Boolean timestamped : booleans) {
        for (final Boolean caching : booleans) {
            for (final Boolean logging : booleans) {
                final List<KeyValueBytesStoreSupplier> keyValueBytesStoreSuppliers = asList(Stores.inMemoryKeyValueStore("kv" + timestamped + caching + logging), Stores.persistentKeyValueStore("kv" + timestamped + caching + logging), Stores.persistentTimestampedKeyValueStore("kv" + timestamped + caching + logging));
                for (final KeyValueBytesStoreSupplier supplier : keyValueBytesStoreSuppliers) {
                    final StoreBuilder<? extends KeyValueStore<String, ?>> builder;
                    if (timestamped) {
                        builder = Stores.timestampedKeyValueStoreBuilder(supplier, Serdes.String(), Serdes.Long());
                    } else {
                        builder = Stores.keyValueStoreBuilder(supplier, Serdes.String(), Serdes.Long());
                    }
                    if (caching) {
                        builder.withCachingEnabled();
                    } else {
                        builder.withCachingDisabled();
                    }
                    if (logging) {
                        builder.withLoggingEnabled(Collections.emptyMap());
                    } else {
                        builder.withLoggingDisabled();
                    }
                    values.add(Arguments.of(builder, timestamped, caching, logging));
                }
            }
        }
    }
    for (final Boolean timestamped : booleans) {
        for (final Boolean caching : booleans) {
            for (final Boolean logging : booleans) {
                final List<WindowBytesStoreSupplier> windowBytesStoreSuppliers = asList(Stores.inMemoryWindowStore("w" + timestamped + caching + logging, Duration.ofSeconds(1), Duration.ofSeconds(1), false), Stores.persistentWindowStore("w" + timestamped + caching + logging, Duration.ofSeconds(1), Duration.ofSeconds(1), false), Stores.persistentTimestampedWindowStore("w" + timestamped + caching + logging, Duration.ofSeconds(1), Duration.ofSeconds(1), false));
                for (final WindowBytesStoreSupplier supplier : windowBytesStoreSuppliers) {
                    final StoreBuilder<? extends WindowStore<String, ?>> builder;
                    if (timestamped) {
                        builder = Stores.timestampedWindowStoreBuilder(supplier, Serdes.String(), Serdes.Long());
                    } else {
                        builder = Stores.windowStoreBuilder(supplier, Serdes.String(), Serdes.Long());
                    }
                    if (caching) {
                        builder.withCachingEnabled();
                    } else {
                        builder.withCachingDisabled();
                    }
                    if (logging) {
                        builder.withLoggingEnabled(Collections.emptyMap());
                    } else {
                        builder.withLoggingDisabled();
                    }
                    values.add(Arguments.of(builder, timestamped, caching, logging));
                }
            }
        }
    }
    for (final Boolean caching : booleans) {
        for (final Boolean logging : booleans) {
            final List<SessionBytesStoreSupplier> sessionBytesStoreSuppliers = asList(Stores.inMemorySessionStore("s" + caching + logging, Duration.ofSeconds(1)), Stores.persistentSessionStore("s" + caching + logging, Duration.ofSeconds(1)));
            for (final SessionBytesStoreSupplier supplier : sessionBytesStoreSuppliers) {
                final StoreBuilder<? extends SessionStore<String, ?>> builder = Stores.sessionStoreBuilder(supplier, Serdes.String(), Serdes.Long());
                if (caching) {
                    builder.withCachingEnabled();
                } else {
                    builder.withCachingDisabled();
                }
                if (logging) {
                    builder.withLoggingEnabled(Collections.emptyMap());
                } else {
                    builder.withLoggingDisabled();
                }
                values.add(Arguments.of(builder, false, caching, logging));
            }
        }
    }
    return values.stream();
}
Also used : WindowBytesStoreSupplier(org.apache.kafka.streams.state.WindowBytesStoreSupplier) SessionBytesStoreSupplier(org.apache.kafka.streams.state.SessionBytesStoreSupplier) Arguments(org.junit.jupiter.params.provider.Arguments) ArrayList(java.util.ArrayList) KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier)

Example 12 with KeyValueBytesStoreSupplier

use of org.apache.kafka.streams.state.KeyValueBytesStoreSupplier in project kafkastreams-cep by fhussonnois.

the class QueryStoreBuilders method getAggregateStateStores.

/**
 * Build all {@link StoreBuilder} used to store aggregate states for stages.
 *
 * @return a new collection of {@link StoreBuilder}.
 */
public StoreBuilder<AggregatesStore<K>> getAggregateStateStores() {
    final String storeName = QueryStores.getQueryAggregateStatesStoreName(queryName);
    KeyValueBytesStoreSupplier storeSupplier = Stores.persistentKeyValueStore(storeName);
    return QueryStores.aggregatesStoreBuilder(storeSupplier);
}
Also used : KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier)

Example 13 with KeyValueBytesStoreSupplier

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

the class KeyValueStoreMaterializer method materialize.

/**
 * @return  StoreBuilder
 */
public StoreBuilder<KeyValueStore<K, V>> materialize() {
    KeyValueBytesStoreSupplier supplier = (KeyValueBytesStoreSupplier) materialized.storeSupplier();
    if (supplier == null) {
        final String name = materialized.storeName();
        supplier = Stores.persistentKeyValueStore(name);
    }
    final StoreBuilder<KeyValueStore<K, V>> builder = Stores.keyValueStoreBuilder(supplier, materialized.keySerde(), materialized.valueSerde());
    if (materialized.loggingEnabled()) {
        builder.withLoggingEnabled(materialized.logConfig());
    } else {
        builder.withLoggingDisabled();
    }
    if (materialized.cachingEnabled()) {
        builder.withCachingEnabled();
    }
    return builder;
}
Also used : KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore)

Example 14 with KeyValueBytesStoreSupplier

use of org.apache.kafka.streams.state.KeyValueBytesStoreSupplier in project kafka by apache.

the class KTableEfficientRangeQueryTest method getStoreConfig.

private Materialized<String, String, KeyValueStore<Bytes, byte[]>> getStoreConfig(final StoreType type, final String name, final boolean cachingEnabled, final boolean loggingEnabled) {
    final Supplier<KeyValueBytesStoreSupplier> createStore = () -> {
        if (type == StoreType.InMemory) {
            return Stores.inMemoryKeyValueStore(TABLE_NAME);
        } else if (type == StoreType.RocksDB) {
            return Stores.persistentKeyValueStore(TABLE_NAME);
        } else if (type == StoreType.Timed) {
            return Stores.persistentTimestampedKeyValueStore(TABLE_NAME);
        } else {
            return Stores.inMemoryKeyValueStore(TABLE_NAME);
        }
    };
    final KeyValueBytesStoreSupplier stateStoreSupplier = createStore.get();
    final Materialized<String, String, KeyValueStore<Bytes, byte[]>> stateStoreConfig = Materialized.<String, String>as(stateStoreSupplier).withKeySerde(Serdes.String()).withValueSerde(Serdes.String());
    if (cachingEnabled) {
        stateStoreConfig.withCachingEnabled();
    } else {
        stateStoreConfig.withCachingDisabled();
    }
    if (loggingEnabled) {
        stateStoreConfig.withLoggingEnabled(new HashMap<String, String>());
    } else {
        stateStoreConfig.withLoggingDisabled();
    }
    return stateStoreConfig;
}
Also used : KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) ReadOnlyKeyValueStore(org.apache.kafka.streams.state.ReadOnlyKeyValueStore)

Example 15 with KeyValueBytesStoreSupplier

use of org.apache.kafka.streams.state.KeyValueBytesStoreSupplier in project kafka by apache.

the class IQv2IntegrationTest method shouldNotRequireQueryHandler.

@Test
public void shouldNotRequireQueryHandler() {
    final KeyQuery<Integer, ValueAndTimestamp<Integer>> query = KeyQuery.withKey(1);
    final int partition = 1;
    final Set<Integer> partitions = singleton(partition);
    final StateQueryRequest<ValueAndTimestamp<Integer>> request = inStore(STORE_NAME).withQuery(query).withPartitions(partitions);
    final StreamsBuilder builder = new StreamsBuilder();
    builder.table(INPUT_TOPIC_NAME, Consumed.with(Serdes.Integer(), Serdes.Integer()), Materialized.as(new KeyValueBytesStoreSupplier() {

        @Override
        public String name() {
            return STORE_NAME;
        }

        @Override
        public KeyValueStore<Bytes, byte[]> get() {
            return new KeyValueStore<Bytes, byte[]>() {

                private boolean open = false;

                private Map<Bytes, byte[]> map = new HashMap<>();

                private Position position;

                private StateStoreContext context;

                @Override
                public void put(final Bytes key, final byte[] value) {
                    map.put(key, value);
                    StoreQueryUtils.updatePosition(position, context);
                }

                @Override
                public byte[] putIfAbsent(final Bytes key, final byte[] value) {
                    StoreQueryUtils.updatePosition(position, context);
                    return map.putIfAbsent(key, value);
                }

                @Override
                public void putAll(final List<KeyValue<Bytes, byte[]>> entries) {
                    StoreQueryUtils.updatePosition(position, context);
                    for (final KeyValue<Bytes, byte[]> entry : entries) {
                        map.put(entry.key, entry.value);
                    }
                }

                @Override
                public byte[] delete(final Bytes key) {
                    StoreQueryUtils.updatePosition(position, context);
                    return map.remove(key);
                }

                @Override
                public String name() {
                    return STORE_NAME;
                }

                @Deprecated
                @Override
                public void init(final ProcessorContext context, final StateStore root) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public void init(final StateStoreContext context, final StateStore root) {
                    context.register(root, (key, value) -> put(Bytes.wrap(key), value));
                    this.open = true;
                    this.position = Position.emptyPosition();
                    this.context = context;
                }

                @Override
                public void flush() {
                }

                @Override
                public void close() {
                    this.open = false;
                    map.clear();
                }

                @Override
                public boolean persistent() {
                    return false;
                }

                @Override
                public boolean isOpen() {
                    return open;
                }

                @Override
                public Position getPosition() {
                    return position;
                }

                @Override
                public byte[] get(final Bytes key) {
                    return map.get(key);
                }

                @Override
                public KeyValueIterator<Bytes, byte[]> range(final Bytes from, final Bytes to) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public KeyValueIterator<Bytes, byte[]> all() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public long approximateNumEntries() {
                    return map.size();
                }
            };
        }

        @Override
        public String metricsScope() {
            return "nonquery";
        }
    }));
    kafkaStreams = new KafkaStreams(builder.build(), streamsConfiguration());
    kafkaStreams.cleanUp();
    kafkaStreams.start();
    final StateQueryResult<ValueAndTimestamp<Integer>> result = IntegrationTestUtils.iqv2WaitForResult(kafkaStreams, request);
    final QueryResult<ValueAndTimestamp<Integer>> queryResult = result.getPartitionResults().get(partition);
    assertThat(queryResult.isFailure(), is(true));
    assertThat(queryResult.getFailureReason(), is(FailureReason.UNKNOWN_QUERY_TYPE));
    assertThat(queryResult.getFailureMessage(), matchesPattern("This store (.*) doesn't know how to execute the given query (.*)." + " Contact the store maintainer if you need support for a new query type."));
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) Position(org.apache.kafka.streams.query.Position) StateStore(org.apache.kafka.streams.processor.StateStore) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) StateStoreContext(org.apache.kafka.streams.processor.StateStoreContext) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Bytes(org.apache.kafka.common.utils.Bytes) KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Aggregations

KeyValueBytesStoreSupplier (org.apache.kafka.streams.state.KeyValueBytesStoreSupplier)20 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)8 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)7 KafkaStreams (org.apache.kafka.streams.KafkaStreams)5 Test (org.junit.Test)5 QueriedInternal (com.github.fhuss.kafka.streams.cep.state.internal.QueriedInternal)3 Properties (java.util.Properties)3 SessionBytesStoreSupplier (org.apache.kafka.streams.state.SessionBytesStoreSupplier)3 WindowBytesStoreSupplier (org.apache.kafka.streams.state.WindowBytesStoreSupplier)3 InMemoryKeyValueStore (org.apache.kafka.streams.state.internals.InMemoryKeyValueStore)3 IntegrationTest (org.apache.kafka.test.IntegrationTest)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)2 Bytes (org.apache.kafka.common.utils.Bytes)2 Topology (org.apache.kafka.streams.Topology)2 MaterializedInternal (org.apache.kafka.streams.kstream.internals.MaterializedInternal)2 StateStore (org.apache.kafka.streams.processor.StateStore)2 ReadOnlyKeyValueStore (org.apache.kafka.streams.state.ReadOnlyKeyValueStore)2 TimestampedKeyValueStore (org.apache.kafka.streams.state.TimestampedKeyValueStore)2