Search in sources :

Example 11 with GraphNode

use of org.apache.kafka.streams.kstream.internals.graph.GraphNode in project kafka by apache.

the class KStreamImpl method toTable.

@Override
public KTable<K, V> toTable(final Named named, final Materialized<K, V, KeyValueStore<Bytes, byte[]>> materialized) {
    Objects.requireNonNull(named, "named can't be null");
    Objects.requireNonNull(materialized, "materialized can't be null");
    final NamedInternal namedInternal = new NamedInternal(named);
    final String name = namedInternal.orElseGenerateWithPrefix(builder, TO_KTABLE_NAME);
    final MaterializedInternal<K, V, KeyValueStore<Bytes, byte[]>> materializedInternal = new MaterializedInternal<>(materialized, builder, TO_KTABLE_NAME);
    final Serde<K> keySerdeOverride = materializedInternal.keySerde() == null ? keySerde : materializedInternal.keySerde();
    final Serde<V> valueSerdeOverride = materializedInternal.valueSerde() == null ? valueSerde : materializedInternal.valueSerde();
    final Set<String> subTopologySourceNodes;
    final GraphNode tableParentNode;
    if (repartitionRequired) {
        final OptimizableRepartitionNodeBuilder<K, V> repartitionNodeBuilder = optimizableRepartitionNodeBuilder();
        final String sourceName = createRepartitionedSource(builder, keySerdeOverride, valueSerdeOverride, name, null, repartitionNodeBuilder);
        tableParentNode = repartitionNodeBuilder.build();
        builder.addGraphNode(graphNode, tableParentNode);
        subTopologySourceNodes = Collections.singleton(sourceName);
    } else {
        tableParentNode = graphNode;
        subTopologySourceNodes = this.subTopologySourceNodes;
    }
    final KTableSource<K, V> tableSource = new KTableSource<>(materializedInternal.storeName(), materializedInternal.queryableStoreName());
    final ProcessorParameters<K, V, ?, ?> processorParameters = new ProcessorParameters<>(tableSource, name);
    final GraphNode tableNode = new StreamToTableNode<>(name, processorParameters, materializedInternal);
    builder.addGraphNode(tableParentNode, tableNode);
    return new KTableImpl<K, V, V>(name, keySerdeOverride, valueSerdeOverride, subTopologySourceNodes, materializedInternal.queryableStoreName(), tableSource, tableNode, builder);
}
Also used : StreamToTableNode(org.apache.kafka.streams.kstream.internals.graph.StreamToTableNode) ProcessorParameters(org.apache.kafka.streams.kstream.internals.graph.ProcessorParameters) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) GraphNode(org.apache.kafka.streams.kstream.internals.graph.GraphNode) ProcessorGraphNode(org.apache.kafka.streams.kstream.internals.graph.ProcessorGraphNode)

Example 12 with GraphNode

use of org.apache.kafka.streams.kstream.internals.graph.GraphNode in project kafka by apache.

the class KTableImpl method doTransformValues.

