This is an automated email from the ASF dual-hosted git repository.

sanha pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nemo.git


The following commit(s) were added to refs/heads/master by this push:
     new de49635  [NEMO-399] Include the official WordCount example from the 
Beam website (#225)
de49635 is described below

commit de496351fc8d95a46f7922e0eea07fe775f2cb1a
Author: Won Wook SONG <[email protected]>
AuthorDate: Wed Oct 23 21:44:48 2019 +0900

    [NEMO-399] Include the official WordCount example from the Beam website 
(#225)
    
    JIRA: [NEMO-399:  Include the official WordCount example from the Beam 
website](https://issues.apache.org/jira/projects/NEMO/issues/NEMO-399)
    
    **Major changes:**
    - Adds the WordCount example from the Beam website
    - Fixes the README accordingly
    
    **Minor changes to note:**
    - None
    
    **Tests for the changes:**
    - None
    
    **Other comments:**
    - None
    
    Closes #225
---
 README.md                                          |  91 +++++-----
 .../apache/nemo/examples/beam/BeamWordCount.java   | 190 +++++++++++++++++++++
 2 files changed, 240 insertions(+), 41 deletions(-)

diff --git a/README.md b/README.md
index f429b27..96bc683 100644
--- a/README.md
+++ b/README.md
@@ -31,38 +31,38 @@ Please refer to the [Contribution 
guideline](.github/CONTRIBUTING.md) to contrib
     * On Ubuntu 14.04 LTS and its point releases:
 
       ```bash
-      sudo apt-get install protobuf-compiler
+      $ sudo apt-get install protobuf-compiler
       ```
 
     * On Ubuntu 16.04 LTS and its point releases:
 
       ```bash
-      sudo add-apt-repository ppa:snuspl/protobuf-250
-      sudo apt update
-      sudo apt install protobuf-compiler=2.5.0-9xenial1
+      $ sudo add-apt-repository ppa:snuspl/protobuf-250
+      $ sudo apt update
+      $ sudo apt install protobuf-compiler=2.5.0-9xenial1
       ```
 
     * On macOS:
 
       ```bash
-      brew tap homebrew/versions
-      brew install [email protected]
+      $ brew tap homebrew/versions
+      $ brew install [email protected]
       ```
 
     * Or build from source:
 
       * Downloadable at https://github.com/google/protobuf/releases/tag/v2.5.0
       * Extract the downloaded tarball
-      * `./configure`
-      * `make`
-      * `make check`
-      * `sudo make install`
+      * `$ ./configure`
+      * `$ make`
+      * `$ make check`
+      * `$ sudo make install`
 
-    *  To check for a successful installation of version 2.5.0, run `protoc 
--version`
+    *  To check for a successful installation of version 2.5.0, run `$ protoc 
--version`
 
 ### Installing Nemo
-* Run all tests and install: `mvn clean install -T 2C`
-* Run only unit tests and install: `mvn clean install -DskipITs -T 2C`
+* Run all tests and install: `$ mvn clean install -T 2C`
+* Run only unit tests and install: `$ mvn clean install -DskipITs -T 2C`
 
 ## Running Beam applications
 
@@ -79,31 +79,40 @@ Below describes how Beam applications can be run directly 
on Nemo.
 
 ### Examples
 ```bash
-## MapReduce example
-./bin/run_beam.sh \
-       -job_id mr_default \
-       -executor_json 
`pwd`/examples/resources/executors/beam_test_executor_resources.json \
-       -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.DefaultPolicy \
-       -user_main org.apache.nemo.examples.beam.WordCount \
-        -user_args "`pwd`/examples/resources/inputs/test_input_wordcount 
`pwd`/outputs/wordcount"
+## WordCount example from the Beam website (Count words from a document)
+$ ./bin/run_beam.sh \
+    -job_id beam_wordcount \
+    -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.DefaultPolicy \
+    -user_main org.apache.nemo.examples.beam.BeamWordCount \
+    -user_args "--runner=NemoRunner 
--inputFile=`pwd`/examples/resources/inputs/test_input_wordcount 
--output=`pwd`/outputs/wordcount"
+$ less `pwd`/outputs/wordcount*
+
+## MapReduce WordCount example (Count words from the Wikipedia dataset)
+$ ./bin/run_beam.sh \
+    -job_id mr_default \
+    -executor_json 
`pwd`/examples/resources/executors/beam_test_executor_resources.json \
+    -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.DefaultPolicy \
+    -user_main org.apache.nemo.examples.beam.WordCount \
+    -user_args "`pwd`/examples/resources/inputs/test_input_wordcount 
`pwd`/outputs/wordcount"
+$ less `pwd`/outputs/wordcount*
 
 ## YARN cluster example
-./bin/run_beam.sh \
-       -deploy_mode yarn \
-       -job_id mr_transient \
-       -executor_json 
`pwd`/examples/resources/executors/beam_test_executor_resources.json \
-       -user_main org.apache.nemo.examples.beam.WordCount \
-       -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.TransientResourcePolicy \
-       -user_args "hdfs://v-m:9000/test_input_wordcount 
hdfs://v-m:9000/test_output_wordcount"
+$ ./bin/run_beam.sh \
+    -deploy_mode yarn \
+    -job_id mr_transient \
+    -executor_json 
`pwd`/examples/resources/executors/beam_test_executor_resources.json \
+    -user_main org.apache.nemo.examples.beam.WordCount \
+    -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.TransientResourcePolicy \
+    -user_args "hdfs://v-m:9000/test_input_wordcount 
hdfs://v-m:9000/test_output_wordcount"
 
 ## NEXMark streaming Q0 (query0) example 
-./bin/run_nexmark.sh \
-       -job_id nexmark-Q0 \
-       -executor_json 
`pwd`/examples/resources/executors/beam_test_executor_resources.json \
-       -user_main org.apache.beam.sdk.nexmark.Main \
-       -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.StreamingPolicy \
-  -scheduler_impl_class_name 
org.apache.nemo.runtime.master.scheduler.StreamingScheduler \     
-       -user_args "--runner=org.apache.nemo.client.beam.NemoRunner 
--streaming=true --query=0 --numEventGenerators=1"
+$ ./bin/run_nexmark.sh \
+    -job_id nexmark-Q0 \
+    -executor_json 
`pwd`/examples/resources/executors/beam_test_executor_resources.json \
+    -user_main org.apache.beam.sdk.nexmark.Main \
+    -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.StreamingPolicy \
+    -scheduler_impl_class_name 
org.apache.nemo.runtime.master.scheduler.StreamingScheduler \   
+    -user_args "--runner=NemoRunner --streaming=true --query=0 
--numEventGenerators=1"
 
 ```
 ## Resource Configuration
@@ -146,13 +155,13 @@ Nemo Compiler and Engine can store JSON representation of 
intermediate DAGs.
 
 ### Examples
 ```bash
-./bin/run_beam.sh \
-       -job_id als \
-       -executor_json 
`pwd`/examples/resources/executors/beam_test_executor_resources.json \
-       -user_main org.apache.nemo.examples.beam.AlternatingLeastSquare \
-       -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.TransientResourcePolicy \
-       -dag_dir "./dag/als" \
-       -user_args "`pwd`/examples/resources/inputs/test_input_als 10 3"
+$ ./bin/run_beam.sh \
+    -job_id als \
+    -executor_json 
`pwd`/examples/resources/executors/beam_test_executor_resources.json \
+    -user_main org.apache.nemo.examples.beam.AlternatingLeastSquare \
+    -optimization_policy 
org.apache.nemo.compiler.optimizer.policy.TransientResourcePolicy \
+    -dag_dir "./dag/als" \
+    -user_args "`pwd`/examples/resources/inputs/test_input_als 10 3"
 ```
 
 ## Options for writing metric results to databases.
diff --git 
a/examples/beam/src/main/java/org/apache/nemo/examples/beam/BeamWordCount.java 
b/examples/beam/src/main/java/org/apache/nemo/examples/beam/BeamWordCount.java
new file mode 100644
index 0000000..2d2021a
--- /dev/null
+++ 
b/examples/beam/src/main/java/org/apache/nemo/examples/beam/BeamWordCount.java
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.nemo.examples.beam;
+
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.io.TextIO;
+import org.apache.beam.sdk.metrics.Counter;
+import org.apache.beam.sdk.metrics.Distribution;
+import org.apache.beam.sdk.metrics.Metrics;
+import org.apache.beam.sdk.options.Default;
+import org.apache.beam.sdk.options.Description;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.options.Validation.Required;
+import org.apache.beam.sdk.transforms.*;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+
+/**
+ * An example that counts words in Shakespeare and includes Beam best 
practices.
+ * THIS EXAMPLE IS TAKEN FROM THE APACHE BEAM REPOSITORY.
+ *
+ * <p>For a detailed walkthrough of this example, see <a
+ * href="https://beam.apache.org/get-started/wordcount-example/";>
+ * https://beam.apache.org/get-started/wordcount-example/ </a>
+ *
+ * <p>Basic concepts, also in the MinimalWordCount example: Reading text 
files; counting a
+ * PCollection; writing to text files
+ *
+ * <p>New Concepts:
+ *
+ * <pre>
+ *   1. Executing a Pipeline both locally and using the selected runner
+ *   2. Using ParDo with static DoFns defined out-of-line
+ *   3. Building a composite transform
+ *   4. Defining your own pipeline options
+ * </pre>
+ *
+ * <p>Concept #1: you can execute this pipeline either locally or using by 
selecting another runner.
+ * These are now command-line options and not hard-coded as they were in the 
MinimalWordCount
+ * example.
+ *
+ * <p>To change the runner, specify:
+ *
+ * <pre>{@code
+ * --runner=YOUR_SELECTED_RUNNER
+ * }</pre>
+ *
+ * <p>To execute this pipeline, specify a local output file (if using the 
{@code DirectRunner}) or
+ * output prefix on a supported distributed file system.
+ *
+ * <pre>{@code
+ * --output=[YOUR_LOCAL_FILE | YOUR_OUTPUT_PREFIX]
+ * }</pre>
+ *
+ * <p>The input file defaults to a public data set containing the text of of 
King Lear, by William
+ * Shakespeare. You can override it and choose your own input with {@code 
--inputFile}.
+ */
+public final class BeamWordCount {
+  private BeamWordCount() {
+  }
+
+  public static final String TOKENIZER_PATTERN = "[^\\p{L}]+";
+
+  /**
+   * Concept #2: You can make your pipeline assembly code less verbose by 
defining your DoFns
+   * statically out-of-line. This DoFn tokenizes lines of text into individual 
words; we pass it to
+   * a ParDo in the pipeline.
+   */
+  static class ExtractWordsFn extends DoFn<String, String> {
+    private final Counter emptyLines = Metrics.counter(ExtractWordsFn.class, 
"emptyLines");
+    private final Distribution lineLenDist =
+      Metrics.distribution(ExtractWordsFn.class, "lineLenDistro");
+
+    @ProcessElement
+    public void processElement(@Element final String element, final 
OutputReceiver<String> receiver) {
+      lineLenDist.update(element.length());
+      if (element.trim().isEmpty()) {
+        emptyLines.inc();
+      }
+
+      // Split the line into words.
+      String[] words = element.split(TOKENIZER_PATTERN, -1);
+
+      // Output each word encountered into the output PCollection.
+      for (String word : words) {
+        if (!word.isEmpty()) {
+          receiver.output(word);
+        }
+      }
+    }
+  }
+
+  /** A SimpleFunction that converts a Word and Count into a printable string. 
*/
+  public static final class FormatAsTextFn extends SimpleFunction<KV<String, 
Long>, String> {
+    @Override
+    public String apply(final KV<String, Long> input) {
+      return input.getKey() + ": " + input.getValue();
+    }
+  }
+
+  /**
+   * A PTransform that converts a PCollection containing lines of text into a 
PCollection of
+   * formatted word counts.
+   *
+   * <p>Concept #3: This is a custom composite transform that bundles two 
transforms (ParDo and
+   * Count) as a reusable PTransform subclass. Using composite transforms 
allows for easy reuse,
+   * modular testing, and an improved monitoring experience.
+   */
+  public static final class CountWords
+    extends PTransform<PCollection<String>, PCollection<KV<String, Long>>> {
+    @Override
+    public PCollection<KV<String, Long>> expand(final PCollection<String> 
lines) {
+
+      // Convert lines of text into individual words.
+      PCollection<String> words = lines.apply(ParDo.of(new ExtractWordsFn()));
+
+      // Count the number of times each word occurs.
+      PCollection<KV<String, Long>> wordCounts = 
words.apply(Count.perElement());
+
+      return wordCounts;
+    }
+  }
+
+  /**
+   * Options supported by {@link WordCount}.
+   *
+   * <p>Concept #4: Defining your own configuration options. Here, you can add 
your own arguments to
+   * be processed by the command-line parser, and specify default values for 
them. You can then
+   * access the options values in your pipeline code.
+   *
+   * <p>Inherits standard configuration options.
+   */
+  public interface WordCountOptions extends PipelineOptions {
+
+    /**
+     * By default, this example reads from a public dataset containing the 
text of King Lear. Set
+     * this option to choose a different input file or glob.
+     */
+    @Description("Path of the file to read from")
+    @Default.String("gs://apache-beam-samples/shakespeare/kinglear.txt")
+    String getInputFile();
+
+    void setInputFile(String value);
+
+    /** Set this required option to specify where to write the output. */
+    @Description("Path of the file to write to")
+    @Required
+    String getOutput();
+
+    void setOutput(String value);
+  }
+
+  static void runWordCount(final WordCountOptions options) {
+    Pipeline p = Pipeline.create(options);
+
+    // Concepts #2 and #3: Our pipeline applies the composite CountWords 
transform, and passes the
+    // static FormatAsTextFn() to the ParDo transform.
+    p.apply("ReadLines", TextIO.read().from(options.getInputFile()))
+      .apply(new CountWords())
+      .apply(MapElements.via(new FormatAsTextFn()))
+      .apply("WriteCounts", TextIO.write().to(options.getOutput()));
+
+    p.run().waitUntilFinish();
+  }
+
+  public static void main(final String[] args) {
+    WordCountOptions options =
+      
PipelineOptionsFactory.fromArgs(args).withValidation().as(WordCountOptions.class);
+
+    runWordCount(options);
+  }
+}

Reply via email to