DoFn is not a functional interface, so lambdas won't work with it. If you
look at MapElements and FlatMapElements, these are transforms built on top
of ParDo that allow passing a lambda. Unfortunately, due to issues with
type erasure and Java generics, when using a lambda it is necessary to
specify the output types as well. You can see an example of this in the
java8tests:

https://github.com/apache/incubator-beam/blob/master/sdks/java/java8tests/src/test/java/org/apache/beam/sdk/transforms/MapElementsJava8Test.java#L49

Unfortunately, this still won't work well for your use case since the
lambda doesn't have access to the timestamp.

On Tue, May 17, 2016 at 5:41 PM Jesse Anderson <[email protected]>
wrote:

> Is there a way to create a DoFN with a lambda function? The DoFN class
> itself should support it, but the overloading of the ParDo.of causes
> ambiguity for a lambda function. Is there a different way to accomplish
> this?
>
> To answer the why would you want this:
>
>   static class EnrichWithTimestampFN extends DoFn<KV<String, Long>,
> KV<String, KV<Instant, Long>>> {
>     @Override
>     public void processElement(DoFn<KV<String, Long>, KV<String,
> KV<Instant, Long>>>.ProcessContext context) throws Exception {
>       context.output(KV.of(context.element().getKey(),
> KV.of(context.timestamp(), context.element().getValue())));
>     }
>   }
>
> This is a DoFN that I wrote to enrich a PCollection with a the time
> (Instant). I need the access to the ProcessContext to get the timestamp.
> This would be much easier expressed with a lambda.
>
> Thanks,
>
> Jesse
>

Reply via email to