title: How can I dynamically add field in MapElements in apache_beam?
body: <p>I would appreciate if someone could help me out writing java code for <em>apache_beam (2.13.0)</em> .</p>
  
  <p>In <em>python</em> , you could <strong>dynamically</strong> add field using <strong>1 to 1 mapping</strong> of <em>Map</em> function.</p>
  
  <h2>Code</h2>
  
  <pre><code>#!/usr/bin/env
  
  import apache_beam as beam
  from apache_beam.io.textio import WriteToText
  
  def addoutput(line):
      return [line, "Its weekend!"]
  
  with beam.Pipeline() as p:
      ( p
        | beam.Create(["blah"])
        | beam.Map(addoutput)
        | WriteToText(file_path_prefix="/tmp/sample")
      )
  </code></pre>
  
  <h2>Result</h2>
  
  <pre><code>['blah', 'Its weekend!']
  </code></pre>
  
  <p>However, when I try to do same thing with java, I get an compile error in <em>maven</em>.</p>
  
  <h2>Code</h2>
  
  <pre><code>public class SampleTextIO
  {
      static class AddFieldFn extends DoFn&lt;String, String&gt; {
  
          @ProcessElement
          public void processElement(@Element String word, OutputReceiver&lt;String&gt; receiver) {
  
              receiver.output(word);
              receiver.output("Its weekend!");
          }
      }
  
      public static void main ( String[] args ) {
          System.out.println( "Main class for DirectRunner" );
  
          // Pipeline create using default runner (DirectRunnter)
          // Interface: PipelineOptions
          PipelineOptions options = PipelineOptionsFactory.create();
  
          Pipeline p = Pipeline.create(options);
  
          // Example pcollection
          final List&lt;String&gt; LINES = Arrays.asList(
              "blah"
          );
  
          // Read lines from file
          p.apply(Create.of(LINES))
           .apply(MapElements.via(new AddFieldFn()))
           .apply(TextIO.write().to("/tmp/test-out"));
  
          p.run().waitUntilFinish();
      }
  }
  </code></pre>
  
  <h2>Result</h2>
  
  <pre><code>[ERROR] /home/ywatanabe/git/google-data-engineer/Data_Science_on_the_Google_Cloud_Platform/Ch04/java/directrunner/src/main/java/com/example/SampleTextIO.java:[43,28] no suitable method found for via(com.example.SampleTextIO.AddFieldFn)
  [ERROR]     method org.apache.beam.sdk.transforms.MapElements.&lt;InputT,OutputT&gt;via(org.apache.beam.sdk.transforms.InferableFunction&lt;InputT,OutputT&gt;) is not applicable
  [ERROR]       (cannot infer type-variable(s) InputT,OutputT
  [ERROR]         (argument mismatch; com.example.SampleTextIO.AddFieldFn cannot be converted to org.apache.beam.sdk.transforms.InferableFunction&lt;InputT,OutputT&gt;))
  [ERROR]     method org.apache.beam.sdk.transforms.MapElements.&lt;InputT,OutputT&gt;via(org.apache.beam.sdk.transforms.SimpleFunction&lt;InputT,OutputT&gt;) is not applicable
  [ERROR]       (cannot infer type-variable(s) InputT,OutputT
  [ERROR]         (argument mismatch; com.example.SampleTextIO.AddFieldFn cannot be converted to org.apache.beam.sdk.transforms.SimpleFunction&lt;InputT,OutputT&gt;))
  [ERROR]     method org.apache.beam.sdk.transforms.MapElements.via(org.apache.beam.sdk.transforms.ProcessFunction) is not applicable
  [ERROR]       (argument mismatch; com.example.SampleTextIO.AddFieldFn cannot be converted to org.apache.beam.sdk.transforms.ProcessFunction)
  [ERROR]     method org.apache.beam.sdk.transforms.MapElements.via(org.apache.beam.sdk.transforms.SerializableFunction) is not applicable
  [ERROR]       (argument mismatch; com.example.SampleTextIO.AddFieldFn cannot be converted to org.apache.beam.sdk.transforms.SerializableFunction)
  [ERROR]     method org.apache.beam.sdk.transforms.MapElements.via(org.apache.beam.sdk.transforms.Contextful) is not applicable
  [ERROR]       (argument mismatch; com.example.SampleTextIO.AddFieldFn cannot be converted to org.apache.beam.sdk.transforms.Contextful)
  </code></pre>
  
  <p>Reading the <a href="https://beam.apache.org/releases/javadoc/2.13.0/org/apache/beam/sdk/transforms/MapElements.html" rel="nofollow noreferrer">javadoc</a>, <em>MapElements</em> supports <em>Processfunction</em> but does not work well in my case.</p>
  
  <p>How can I <strong>dynamically</strong> add fields like <em>python</em> in <em>java</em> ?</p>