Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Apache Beam ValueProvider usage sample from GCP document (
package dummy;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.options.ValueProvider;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.values.TypeDescriptors;
* Usage: ./gradlew run --args="--runner=DirectRunner --int=10"
* This program requires that this class is specified main class via
* `application` plugin setting of
public class SumInteger {
public interface SumIntOptions extends PipelineOptions {
ValueProvider<Integer> getInt();
void setInt(ValueProvider<Integer> value);
static class MySumFn extends DoFn<Integer, Integer> {
ValueProvider<Integer> mySumInteger;
MySumFn(ValueProvider<Integer> sumInt) {
this.mySumInteger = sumInt;
public void processElement(ProcessContext c) {
c.output(c.element() + mySumInteger.get());
public static void main(String... args) {
SumIntOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().as(SumIntOptions.class);
Pipeline p = Pipeline.create(options);
.apply(ParDo.of(new MySumFn(options.getInt())))
.apply("ToString", MapElements.into(TypeDescriptors.strings()).via(x -> x.toString()))
.apply("OutputNums", TextIO.write().to("numvalues"));;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment