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.metrics.Sensor;
import org.apache.kafka.streams.kstream.ValueJoiner;
import org.apache.kafka.streams.processor.AbstractProcessor;
import org.apache.kafka.streams.processor.Processor;
import org.apache.kafka.streams.processor.ProcessorContext;
import org.apache.kafka.streams.processor.To;
import org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl;
import org.apache.kafka.streams.state.ValueAndTimestamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.kafka.streams.processor.internals.metrics.TaskMetrics.droppedRecordsSensorOrSkippedRecordsSensor;
import static org.apache.kafka.streams.processor.internals.RecordQueue.UNKNOWN;
import static org.apache.kafka.streams.state.ValueAndTimestamp.getValueOrNull;
class KTableKTableOuterJoin extends KTableKTableAbstractJoin {
private static final Logger LOG = LoggerFactory.getLogger(KTableKTableOuterJoin.class);
KTableKTableOuterJoin(final KTableImpl table1,
final KTableImpl table2,
final ValueJoiner super V1, ? super V2, ? extends R> joiner) {
super(table1, table2, joiner);
}
@Override
public Processor> get() {
return new KTableKTableOuterJoinProcessor(valueGetterSupplier2.get());
}
@Override
public KTableValueGetterSupplier view() {
return new KTableKTableOuterJoinValueGetterSupplier(valueGetterSupplier1, valueGetterSupplier2);
}
private class KTableKTableOuterJoinValueGetterSupplier extends KTableKTableAbstractJoinValueGetterSupplier {
KTableKTableOuterJoinValueGetterSupplier(final KTableValueGetterSupplier valueGetterSupplier1,
final KTableValueGetterSupplier valueGetterSupplier2) {
super(valueGetterSupplier1, valueGetterSupplier2);
}
public KTableValueGetter get() {
return new KTableKTableOuterJoinValueGetter(valueGetterSupplier1.get(), valueGetterSupplier2.get());
}
}
private class KTableKTableOuterJoinProcessor extends AbstractProcessor> {
private final KTableValueGetter valueGetter;
private StreamsMetricsImpl metrics;
private Sensor droppedRecordsSensor;
KTableKTableOuterJoinProcessor(final KTableValueGetter valueGetter) {
this.valueGetter = valueGetter;
}
@Override
public void init(final ProcessorContext context) {
super.init(context);
metrics = (StreamsMetricsImpl) context.metrics();
droppedRecordsSensor = droppedRecordsSensorOrSkippedRecordsSensor(Thread.currentThread().getName(), context.taskId().toString(), metrics);
valueGetter.init(context);
}
@Override
public void process(final K key, final Change change) {
// we do join iff keys are equal, thus, if key is null we cannot join and just ignore the record
if (key == null) {
LOG.warn(
"Skipping record due to null key. change=[{}] topic=[{}] partition=[{}] offset=[{}]",
change, context().topic(), context().partition(), context().offset()
);
droppedRecordsSensor.record();
return;
}
R newValue = null;
final long resultTimestamp;
R oldValue = null;
final ValueAndTimestamp valueAndTimestamp2 = valueGetter.get(key);
final V2 value2 = getValueOrNull(valueAndTimestamp2);
if (value2 == null) {
if (change.newValue == null && change.oldValue == null) {
return;
}
resultTimestamp = context().timestamp();
} else {
resultTimestamp = Math.max(context().timestamp(), valueAndTimestamp2.timestamp());
}
if (value2 != null || change.newValue != null) {
newValue = joiner.apply(change.newValue, value2);
}
if (sendOldValues && (value2 != null || change.oldValue != null)) {
oldValue = joiner.apply(change.oldValue, value2);
}
context().forward(key, new Change<>(newValue, oldValue), To.all().withTimestamp(resultTimestamp));
}
@Override
public void close() {
valueGetter.close();
}
}
private class KTableKTableOuterJoinValueGetter implements KTableValueGetter {
private final KTableValueGetter valueGetter1;
private final KTableValueGetter valueGetter2;
KTableKTableOuterJoinValueGetter(final KTableValueGetter valueGetter1,
final KTableValueGetter valueGetter2) {
this.valueGetter1 = valueGetter1;
this.valueGetter2 = valueGetter2;
}
@Override
public void init(final ProcessorContext context) {
valueGetter1.init(context);
valueGetter2.init(context);
}
@Override
public ValueAndTimestamp get(final K key) {
R newValue = null;
final ValueAndTimestamp valueAndTimestamp1 = valueGetter1.get(key);
final V1 value1;
final long timestamp1;
if (valueAndTimestamp1 == null) {
value1 = null;
timestamp1 = UNKNOWN;
} else {
value1 = valueAndTimestamp1.value();
timestamp1 = valueAndTimestamp1.timestamp();
}
final ValueAndTimestamp valueAndTimestamp2 = valueGetter2.get(key);
final V2 value2;
final long timestamp2;
if (valueAndTimestamp2 == null) {
value2 = null;
timestamp2 = UNKNOWN;
} else {
value2 = valueAndTimestamp2.value();
timestamp2 = valueAndTimestamp2.timestamp();
}
if (value1 != null || value2 != null) {
newValue = joiner.apply(value1, value2);
}
return ValueAndTimestamp.make(newValue, Math.max(timestamp1, timestamp2));
}
@Override
public void close() {
valueGetter1.close();
valueGetter2.close();
}
}
}