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

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

There is a newer version: 3.8.0
Show newest version
/*
 * 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.Serdes;
import org.apache.kafka.common.utils.Bytes;
import org.apache.kafka.streams.kstream.Aggregator;
import org.apache.kafka.streams.kstream.CogroupedKStream;
import org.apache.kafka.streams.kstream.Initializer;
import org.apache.kafka.streams.kstream.KGroupedStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.Named;
import org.apache.kafka.streams.kstream.Reducer;
import org.apache.kafka.streams.kstream.SessionWindowedKStream;
import org.apache.kafka.streams.kstream.SessionWindows;
import org.apache.kafka.streams.kstream.SlidingWindows;
import org.apache.kafka.streams.kstream.TimeWindowedKStream;
import org.apache.kafka.streams.kstream.Window;
import org.apache.kafka.streams.kstream.Windows;
import org.apache.kafka.streams.kstream.internals.graph.StreamsGraphNode;
import org.apache.kafka.streams.state.KeyValueStore;

import java.util.Objects;
import java.util.Set;

class KGroupedStreamImpl extends AbstractStream implements KGroupedStream {

    static final String REDUCE_NAME = "KSTREAM-REDUCE-";
    static final String AGGREGATE_NAME = "KSTREAM-AGGREGATE-";

    private final GroupedStreamAggregateBuilder aggregateBuilder;
    final boolean repartitionRequired;
    final String userProvidedRepartitionTopicName;

    KGroupedStreamImpl(final String name,
                       final Set subTopologySourceNodes,
                       final GroupedInternal groupedInternal,
                       final boolean repartitionRequired,
                       final StreamsGraphNode streamsGraphNode,
                       final InternalStreamsBuilder builder) {
        super(name, groupedInternal.keySerde(), groupedInternal.valueSerde(), subTopologySourceNodes, streamsGraphNode, builder);
        this.repartitionRequired = repartitionRequired;
        this.userProvidedRepartitionTopicName = groupedInternal.name();
        this.aggregateBuilder = new GroupedStreamAggregateBuilder<>(
            builder,
            groupedInternal,
            repartitionRequired,
            subTopologySourceNodes,
            name,
            streamsGraphNode
        );
    }

    @Override
    public KTable reduce(final Reducer reducer) {
        return reduce(reducer, Materialized.with(keySerde, valueSerde));
    }

    @Override
    public KTable reduce(final Reducer reducer,
                               final Materialized> materialized) {
        return reduce(reducer, NamedInternal.empty(), materialized);
    }

    @Override
    public KTable reduce(final Reducer reducer,
                               final Named named,
                               final Materialized> materialized) {
        Objects.requireNonNull(reducer, "reducer can't be null");
        Objects.requireNonNull(materialized, "materialized can't be null");
        Objects.requireNonNull(named, "name can't be null");

        final MaterializedInternal> materializedInternal =
            new MaterializedInternal<>(materialized, builder, REDUCE_NAME);

        if (materializedInternal.keySerde() == null) {
            materializedInternal.withKeySerde(keySerde);
        }
        if (materializedInternal.valueSerde() == null) {
            materializedInternal.withValueSerde(valueSerde);
        }

        final String name = new NamedInternal(named).orElseGenerateWithPrefix(builder, REDUCE_NAME);
        return doAggregate(
            new KStreamReduce<>(materializedInternal.storeName(), reducer),
            name,
            materializedInternal
        );
    }

    @Override
    public  KTable aggregate(final Initializer initializer,
                                        final Aggregator aggregator,
                                        final Materialized> materialized) {
        return aggregate(initializer, aggregator, NamedInternal.empty(), materialized);
    }

    @Override
    public  KTable aggregate(final Initializer initializer,
                                        final Aggregator aggregator,
                                        final Named named,
                                        final Materialized> materialized) {
        Objects.requireNonNull(initializer, "initializer can't be null");
        Objects.requireNonNull(aggregator, "aggregator can't be null");
        Objects.requireNonNull(materialized, "materialized can't be null");
        Objects.requireNonNull(named, "named can't be null");

        final MaterializedInternal> materializedInternal =
            new MaterializedInternal<>(materialized, builder, AGGREGATE_NAME);

        if (materializedInternal.keySerde() == null) {
            materializedInternal.withKeySerde(keySerde);
        }

        final String name = new NamedInternal(named).orElseGenerateWithPrefix(builder, AGGREGATE_NAME);
        return doAggregate(
            new KStreamAggregate<>(materializedInternal.storeName(), initializer, aggregator),
            name,
            materializedInternal
        );
    }

    @Override
    public  KTable aggregate(final Initializer initializer,
                                        final Aggregator aggregator) {
        return aggregate(initializer, aggregator, Materialized.with(keySerde, null));
    }

    @Override
    public KTable count() {
        return doCount(NamedInternal.empty(), Materialized.with(keySerde, Serdes.Long()));
    }

    @Override
    public KTable count(final Named named) {
        Objects.requireNonNull(named, "named can't be null");
        return doCount(named, Materialized.with(keySerde, Serdes.Long()));
    }

    @Override
    public KTable count(final Materialized> materialized) {
        return count(NamedInternal.empty(), materialized);
    }

    @Override
    public KTable count(final Named named, final Materialized> materialized) {
        Objects.requireNonNull(materialized, "materialized can't be null");

        // TODO: remove this when we do a topology-incompatible release
        // we used to burn a topology name here, so we have to keep doing it for compatibility
        if (new MaterializedInternal<>(materialized).storeName() == null) {
            builder.newStoreName(AGGREGATE_NAME);
        }

        return doCount(named, materialized);
    }

    private KTable doCount(final Named named, final Materialized> materialized) {
        final MaterializedInternal> materializedInternal =
            new MaterializedInternal<>(materialized, builder, AGGREGATE_NAME);

        if (materializedInternal.keySerde() == null) {
            materializedInternal.withKeySerde(keySerde);
        }
        if (materializedInternal.valueSerde() == null) {
            materializedInternal.withValueSerde(Serdes.Long());
        }

        final String name = new NamedInternal(named).orElseGenerateWithPrefix(builder, AGGREGATE_NAME);
        return doAggregate(
            new KStreamAggregate<>(materializedInternal.storeName(), aggregateBuilder.countInitializer, aggregateBuilder.countAggregator),
            name,
            materializedInternal);
    }

    @Override
    public  TimeWindowedKStream windowedBy(final Windows windows) {

        return new TimeWindowedKStreamImpl<>(
            windows,
            builder,
            subTopologySourceNodes,
            name,
            keySerde,
            valueSerde,
            aggregateBuilder,
            streamsGraphNode
        );
    }

    @Override
    public TimeWindowedKStream windowedBy(final SlidingWindows windows) {

        return new SlidingWindowedKStreamImpl<>(
                windows,
                builder,
                subTopologySourceNodes,
                name,
                keySerde,
                valueSerde,
                aggregateBuilder,
                streamsGraphNode
        );
    }

    @Override
    public SessionWindowedKStream windowedBy(final SessionWindows windows) {

        return new SessionWindowedKStreamImpl<>(
            windows,
            builder,
            subTopologySourceNodes,
            name,
            keySerde,
            valueSerde,
            aggregateBuilder,
            streamsGraphNode
        );
    }

    private  KTable doAggregate(final KStreamAggProcessorSupplier aggregateSupplier,
                                         final String functionName,
                                         final MaterializedInternal> materializedInternal) {
        return aggregateBuilder.build(
            new NamedInternal(functionName),
            new TimestampedKeyValueStoreMaterializer<>(materializedInternal).materialize(),
            aggregateSupplier,
            materializedInternal.queryableStoreName(),
            materializedInternal.keySerde(),
            materializedInternal.valueSerde());
    }

    @Override
    public  CogroupedKStream cogroup(final Aggregator aggregator) {
        Objects.requireNonNull(aggregator, "aggregator can't be null");
        return new CogroupedKStreamImpl(name, subTopologySourceNodes, streamsGraphNode, builder)
            .cogroup(this, aggregator);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy