org.apache.beam.runners.twister2.Twister2TranslationContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twister2-beam Show documentation
Show all versions of twister2-beam Show documentation
Twister2 Big Data Hosting Environment: A composable framework for high-performance
data analytics
/*
* 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.beam.runners.twister2;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.beam.runners.core.construction.SerializablePipelineOptions;
import org.apache.beam.runners.core.construction.TransformInputs;
import org.apache.beam.runners.twister2.translators.functions.Twister2SinkFunction;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.runners.AppliedPTransform;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.util.WindowedValue;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionView;
import org.apache.beam.sdk.values.PValue;
import org.apache.beam.sdk.values.TupleTag;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
import edu.iu.dsc.tws.api.tset.TSetEnvironment;
import edu.iu.dsc.tws.api.tset.sets.TSet;
/**
* doc.
*/
public class Twister2TranslationContext {
private final Twister2PipelineOptions options;
protected final Map> dataSets = new LinkedHashMap<>();
private final Set leaves = new LinkedHashSet<>();
private final Map, TSet>> sideInputDataSets;
private AppliedPTransform, ?, ?> currentTransform;
private final TSetEnvironment environment;
private final SerializablePipelineOptions serializableOptions;
public SerializablePipelineOptions getSerializableOptions() {
return serializableOptions;
}
public Twister2TranslationContext(Twister2PipelineOptions options) {
this.options = options;
this.environment = options.getTSetEnvironment();
this.sideInputDataSets = new HashMap<>();
this.serializableOptions = new SerializablePipelineOptions(options);
}
@SuppressWarnings("unchecked")
public T getOutput(PTransform, T> transform) {
return (T) Iterables.getOnlyElement(currentTransform.getOutputs().values());
}
public PipelineOptions getOptions() {
return options;
}
public void setOutputDataSet(PCollection output, TSet> tset) {
if (!dataSets.containsKey(output)) {
dataSets.put(output, tset);
leaves.add(tset);
}
}
public TSet> getInputDataSet(PValue input) {
TSet> tSet = (TSet>) dataSets.get(input);
leaves.remove(tSet);
return tSet;
}
public Map, PValue> getInputs() {
return currentTransform.getInputs();
}
public T getInput(PTransform transform) {
return (T) Iterables.getOnlyElement(TransformInputs.nonAdditionalInputs(currentTransform));
}
public void setCurrentTransform(AppliedPTransform, ?, ?> transform) {
this.currentTransform = transform;
}
public AppliedPTransform, ?, ?> getCurrentTransform() {
return currentTransform;
}
public Map, PValue> getOutputs() {
return getCurrentTransform().getOutputs();
}
public Map, Coder>> getOutputCoders() {
return currentTransform.getOutputs().entrySet().stream()
.filter(e -> e.getValue() instanceof PCollection)
.collect(Collectors.toMap(Map.Entry::getKey, e -> ((PCollection) e.getValue()).getCoder()));
}
public TSetEnvironment getEnvironment() {
return environment;
}
public void execute() {
for (TSet leaf : leaves) {
leaf.direct().sink(new Twister2SinkFunction());
}
}
public void setSideInputDataSet(
PCollectionView value, TSet> set) {
if (!sideInputDataSets.containsKey(value)) {
sideInputDataSets.put(value, set);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy