Project: Beam (App Definition and Development / Streaming & Messaging)
File: quickstart-java.md

title: "WordCount quickstart for Java" aliases: /get-started/quickstart/ /use/quickstart/ /getting-started/ <!-- Licensed 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. --> This quickstart shows you how to set up a Java development environment and run an written with the , using a of your choice. If you're interested in contributing to the Apache Beam Java codebase, see the . On this page: {{< toc >}} Download and install the version 8, 11, or 17. Verify that the environment variable is set and points to your JDK installation. Download and install by following the for your operating system. Optional: If you want to convert your Maven project to Gradle, install . Generate a Maven example project that builds against the latest Beam release: {{< shell unix >}} mvn archetype:generate \ -DarchetypeGroupId=org.apache.beam \ -DarchetypeArtifactId=beam-sdks-java-maven-archetypes-examples \ -DarchetypeVersion={{< param release_latest >}} \ -DgroupId=org.example \ -DartifactId=word-count-beam \ -Dversion="0.1" \ -Dpackage=org.apache.beam.examples \ -DinteractiveMode=false {{< /shell >}} {{< shell powerShell >}} mvn archetype:generate ` -D archetypeGroupId=org.apache.beam ` -D archetypeArtifactId=beam-sdks-java-maven-archetypes-examples ` -D archetypeVersion={{< param release_latest >}} ` -D groupId=org.example ` -D artifactId=word-count-beam ` -D version="0.1" ` -D package=org.apache.beam.examples ` -D interactiveMode=false {{< /shell >}} Maven creates a new project in the word-count-beam directory. Change into word-count-beam: {{< shell unix >}} cd word-count-beam/ {{< /shell >}} {{< shell powerShell >}} cd .\word-count-beam {{< /shell >}} The directory contains a pom.xml and a src directory with example pipelines. List the example pipelines: {{< shell unix >}} ls src/main/java/org/apache/beam/examples/ {{< /shell >}} {{< shell powerShell >}} dir .\src\main\java\org\apache\beam\examples {{< /shell >}} You should see the following examples: DebuggingWordCount.java* () MinimalWordCount.java* () WindowedWordCount.java* () WordCount.java* () The example used in this tutorial, WordCount.java, defines a Beam pipeline that counts words from an input file (by default, a .txt file containing Shakespeare's "King Lear"). To learn more about the examples, see the . The steps below explain how to convert the build from Maven to Gradle for the following runners: Direct runner Dataflow runner The conversion process for other runners is similar. For additional guidance, see . In the directory with the pom.xml file, run the automated Maven-to-Gradle conversion: {{< highlight >}} gradle init {{< /highlight >}} You'll be asked if you want to generate a Gradle build. Enter yes. You'll also be prompted to choose a DSL (Groovy or Kotlin). For this tutorial, enter 2 for Kotlin. Open the generated build.gradle.kts file and make the following changes: In `repositories`, replace `mavenLocal()` with `mavenCentral()`. In `repositories`, declare a repository for Confluent Kafka dependencies: {{< highlight >}} maven { url =

} {{< /highlight >}} At the end of the build script, add the following conditional dependency: {{< highlight >}} if (project.hasProperty("dataflow-runner")) { dependencies { runtimeOnly("org.apache.beam:beam-runners-google-cloud-dataflow-java:{{< param release_latest >}}") } } {{< /highlight >}} At the end of the build script, add the following task: {{< highlight >}} task("execute", JavaExec::class) { classpath = sourceSets["main"].runtimeClasspath mainClass.set(System.getProperty("mainClass")) } {{< /highlight >}} Build your project: {{< highlight >}} gradle build {{< /highlight >}} If you're planning to use the DataflowRunner, you can skip this step. The runner will pull text directly from Google Cloud Storage. In the word-count-beam directory, create a file called sample.txt. Add some text to the file. For this example, use the text of Shakespeare's . A single Beam pipeline can run on multiple Beam . The is useful for getting started, because it runs on your machine and requires no specific setup. If you're just trying out Beam and you're not sure what to use, use the . The general process for running a pipeline goes like this: Complete any runner-specific setup. Build your command line: Specify a runner with `--runner=<runner>` (defaults to the ). Add any runner-specific required options. Choose input files and an output location that are accessible to the runner. (For example, you can't access a local file if you are running the pipeline on an external cluster.) Run the command. To run the WordCount pipeline: Follow the setup steps for your runner: * * * The DirectRunner will work without additional setup. Run the corresponding Maven or Gradle command below. For Unix shells: {{< runner direct >}} mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \ -Dexec.args="--inputFile=sample.txt --output=counts" -Pdirect-runner {{< /runner >}} {{< runner flink >}} mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \ -Dexec.args="--runner=FlinkRunner --inputFile=sample.txt --output=counts" -Pflink-runner {{< /runner >}} {{< runner flinkCluster >}} mvn package exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \ -Dexec.args="--runner=FlinkRunner --flinkMaster=<flink master> --filesToStage=target/word-count-beam-bundled-0.1.jar \ --inputFile=sample.txt --output=/tmp/counts" -Pflink-runner {{< /runner >}} {{< runner spark >}} mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \ -Dexec.args="--runner=SparkRunner --inputFile=sample.txt --output=counts" -Pspark-runner {{< /runner >}} {{< runner dataflow >}} mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \ -Dexec.args="--runner=DataflowRunner --project=<your-gcp-project> \ --region=<your-gcp-region> \ --gcpTempLocation=gs://<your-gcs-bucket>/tmp \ --inputFile=gs://apache-beam-samples/shakespeare/* --output=gs://<your-gcs-bucket>/counts" \ -Pdataflow-runner {{< /runner >}} {{< runner samza >}} mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \ -Dexec.args="--inputFile=sample.txt --output=/tmp/counts --runner=SamzaRunner" -Psamza-runner {{< /runner >}} {{< runner nemo >}} mvn package -Pnemo-runner && java -cp target/word-count-beam-bundled-0.1.jar org.apache.beam.examples.WordCount \ --runner=NemoRunner --inputFile=`pwd`/sample.txt --output=counts {{< /runner >}} {{< runner jet >}} mvn package -Pjet-runner java -cp target/word-count-beam-bundled-0.1.jar org.apache.beam.examples.WordCount \ --runner=JetRunner --jetLocalMode=3 --inputFile=`pwd`/sample.txt --output=counts {{< /runner >}} For Windows PowerShell: {{< runner direct >}} mvn compile exec:java -D exec.mainClass=org.apache.beam.examples.WordCount ` -D exec.args="--inputFile=sample.txt --output=counts" -P direct-runner {{< /runner >}} {{< runner flink >}} mvn compile exec:java -D exec.mainClass=org.apache.beam.examples.WordCount ` -D exec.args="--runner=FlinkRunner --inputFile=sample.txt --output=counts" -P flink-runner {{< /runner >}} {{< runner flinkCluster >}} mvn package exec:java -D exec.mainClass=org.apache.beam.examples.WordCount ` -D exec.args="--runner=FlinkRunner --flinkMaster=<flink master> --filesToStage=.\target\word-count-beam-bundled-0.1.jar ` --inputFile=C:\path\to\quickstart\sample.txt --output=C:\tmp\counts" -P flink-runner {{< /runner >}} {{< runner spark >}} mvn compile exec:java -D exec.mainClass=org.apache.beam.examples.WordCount ` -D exec.args="--runner=SparkRunner

--output=counts" -P spark-runner {{< /runner >}} {{< runner dataflow >}} mvn compile exec:java -D exec.mainClass=org.apache.beam.examples.WordCount ` -D exec.args="--runner=DataflowRunner --project=<your-gcp-project> ` --region=<your-gcp-region> \ --gcpTempLocation=gs://<your-gcs-bucket>/tmp ` --inputFile=gs://apache-beam-samples/shakespeare/* --output=gs://<your-gcs-bucket>/counts" ` -P dataflow-runner {{< /runner >}} {{< runner samza >}} mvn compile exec:java -D exec.mainClass=org.apache.beam.examples.WordCount ` -D exec.args="--inputFile=sample.txt --output=/tmp/counts --runner=SamzaRunner" -P samza-runner {{< /runner >}} {{< runner nemo >}} mvn package -P nemo-runner -DskipTests java -cp target/word-count-beam-bundled-0.1.jar org.apache.beam.examples.WordCount ` --runner=NemoRunner --inputFile=`pwd`/sample.txt --output=counts {{< /runner >}} {{< runner jet >}} mvn package -P jet-runner java -cp target/word-count-beam-bundled-0.1.jar org.apache.beam.examples.WordCount ` --runner=JetRunner --jetLocalMode=3 --inputFile=$pwd/sample.txt --output=counts {{< /runner >}} For Unix shells: {{< runner direct>}} gradle clean execute -DmainClass=org.apache.beam.examples.WordCount \ --args="--inputFile=sample.txt --output=counts" {{< /runner >}} {{< runner flink>}} TODO: document Flink on Gradle: https://github.com/apache/beam/issues/21498 {{< /runner >}} {{< runner flinkCluster>}} TODO: document FlinkCluster on Gradle: https://github.com/apache/beam/issues/21499 {{< /runner >}} {{< runner spark >}} TODO: document Spark on Gradle: https://github.com/apache/beam/issues/21502 {{< /runner >}} {{< runner dataflow >}} gradle clean execute -DmainClass=org.apache.beam.examples.WordCount \ --args="--project=<your-gcp-project> --inputFile=gs://apache-beam-samples/shakespeare/* \ --output=gs://<your-gcs-bucket>/counts --runner=DataflowRunner" -Pdataflow-runner {{< /runner >}} {{< runner samza>}} TODO: document Samza on Gradle: https://github.com/apache/beam/issues/21500 {{< /runner >}} {{< runner nemo>}} TODO: document Nemo on Gradle: https://github.com/apache/beam/issues/21503 {{< /runner >}} {{< runner jet>}} TODO: document Jet on Gradle: https://github.com/apache/beam/issues/21501 {{< /runner >}} After the pipeline has completed, you can view the output. There might be multiple output files prefixed by `count`. The number of output files is decided by the runner, giving it the flexibility to do efficient, distributed execution. View the output files in a Unix shell: {{< runner direct >}} ls counts* {{< /runner >}} {{< runner flink >}} ls counts* {{< /runner >}} {{< runner flinkCluster >}} ls /tmp/counts* {{< /runner >}} {{< runner spark >}} ls counts* {{< /runner >}} {{< runner dataflow >}} gsutil ls gs://<your-gcs-bucket>/counts* {{< /runner >}} {{< runner samza >}} ls /tmp/counts* {{< /runner >}} {{< runner nemo >}} ls counts* {{< /runner >}} {{< runner jet >}} ls counts* {{< /runner >}} The output files contain unique words and the number of occurrences of each word. View the output content in a Unix shell: {{< runner direct >}} more counts* {{< /runner >}} {{< runner flink >}} more counts* {{< /runner >}} {{< runner flinkCluster >}} more /tmp/counts* {{< /runner >}} {{< runner spark >}} more counts* {{< /runner >}} {{< runner dataflow >}} gsutil cat gs://<your-gcs-bucket>/counts* {{< /runner >}} {{< runner samza >}} more /tmp/counts* {{< /runner >}} {{< runner nemo >}} more counts* {{< /runner >}} {{< runner jet >}} more counts* {{< /runner >}} The order of elements is not guaranteed, to allow runners to optimize for efficiency. But the output should look something like this: ``` ... Think: 3 slower: 1 Having: 1 revives: 1 these: 33 wipe: 1 arrives: 1 concluded: 1 begins: 3 ... ``` Learn more about the and look through the . Walk through the WordCount examples in the . Take a self-paced tour through our . Dive in to some of our favorite . Join the Beam mailing list. Please don't hesitate to if you encounter any issues!