Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.GraphNode;
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 GraphNode graphNode,
final InternalStreamsBuilder builder) {
super(name, groupedInternal.keySerde(), groupedInternal.valueSerde(), subTopologySourceNodes, graphNode, builder);
this.repartitionRequired = repartitionRequired;
this.userProvidedRepartitionTopicName = groupedInternal.name();
this.aggregateBuilder = new GroupedStreamAggregateBuilder<>(
builder,
groupedInternal,
repartitionRequired,
subTopologySourceNodes,
name,
graphNode
);
}
@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 super K, ? super V, VR> aggregator,
final Materialized> materialized) {
return aggregate(initializer, aggregator, NamedInternal.empty(), materialized);
}
@Override
public KTable aggregate(final Initializer initializer,
final Aggregator super K, ? super V, VR> 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 super K, ? super V, VR> 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,
graphNode
);
}
@Override
public TimeWindowedKStream windowedBy(final SlidingWindows windows) {
return new SlidingWindowedKStreamImpl<>(
windows,
builder,
subTopologySourceNodes,
name,
keySerde,
valueSerde,
aggregateBuilder,
graphNode
);
}
@Override
public SessionWindowedKStream windowedBy(final SessionWindows windows) {
return new SessionWindowedKStreamImpl<>(
windows,
builder,
subTopologySourceNodes,
name,
keySerde,
valueSerde,
aggregateBuilder,
graphNode
);
}
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 super K, ? super V, VOut> aggregator) {
Objects.requireNonNull(aggregator, "aggregator can't be null");
return new CogroupedKStreamImpl(name, subTopologySourceNodes, graphNode, builder)
.cogroup(this, aggregator);
}
}