private <VR> KTable<K, VR> doTransformValues(final ValueTransformerWithKeySupplier<? super K, ? super V, ? extends VR> transformerSupplier, final MaterializedInternal<K, VR, KeyValueStore<Bytes, byte[]>> materializedInternal, final NamedInternal namedInternal, final String... stateStoreNames) {
    Objects.requireNonNull(stateStoreNames, "stateStoreNames");
    final Serde<K> keySerde;
    final Serde<VR> valueSerde;
    final String queryableStoreName;
    final StoreBuilder<TimestampedKeyValueStore<K, VR>> storeBuilder;
    if (materializedInternal != null) {
        // don't inherit parent value serde, since this operation may change the value type, more specifically:
        // we preserve the key following the order of 1) materialized, 2) parent, 3) null
        keySerde = materializedInternal.keySerde() != null ? materializedInternal.keySerde() : this.keySerde;
        // we preserve the value following the order of 1) materialized, 2) null
        valueSerde = materializedInternal.valueSerde();
        queryableStoreName = materializedInternal.queryableStoreName();
        // only materialize if materialized is specified and it has queryable name
        storeBuilder = queryableStoreName != null ? (new TimestampedKeyValueStoreMaterializer<>(materializedInternal)).materialize() : null;
    } else {
        keySerde = this.keySerde;
        valueSerde = null;
        queryableStoreName = null;
        storeBuilder = null;
    }
    final String name = namedInternal.orElseGenerateWithPrefix(builder, TRANSFORMVALUES_NAME);
    final KTableProcessorSupplier<K, V, K, VR> processorSupplier = new KTableTransformValues<>(this, transformerSupplier, queryableStoreName);
    final ProcessorParameters<K, VR, ?, ?> processorParameters = unsafeCastProcessorParametersToCompletelyDifferentType(new ProcessorParameters<>(processorSupplier, name));
    final GraphNode tableNode = new TableProcessorNode<>(name, processorParameters, storeBuilder, stateStoreNames);
    builder.addGraphNode(this.graphNode, tableNode);
    return new KTableImpl<>(name, keySerde, valueSerde, subTopologySourceNodes, queryableStoreName, processorSupplier, tableNode, builder);
}
Also used : GraphNode(org.apache.kafka.streams.kstream.internals.graph.GraphNode) ProcessorGraphNode(org.apache.kafka.streams.kstream.internals.graph.ProcessorGraphNode) TableProcessorNode(org.apache.kafka.streams.kstream.internals.graph.TableProcessorNode) TimestampedKeyValueStore(org.apache.kafka.streams.state.TimestampedKeyValueStore)

Example 13 with GraphNode

use of org.apache.kafka.streams.kstream.internals.graph.GraphNode in project kafka by apache.

the class KTableImpl method doMapValues.

private <VR> KTable<K, VR> doMapValues(final ValueMapperWithKey<? super K, ? super V, ? extends VR> mapper, final Named named, final MaterializedInternal<K, VR, KeyValueStore<Bytes, byte[]>> materializedInternal) {
    final Serde<K> keySerde;
    final Serde<VR> valueSerde;
    final String queryableStoreName;
    final StoreBuilder<TimestampedKeyValueStore<K, VR>> storeBuilder;
    if (materializedInternal != null) {
        // materialize the store; but we still need to burn one index BEFORE generating the processor to keep compatibility.
        if (materializedInternal.storeName() == null) {
            builder.newStoreName(MAPVALUES_NAME);
        }
        keySerde = materializedInternal.keySerde() != null ? materializedInternal.keySerde() : this.keySerde;
        valueSerde = materializedInternal.valueSerde();
        queryableStoreName = materializedInternal.queryableStoreName();
        // only materialize if materialized is specified and it has queryable name
        storeBuilder = queryableStoreName != null ? (new TimestampedKeyValueStoreMaterializer<>(materializedInternal)).materialize() : null;
    } else {
        keySerde = this.keySerde;
        valueSerde = null;
        queryableStoreName = null;
        storeBuilder = null;
    }
    final String name = new NamedInternal(named).orElseGenerateWithPrefix(builder, MAPVALUES_NAME);
    final KTableProcessorSupplier<K, V, K, VR> processorSupplier = new KTableMapValues<>(this, mapper, queryableStoreName);
    // leaving in calls to ITB until building topology with graph
    final ProcessorParameters<K, VR, ?, ?> processorParameters = unsafeCastProcessorParametersToCompletelyDifferentType(new ProcessorParameters<>(processorSupplier, name));
    final GraphNode tableNode = new TableProcessorNode<>(name, processorParameters, storeBuilder);
    builder.addGraphNode(this.graphNode, tableNode);
    // we preserve the value following the order of 1) materialized, 2) null
    return new KTableImpl<>(name, keySerde, valueSerde, subTopologySourceNodes, queryableStoreName, processorSupplier, tableNode, builder);
}
Also used : GraphNode(org.apache.kafka.streams.kstream.internals.graph.GraphNode) ProcessorGraphNode(org.apache.kafka.streams.kstream.internals.graph.ProcessorGraphNode) TableProcessorNode(org.apache.kafka.streams.kstream.internals.graph.TableProcessorNode) TimestampedKeyValueStore(org.apache.kafka.streams.state.TimestampedKeyValueStore)

