au.csiro.sparkle.spark.AppContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of variant-spark_2.11 Show documentation
Show all versions of variant-spark_2.11 Show documentation
Genomic variants interpretation toolkit
The newest version!
package au.csiro.sparkle.spark;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import org.apache.spark.Accumulator;
import org.apache.spark.api.java.JavaSparkContext;
public class AppContext {
private final JavaSparkContext sc;
private final Map, Counter> counters = new LinkedHashMap, Counter>();
public AppContext(JavaSparkContext sc) {
super();
this.sc = sc;
}
public JavaSparkContext getSparkContext() {
return sc;
}
public synchronized Counter getCounter(Enum> counterKey) {
return counters.computeIfAbsent(counterKey, k-> new Counter(sc.accumulator(0, k.toString())));
}
public static Function countInto(final Accumulator counter) {
return new Function() {
@Override
public T apply(T t) {
counter.add(1);
return t;
}
};
}
public synchronized void printAllCounters() {
counters.forEach( (k,v) -> System.out.format("%s:%s = %d\n",k.getClass().getName(), k, v.value()));
}
public void close() {
sc.close();
}
}