All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.kafka.streams.kstream.internals.GroupedStreamAggregateBuilder Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.kafka.streams.kstream.internals;

import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.streams.kstream.Aggregator;
import org.apache.kafka.streams.kstream.Initializer;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.internals.graph.ProcessorParameters;
import org.apache.kafka.streams.kstream.internals.graph.StatefulProcessorNode;
import org.apache.kafka.streams.kstream.internals.graph.GraphNode;
import org.apache.kafka.streams.processor.internals.StoreFactory;

import java.util.Collections;
import java.util.Set;

import static org.apache.kafka.streams.kstream.internals.graph.OptimizableRepartitionNode.OptimizableRepartitionNodeBuilder;
import static org.apache.kafka.streams.kstream.internals.graph.OptimizableRepartitionNode.optimizableRepartitionNodeBuilder;

class GroupedStreamAggregateBuilder {

    private final InternalStreamsBuilder builder;
    private final Serde keySerde;
    private final Serde valueSerde;
    private final boolean repartitionRequired;
    private final String userProvidedRepartitionTopicName;
    private final Set subTopologySourceNodes;
    private final String name;
    private final GraphNode graphNode;
    private GraphNode repartitionNode;

    final Initializer countInitializer = () -> 0L;

    final Aggregator countAggregator = (aggKey, value, aggregate) -> aggregate + 1;

    final Initializer reduceInitializer = () -> null;

    GroupedStreamAggregateBuilder(final InternalStreamsBuilder builder,
                                  final GroupedInternal groupedInternal,
                                  final boolean repartitionRequired,
                                  final Set subTopologySourceNodes,
                                  final String name,
                                  final GraphNode graphNode) {

        this.builder = builder;
        this.keySerde = groupedInternal.keySerde();
        this.valueSerde = groupedInternal.valueSerde();
        this.repartitionRequired = repartitionRequired;
        this.subTopologySourceNodes = subTopologySourceNodes;
        this.name = name;
        this.graphNode = graphNode;
        this.userProvidedRepartitionTopicName = groupedInternal.name();
    }

     KTable build(final NamedInternal functionName,
                                  final StoreFactory storeFactory,
                                  final KStreamAggProcessorSupplier aggregateSupplier,
                                  final String queryableStoreName,
                                  final Serde keySerde,
                                  final Serde valueSerde,
                                  final boolean isOutputVersioned) {
        assert queryableStoreName == null || queryableStoreName.equals(storeFactory.name());

        final String aggFunctionName = functionName.name();

        String sourceName = this.name;
        GraphNode parentNode = graphNode;

        if (repartitionRequired) {
            final OptimizableRepartitionNodeBuilder repartitionNodeBuilder = optimizableRepartitionNodeBuilder();
            final String repartitionTopicPrefix = userProvidedRepartitionTopicName != null ? userProvidedRepartitionTopicName : storeFactory.name();
            sourceName = createRepartitionSource(repartitionTopicPrefix, repartitionNodeBuilder);

            // First time through we need to create a repartition node.
            // Any subsequent calls to GroupedStreamAggregateBuilder#build we check if
            // the user has provided a name for the repartition topic, is so we re-use
            // the existing repartition node, otherwise we create a new one.
            if (repartitionNode == null || userProvidedRepartitionTopicName == null) {
                repartitionNode = repartitionNodeBuilder.build();
            }

            builder.addGraphNode(parentNode, repartitionNode);
            parentNode = repartitionNode;
        }

        final StatefulProcessorNode statefulProcessorNode =
            new StatefulProcessorNode<>(
                aggFunctionName,
                new ProcessorParameters<>(aggregateSupplier, aggFunctionName),
                storeFactory
            );
        statefulProcessorNode.setOutputVersioned(isOutputVersioned);

        builder.addGraphNode(parentNode, statefulProcessorNode);

        return new KTableImpl<>(aggFunctionName,
                                keySerde,
                                valueSerde,
                                sourceName.equals(this.name) ? subTopologySourceNodes : Collections.singleton(sourceName),
                                queryableStoreName,
                                aggregateSupplier,
                                statefulProcessorNode,
                                builder);

    }

    /**
     * @return the new sourceName of the repartitioned source
     */
    private String createRepartitionSource(final String repartitionTopicNamePrefix,
                                           final OptimizableRepartitionNodeBuilder optimizableRepartitionNodeBuilder) {

        return KStreamImpl.createRepartitionedSource(builder,
                                                     keySerde,
                                                     valueSerde,
                                                     repartitionTopicNamePrefix,
                                                     null,
                                                     optimizableRepartitionNodeBuilder);

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy