|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Beam Code Examples |
| 4 | +--- |
| 5 | +<!-- |
| 6 | + Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + contributor license agreements. See the NOTICE file distributed with |
| 8 | + this work for additional information regarding copyright ownership. |
| 9 | + The ASF licenses this file to You under the Apache License, Version 2.0 |
| 10 | + (the "License"); you may not use this file except in compliance with |
| 11 | + the License. You may obtain a copy of the License at |
| 12 | +
|
| 13 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | + Unless required by applicable law or agreed to in writing, software |
| 16 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + See the License for the specific language governing permissions and |
| 19 | + limitations under the License. |
| 20 | +--> |
| 21 | + |
| 22 | +The [samza-beam-examples](https://github.com/apache/samza-beam-examples) project contains examples to demonstrate running Beam pipelines with SamzaRunner locally, in Yarn cluster, or in standalone cluster with Zookeeper. More complex pipelines can be built from this project and run in similar manner. |
| 23 | + |
| 24 | +### Example Pipelines |
| 25 | +The following examples are included: |
| 26 | + |
| 27 | +1. [`WordCount`](https://github.com/apache/samza-beam-examples/blob/master/src/main/java/org/apache/beam/examples/WordCount.java) reads a file as input (bounded data source), and computes word frequencies. |
| 28 | + |
| 29 | +2. [`KafkaWordCount`](https://github.com/apache/samza-beam-examples/blob/master/src/main/java/org/apache/beam/examples/KafkaWordCount.java) does the same word-count computation but reading from a Kafka stream (unbounded data source). It uses a fixed 10-sec window to aggregate the counts. |
| 30 | + |
| 31 | +### Run the Examples |
| 32 | + |
| 33 | +Each example can be run locally, in Yarn cluster or in standalone cluster. Here we use KafkaWordCount as an example. |
| 34 | + |
| 35 | +#### Set Up |
| 36 | +1. Download and install [JDK version 8](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html). Verify that the JAVA_HOME environment variable is set and points to your JDK installation. |
| 37 | + |
| 38 | +2. Download and install [Apache Maven](http://maven.apache.org/download.cgi) by following Maven’s [installation guide](http://maven.apache.org/install.html) for your specific operating system. |
| 39 | + |
| 40 | +Check out the `samza-beam-examples` repo: |
| 41 | + |
| 42 | +``` |
| 43 | +$ git clone https://github.com/apache/samza-beam-examples.git |
| 44 | +$ cd samza-beam-examples |
| 45 | +``` |
| 46 | + |
| 47 | +A script named "grid" is included in this project which allows you to easily download and install Zookeeper, Kafka, and Yarn. |
| 48 | +You can run the following to bring them all up running in your local machine: |
| 49 | + |
| 50 | +``` |
| 51 | +$ scripts/grid bootstrap |
| 52 | +``` |
| 53 | + |
| 54 | +All the downloaded package files will be put under `deploy` folder. Once the grid command completes, |
| 55 | +you can verify that Yarn is up and running by going to http://localhost:8088. You can also choose to |
| 56 | +bring them up separately, e.g.: |
| 57 | + |
| 58 | +``` |
| 59 | +$ scripts/grid install zookeeper |
| 60 | +$ scripts/grid start zookeeper |
| 61 | +``` |
| 62 | +Now let's create a Kafka topic named "input-text" for this example: |
| 63 | + |
| 64 | +``` |
| 65 | +$ ./deploy/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic input-text --partitions 10 --replication-factor 1 |
| 66 | +``` |
| 67 | + |
| 68 | +#### Run Locally |
| 69 | +You can run directly within the project using maven: |
| 70 | + |
| 71 | +``` |
| 72 | +$ mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.KafkaWordCount \ |
| 73 | + -Dexec.args="--runner=SamzaRunner" -P samza-runner |
| 74 | +``` |
| 75 | + |
| 76 | +#### Packaging Your Application |
| 77 | +To execute the example in either Yarn or standalone, you need to package it first. |
| 78 | +After packaging, we deploy and explode the tgz in the deploy folder: |
| 79 | + |
| 80 | +``` |
| 81 | + $ mkdir -p deploy/examples |
| 82 | + $ mvn package && tar -xvf target/samza-beam-examples-0.1-dist.tar.gz -C deploy/examples/ |
| 83 | +``` |
| 84 | + |
| 85 | +#### Run in Standalone Cluster with Zookeeper |
| 86 | +You can use the `run-beam-standalone.sh` script included in this repo to run an example |
| 87 | +in standalone mode. The config file is provided as `config/standalone.properties`. Note by |
| 88 | +default we create one single split for the whole input (--maxSourceParallelism=1). To |
| 89 | +set each Kafka partition in a split, we can set a large "maxSourceParallelism" value which |
| 90 | +is the upper bound of the number of splits. |
| 91 | + |
| 92 | +``` |
| 93 | +$ deploy/examples/bin/run-beam-standalone.sh org.apache.beam.examples.KafkaWordCount \ |
| 94 | + --configFilePath=$PWD/deploy/examples/config/standalone.properties --maxSourceParallelism=1024 |
| 95 | +``` |
| 96 | + |
| 97 | +#### Run Yarn Cluster |
| 98 | +Similar to running standalone, we can use the `run-beam-yarn.sh` to run the examples |
| 99 | +in Yarn cluster. The config file is provided as `config/yarn.properties`. To run the |
| 100 | +KafkaWordCount example in yarn: |
| 101 | + |
| 102 | +``` |
| 103 | +$ deploy/examples/bin/run-beam-yarn.sh org.apache.beam.examples.KafkaWordCount \ |
| 104 | + --configFilePath=$PWD/deploy/examples/config/yarn.properties --maxSourceParallelism=1024 |
| 105 | +``` |
| 106 | + |
| 107 | +#### Validate the Pipeline Results |
| 108 | +Now the pipeline is deployed to either locally, standalone or Yarn. Let's check out the results. First we start a kakfa consumer to listen to the output: |
| 109 | + |
| 110 | +``` |
| 111 | +$ ./deploy/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic word-count --property print.key=true |
| 112 | +``` |
| 113 | + |
| 114 | +Then let's publish a few lines to the input Kafka topic: |
| 115 | + |
| 116 | +``` |
| 117 | +$ ./deploy/kafka/bin/kafka-console-producer.sh --topic input-text --broker-list localhost:9092 |
| 118 | +Nory was a Catholic because her mother was a Catholic, and Nory’s mother was a Catholic because her father was a Catholic, and her father was a Catholic because his mother was a Catholic, or had been. |
| 119 | +``` |
| 120 | + |
| 121 | +You should see the word count shows up in the consumer console in about 10 secs: |
| 122 | + |
| 123 | +``` |
| 124 | +a 6 |
| 125 | +br 1 |
| 126 | +mother 3 |
| 127 | +was 6 |
| 128 | +Catholic 6 |
| 129 | +his 1 |
| 130 | +Nory 2 |
| 131 | +s 1 |
| 132 | +father 2 |
| 133 | +had 1 |
| 134 | +been 1 |
| 135 | +and 2 |
| 136 | +her 3 |
| 137 | +or 1 |
| 138 | +because 3 |
| 139 | +``` |
| 140 | + |
| 141 | +### Beyond Examples |
| 142 | +Feel free to build more complex pipelines based on the examples above, and reach out to us: |
| 143 | + |
| 144 | +* Subscribe and mail to [[email protected]](mailto:[email protected]) for any Beam questions. |
| 145 | + |
| 146 | +* Subscribe and mail to [[email protected]](mailto:[email protected]) for any Samza questions. |
| 147 | + |
| 148 | +### More Information |
| 149 | + |
| 150 | +* [Apache Beam](http://beam.apache.org) |
| 151 | +* [Apache Samza](https://samza.apache.org/) |
| 152 | +* Quickstart: [Java](https://beam.apache.org/get-started/quickstart-java), [Python](https://beam.apache.org/get-started/quickstart-py), [Go](https://beam.apache.org/get-started/quickstart-go) |
0 commit comments