Example 14 with GraphNode

use of org.apache.kafka.streams.kstream.internals.graph.GraphNode in project kafka by apache.

the class KTableImpl method doFilter.

private KTable<K, V> doFilter(final Predicate<? super K, ? super V> predicate, final Named named, final MaterializedInternal<K, V, KeyValueStore<Bytes, byte[]>> materializedInternal, final boolean filterNot) {
    final Serde<K> keySerde;
    final Serde<V> valueSerde;
    final String queryableStoreName;
    final StoreBuilder<TimestampedKeyValueStore<K, V>> storeBuilder;
    if (materializedInternal != null) {
        // materialize the store; but we still need to burn one index BEFORE generating the processor to keep compatibility.
        if (materializedInternal.storeName() == null) {
            builder.newStoreName(FILTER_NAME);
        }
        // we can inherit parent key and value serde if user do not provide specific overrides, more specifically:
        // we preserve the key following the order of 1) materialized, 2) parent
        keySerde = materializedInternal.keySerde() != null ? materializedInternal.keySerde() : this.keySerde;
        // we preserve the value following the order of 1) materialized, 2) parent
        valueSerde = materializedInternal.valueSerde() != null ? materializedInternal.valueSerde() : this.valueSerde;
        queryableStoreName = materializedInternal.queryableStoreName();
        // only materialize if materialized is specified and it has queryable name
        storeBuilder = queryableStoreName != null ? (new TimestampedKeyValueStoreMaterializer<>(materializedInternal)).materialize() : null;
    } else {
        keySerde = this.keySerde;
        valueSerde = this.valueSerde;
        queryableStoreName = null;
        storeBuilder = null;
    }
    final String name = new NamedInternal(named).orElseGenerateWithPrefix(builder, FILTER_NAME);
    final KTableProcessorSupplier<K, V, K, V> processorSupplier = new KTableFilter<>(this, predicate, filterNot, queryableStoreName);
    final ProcessorParameters<K, V, ?, ?> processorParameters = unsafeCastProcessorParametersToCompletelyDifferentType(new ProcessorParameters<>(processorSupplier, name));
    final GraphNode tableNode = new TableProcessorNode<>(name, processorParameters, storeBuilder);
    builder.addGraphNode(this.graphNode, tableNode);
    return new KTableImpl<K, V, V>(name, keySerde, valueSerde, subTopologySourceNodes, queryableStoreName, processorSupplier, tableNode, builder);
}
Also used : GraphNode(org.apache.kafka.streams.kstream.internals.graph.GraphNode) ProcessorGraphNode(org.apache.kafka.streams.kstream.internals.graph.ProcessorGraphNode) TableProcessorNode(org.apache.kafka.streams.kstream.internals.graph.TableProcessorNode) TimestampedKeyValueStore(org.apache.kafka.streams.state.TimestampedKeyValueStore)

Aggregations

GraphNode (org.apache.kafka.streams.kstream.internals.graph.GraphNode)14 ProcessorGraphNode (org.apache.kafka.streams.kstream.internals.graph.ProcessorGraphNode)6 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 TreeMap (java.util.TreeMap)3 TableProcessorNode (org.apache.kafka.streams.kstream.internals.graph.TableProcessorNode)3 TimestampedKeyValueStore (org.apache.kafka.streams.state.TimestampedKeyValueStore)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 StreamsException (org.apache.kafka.streams.errors.StreamsException)2 OptimizableRepartitionNode (org.apache.kafka.streams.kstream.internals.graph.OptimizableRepartitionNode)2 ProcessorParameters (org.apache.kafka.streams.kstream.internals.graph.ProcessorParameters)2 ArrayList (java.util.ArrayList)1 PriorityQueue (java.util.PriorityQueue)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 TopologyException (org.apache.kafka.streams.errors.TopologyException)1 GlobalStoreNode (org.apache.kafka.streams.kstream.internals.graph.GlobalStoreNode)1