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.Serde;
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.EmitStrategy;
import org.apache.kafka.streams.kstream.EmitStrategy.StrategyType;
import org.apache.kafka.streams.kstream.Initializer;
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.SlidingWindows;
import org.apache.kafka.streams.kstream.TimeWindowedKStream;
import org.apache.kafka.streams.kstream.Windowed;
import org.apache.kafka.streams.kstream.internals.graph.GraphNode;
import org.apache.kafka.streams.state.StoreBuilder;
import org.apache.kafka.streams.state.Stores;
import org.apache.kafka.streams.state.TimestampedWindowStore;
import org.apache.kafka.streams.state.WindowBytesStoreSupplier;
import org.apache.kafka.streams.state.WindowStore;
import java.time.Duration;
import java.util.Objects;
import java.util.Set;
import org.apache.kafka.streams.state.internals.RocksDbIndexedTimeOrderedWindowBytesStoreSupplier;
import static org.apache.kafka.streams.kstream.internals.KGroupedStreamImpl.AGGREGATE_NAME;
import static org.apache.kafka.streams.kstream.internals.KGroupedStreamImpl.REDUCE_NAME;
public class SlidingWindowedKStreamImpl extends AbstractStream implements TimeWindowedKStream {
private final SlidingWindows windows;
private final GroupedStreamAggregateBuilder aggregateBuilder;
private EmitStrategy emitStrategy = EmitStrategy.onWindowUpdate();
SlidingWindowedKStreamImpl(final SlidingWindows windows,
final InternalStreamsBuilder builder,
final Set subTopologySourceNodes,
final String name,
final Serde keySerde,
final Serde valueSerde,
final GroupedStreamAggregateBuilder aggregateBuilder,
final GraphNode graphNode) {
super(name, keySerde, valueSerde, subTopologySourceNodes, graphNode, builder);
this.windows = Objects.requireNonNull(windows, "windows can't be null");
this.aggregateBuilder = aggregateBuilder;
}
@Override
public KTable, Long> count() {
return count(NamedInternal.empty());
}
@Override
public KTable, Long> count(final Named named) {
return doCount(named, Materialized.with(keySerde, Serdes.Long()));
}
@Override
public KTable, Long> count(final Materialized> materialized) {
return count(NamedInternal.empty(), materialized);
}
@Override
public KTable, Long> count(final Named named, final Materialized> materialized) {
Objects.requireNonNull(materialized, "materialized can't be null");
return doCount(named, materialized);
}
private KTable, Long> 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 aggregateName = new NamedInternal(named).orElseGenerateWithPrefix(builder, AGGREGATE_NAME);
return aggregateBuilder.build(
new NamedInternal(aggregateName),
materialize(materializedInternal),
new KStreamSlidingWindowAggregate<>(windows, materializedInternal.storeName(), emitStrategy, aggregateBuilder.countInitializer, aggregateBuilder.countAggregator),
materializedInternal.queryableStoreName(),
materializedInternal.keySerde() != null ? new FullTimeWindowedSerde<>(materializedInternal.keySerde(), windows.timeDifferenceMs()) : null,
materializedInternal.valueSerde(),
false);
}
@Override
public KTable, VR> aggregate(final Initializer initializer,
final Aggregator super K, ? super V, VR> aggregator) {
return aggregate(initializer, aggregator, Materialized.with(keySerde, null));
}
@Override
public KTable, VR> aggregate(final Initializer initializer,
final Aggregator super K, ? super V, VR> aggregator,
final Named named) {
return aggregate(initializer, aggregator, named, Materialized.with(keySerde, null));
}
@Override
public KTable, VR> 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, VR> 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");
final MaterializedInternal> materializedInternal =
new MaterializedInternal<>(materialized, builder, AGGREGATE_NAME);
if (materializedInternal.keySerde() == null) {
materializedInternal.withKeySerde(keySerde);
}
final String aggregateName = new NamedInternal(named).orElseGenerateWithPrefix(builder, AGGREGATE_NAME);
return aggregateBuilder.build(
new NamedInternal(aggregateName),
materialize(materializedInternal),
new KStreamSlidingWindowAggregate<>(windows, materializedInternal.storeName(), emitStrategy, initializer, aggregator),
materializedInternal.queryableStoreName(),
materializedInternal.keySerde() != null ? new FullTimeWindowedSerde<>(materializedInternal.keySerde(), windows.timeDifferenceMs()) : null,
materializedInternal.valueSerde(),
false);
}
@Override
public KTable, V> reduce(final Reducer reducer) {
return reduce(reducer, NamedInternal.empty());
}
@Override
public KTable, V> reduce(final Reducer reducer, final Named named) {
return reduce(reducer, named, Materialized.with(keySerde, valueSerde));
}
@Override
public KTable, V> reduce(final Reducer reducer,
final Materialized> materialized) {
return reduce(reducer, NamedInternal.empty(), materialized);
}
@Override
public KTable, V> reduce(final Reducer reducer,
final Named named,
final Materialized> materialized) {
Objects.requireNonNull(reducer, "reducer can't be null");
Objects.requireNonNull(named, "named can't be null");
Objects.requireNonNull(materialized, "materialized 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 reduceName = new NamedInternal(named).orElseGenerateWithPrefix(builder, REDUCE_NAME);
return aggregateBuilder.build(
new NamedInternal(reduceName),
materialize(materializedInternal),
new KStreamSlidingWindowAggregate<>(windows, materializedInternal.storeName(), emitStrategy, aggregateBuilder.reduceInitializer, aggregatorForReducer(reducer)),
materializedInternal.queryableStoreName(),
materializedInternal.keySerde() != null ? new FullTimeWindowedSerde<>(materializedInternal.keySerde(), windows.timeDifferenceMs()) : null,
materializedInternal.valueSerde(),
false);
}
@Override
public TimeWindowedKStream emitStrategy(final EmitStrategy emitStrategy) {
this.emitStrategy = emitStrategy;
return this;
}
private StoreBuilder> materialize(final MaterializedInternal> materialized) {
WindowBytesStoreSupplier supplier = (WindowBytesStoreSupplier) materialized.storeSupplier();
if (supplier == null) {
final long retentionPeriod = materialized.retention() != null ? materialized.retention().toMillis() : windows.gracePeriodMs() + 2 * windows.timeDifferenceMs();
// large retention time to ensure that all existing windows needed to create new sliding windows can be accessed
// earliest window start time we could need to create corresponding right window would be recordTime - 2 * timeDifference
if ((windows.timeDifferenceMs() * 2 + windows.gracePeriodMs()) > retentionPeriod) {
throw new IllegalArgumentException("The retention period of the window store "
+ name + " must be no smaller than 2 * time difference plus the grace period."
+ " Got time difference=[" + windows.timeDifferenceMs() + "],"
+ " grace=[" + windows.gracePeriodMs() + "],"
+ " retention=[" + retentionPeriod + "]");
}
switch (materialized.storeType()) {
case IN_MEMORY:
supplier = Stores.inMemoryWindowStore(
materialized.storeName(),
Duration.ofMillis(retentionPeriod),
Duration.ofMillis(windows.timeDifferenceMs()),
false
);
break;
case ROCKS_DB:
supplier = emitStrategy.type() == StrategyType.ON_WINDOW_CLOSE ?
RocksDbIndexedTimeOrderedWindowBytesStoreSupplier.create(
materialized.storeName(),
Duration.ofMillis(retentionPeriod),
Duration.ofMillis(windows.timeDifferenceMs()),
false,
true
) :
Stores.persistentTimestampedWindowStore(
materialized.storeName(),
Duration.ofMillis(retentionPeriod),
Duration.ofMillis(windows.timeDifferenceMs()),
false
);
break;
default:
throw new IllegalStateException("Unknown store type: " + materialized.storeType());
}
}
final StoreBuilder> builder = Stores.timestampedWindowStoreBuilder(
supplier,
materialized.keySerde(),
materialized.valueSerde()
);
if (materialized.loggingEnabled()) {
builder.withLoggingEnabled(materialized.logConfig());
} else {
builder.withLoggingDisabled();
}
// do not enable cache if the emit final strategy is used
if (materialized.cachingEnabled() && emitStrategy.type() != StrategyType.ON_WINDOW_CLOSE) {
builder.withCachingEnabled();
} else {
builder.withCachingDisabled();
}
return builder;
}
private Aggregator aggregatorForReducer(final Reducer reducer) {
return (aggKey, value, aggregate) -> aggregate == null ? value : reducer.apply(aggregate, value);
}
}