use of org.apache.beam.runners.spark.translation.SparkCombineFn in project beam by apache.
the class SparkCompat method extractOutput.
/**
* Extracts the output for a given collection of WindowedAccumulators.
*
* <p>This is required because the API of JavaPairRDD.flatMapValues is different among Spark
* versions. See https://issues.apache.org/jira/browse/SPARK-19287
*/
public static <K, InputT, AccumT, OutputT> JavaPairRDD<K, WindowedValue<OutputT>> extractOutput(JavaPairRDD<K, SparkCombineFn.WindowedAccumulator<KV<K, InputT>, InputT, AccumT, ?>> accumulatePerKey, SparkCombineFn<KV<K, InputT>, InputT, AccumT, OutputT> sparkCombineFn) {
try {
if (accumulatePerKey.context().version().startsWith("3")) {
FlatMapFunction<SparkCombineFn.WindowedAccumulator<KV<K, InputT>, InputT, AccumT, ?>, WindowedValue<OutputT>> flatMapFunction = (FlatMapFunction<SparkCombineFn.WindowedAccumulator<KV<K, InputT>, InputT, AccumT, ?>, WindowedValue<OutputT>>) windowedAccumulator -> sparkCombineFn.extractOutputStream(windowedAccumulator).iterator();
// This invokes by reflection the equivalent of:
// return accumulatePerKey.flatMapValues(flatMapFunction);
Method method = accumulatePerKey.getClass().getDeclaredMethod("flatMapValues", FlatMapFunction.class);
Object result = method.invoke(accumulatePerKey, flatMapFunction);
return (JavaPairRDD<K, WindowedValue<OutputT>>) result;
}
Function<SparkCombineFn.WindowedAccumulator<KV<K, InputT>, InputT, AccumT, ?>, Iterable<WindowedValue<OutputT>>> flatMapFunction = windowedAccumulator -> sparkCombineFn.extractOutputStream(windowedAccumulator).collect(Collectors.toList());
// This invokes by reflection the equivalent of:
// return accumulatePerKey.flatMapValues(flatMapFunction);
Method method = accumulatePerKey.getClass().getDeclaredMethod("flatMapValues", Function.class);
Object result = method.invoke(accumulatePerKey, flatMapFunction);
return (JavaPairRDD<K, WindowedValue<OutputT>>) result;
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Error invoking Spark flatMapValues", e);
}
}
Aggregations