Applies a simple 1-to-1 mapping function over each element in the collection.
Example 1: providing the mapping function using a SimpleFunction
{{< highlight java >}} PCollection lines = Create.of(“Hello World”, “Beam is fun”); PCollection lineLengths = lines.apply(MapElements.via( new SimpleFunction<String, Integer>() { @Override public Integer apply(String line) { return line.length(); } }); {{< /highlight >}}
Example 2: providing the mapping function using a SerializableFunction
, which allows the use of Java 8 lambdas. Due to type erasure, you need to provide a hint indicating the desired return type.
{{< highlight java >}} PCollection lines = Create.of(“Hello World”, “Beam is fun”); PCollection lineLengths = lines.apply(MapElements .into(TypeDescriptors.integers()) .via((String line) -> line.length())); {{< /highlight >}}
Map
, but for each input it may produce zero or more outputs.