org.apache.edgent.oplet.window.Aggregate Maven / Gradle / Ivy
The 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.edgent.oplet.window;
import static org.apache.edgent.function.Functions.closeFunction;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import org.apache.edgent.function.BiConsumer;
import org.apache.edgent.function.BiFunction;
import org.apache.edgent.oplet.OpletContext;
import org.apache.edgent.oplet.core.Pipe;
import org.apache.edgent.window.Window;
/**
* Aggregate a window.
* Window contents are aggregated by a
* {@link BiFunction aggregator function}
* passing the list of tuples in the window and
* the partition key. The returned value
* is submitted to the sole output port
* if it is not {@code null}.
*
* @param Type of the input tuples.
* @param Type of the output tuples.
* @param Type of the partition key.
*/
public class Aggregate extends Pipe {
private static final long serialVersionUID = 1L;
private final Window> window;
/**
* The aggregator provided by the user.
*/
private final BiFunction,K, U> aggregator;
public Aggregate(Window> window, BiFunction,K, U> aggregator){
this.aggregator = aggregator;
BiConsumer, K> partProcessor = (tuples, key) -> {
U aggregateTuple = aggregator.apply(tuples, key);
if (aggregateTuple != null)
submit(aggregateTuple);
};
window.registerPartitionProcessor(partProcessor);
this.window=window;
}
@Override
public void initialize(OpletContext context) {
super.initialize(context);
window.registerScheduledExecutorService(this.getOpletContext().getService(ScheduledExecutorService.class));
}
@Override
public void accept(T tuple) {
window.insert(tuple);
}
@Override
public void close() throws Exception {
closeFunction(aggregator);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy