GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use MapElements in WordCount example · rajim17/DataflowJavaSDK@27a68e8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • examples/src
    • main/java/com/google/cloud/dataflow/examples
      • WordCount.java
      • cookbook
        • DatastoreWordCount.java
    • test/java/com/google/cloud/dataflow/examples
      • WordCountTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎examples/src/main/java/com/google/cloud/dataflow/examples/WordCount.java‎

Copy file name to clipboardExpand all lines: examples/src/main/java/com/google/cloud/dataflow/examples/WordCount.java
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import com.google.cloud.dataflow.sdk.transforms.Aggregator;
2828
import com.google.cloud.dataflow.sdk.transforms.Count;
2929
import com.google.cloud.dataflow.sdk.transforms.DoFn;
30+
import com.google.cloud.dataflow.sdk.transforms.MapElements;
3031
import com.google.cloud.dataflow.sdk.transforms.PTransform;
3132
import com.google.cloud.dataflow.sdk.transforms.ParDo;
33+
import com.google.cloud.dataflow.sdk.transforms.SimpleFunction;
3234
import com.google.cloud.dataflow.sdk.transforms.Sum;
3335
import com.google.cloud.dataflow.sdk.util.gcsfs.GcsPath;
3436
import com.google.cloud.dataflow.sdk.values.KV;
@@ -116,11 +118,11 @@ public void processElement(ProcessContext c) {
116118
}
117119
}
118120

119-
/** A DoFn that converts a Word and Count into a printable string. */
120-
public static class FormatAsTextFn extends DoFn<KV<String, Long>, String> {
121+
/** A SimpleFunction that converts a Word and Count into a printable string. */
122+
public static class FormatAsTextFn extends SimpleFunction<KV<String, Long>, String> {
121123
@Override
122-
public void processElement(ProcessContext c) {
123-
c.output(c.element().getKey() + ": " + c.element().getValue());
124+
public String apply(KV<String, Long> input) {
125+
return input.getKey() + ": " + input.getValue();
124126
}
125127
}
126128

@@ -196,7 +198,7 @@ public static void main(String[] args) {
196198
// static FormatAsTextFn() to the ParDo transform.
197199
p.apply(TextIO.Read.named("ReadLines").from(options.getInputFile()))
198200
.apply(new CountWords())
199-
.apply(ParDo.of(new FormatAsTextFn()))
201+
.apply(MapElements.via(new FormatAsTextFn()))
200202
.apply(TextIO.Write.named("WriteCounts").to(options.getOutput()));
201203

202204
p.run();
Collapse file

‎examples/src/main/java/com/google/cloud/dataflow/examples/cookbook/DatastoreWordCount.java‎

Copy file name to clipboardExpand all lines: examples/src/main/java/com/google/cloud/dataflow/examples/cookbook/DatastoreWordCount.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.cloud.dataflow.sdk.options.PipelineOptionsFactory;
3434
import com.google.cloud.dataflow.sdk.options.Validation;
3535
import com.google.cloud.dataflow.sdk.transforms.DoFn;
36+
import com.google.cloud.dataflow.sdk.transforms.MapElements;
3637
import com.google.cloud.dataflow.sdk.transforms.ParDo;
3738

3839
import java.util.Map;
@@ -198,7 +199,7 @@ public static void readDataFromDatastore(Options options) {
198199
p.apply("ReadShakespeareFromDatastore", Read.from(source))
199200
.apply("StringifyEntity", ParDo.of(new GetContentFn()))
200201
.apply("CountWords", new WordCount.CountWords())
201-
.apply("PrintWordCount", ParDo.of(new WordCount.FormatAsTextFn()))
202+
.apply("PrintWordCount", MapElements.via(new WordCount.FormatAsTextFn()))
202203
.apply("WriteLines", TextIO.Write.to(options.getOutput())
203204
.withNumShards(options.getNumShards()));
204205
p.run();
Collapse file

‎examples/src/test/java/com/google/cloud/dataflow/examples/WordCountTest.java‎

Copy file name to clipboardExpand all lines: examples/src/test/java/com/google/cloud/dataflow/examples/WordCountTest.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import com.google.cloud.dataflow.sdk.testing.TestPipeline;
2727
import com.google.cloud.dataflow.sdk.transforms.Create;
2828
import com.google.cloud.dataflow.sdk.transforms.DoFnTester;
29-
import com.google.cloud.dataflow.sdk.transforms.ParDo;
29+
import com.google.cloud.dataflow.sdk.transforms.MapElements;
3030
import com.google.cloud.dataflow.sdk.values.PCollection;
3131

3232
import org.hamcrest.CoreMatchers;
@@ -77,7 +77,7 @@ public void testCountWords() throws Exception {
7777
PCollection<String> input = p.apply(Create.of(WORDS).withCoder(StringUtf8Coder.of()));
7878

7979
PCollection<String> output = input.apply(new CountWords())
80-
.apply(ParDo.of(new FormatAsTextFn()));
80+
.apply(MapElements.via(new FormatAsTextFn()));
8181

8282
DataflowAssert.that(output).containsInAnyOrder(COUNTS_ARRAY);
8383
p.run();

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.