Apache Beam Java SDK 快速入门

Apache Beam Java SDK 快速入门

本快速入门将指导您使用beam的Java SDK在您选择的runner上执行您的第一个beam管道来运行WordCount

设置开发环境

获取WordCount 代码

获取WordCount管道副本的最简单方法是使用以下命令生成一个简单的Maven项目,该项目包含Beam的WordCount示例,并针对最新的Beam版本构建:

$ mvn archetype:generate \      -DarchetypeGroupId=org.apache.beam \      -DarchetypeArtifactId=beam-sdks-java-maven-archetypes-examples \      -DarchetypeVersion=2.1.0 \      -DgroupId=org.example \      -DartifactId=word-count-beam \      -Dversion="0.1" \      -Dpackage=org.apache.beam.examples \      -DinteractiveMode=false

执行命令后,会创建一个word-count-beam目录,目录里面包含一个简单的pom.xml配置和一系列示例管道用于计算文本文件中单词的数量。

$ cd word-count-beam/$ lspom.xml        src$ ls src/main/java/org/apache/beam/examples/DebuggingWordCount.java        WindowedWordCount.java        commonMinimalWordCount.java        WordCount.java

有关这些示例中使用的Beam概念的详细介绍,请参阅 WordCount Example Walkthrough在这里,我们将聚焦于执行WordCount.java。

运行WordCount

单个beam管道可以在多个 runner上运行, 包括 ApexRunnerFlinkRunnerSparkRunner 或 DataflowRunner.。 DirectRunner 是开始入门时通用的一个运行器,因为他在您的本地计算机上运行,不需要特定的设置。
在您选择了需要使用的runner之后:
  • 确保您已正确配置了该runner。
  • 通过以下方式构建命令行:
    • 使用 --runner=<runner> 选项制定选定好的runner(默认为 DirectRunner)
    • 添加该runner的必须选项
    • 选择runner能访问到的输入文件和输出位置。 (例如,如果如果在外部群集上运行管道,则无法访问本地文件。)
  • 运行您的第一个WordCount管道。

$ mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \     -Dexec.args="--inputFile=pom.xml --output=counts" -Pdirect-runner
$ mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \     -Dexec.args="--inputFile=pom.xml --output=counts --runner=ApexRunner" -Papex-runner
$ mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \     -Dexec.args="--runner=FlinkRunner --inputFile=pom.xml --output=counts" -Pflink-runner
$ 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=/path/to/quickstart/pom.xml --output=/tmp/counts" -Pflink-runnerYou can monitor the running job by visiting the Flink dashboard at http://<flink master>:8081
$ mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \     -Dexec.args="--runner=SparkRunner --inputFile=pom.xml --output=counts" -Pspark-runner
$ mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.WordCount \     -Dexec.args="--runner=DataflowRunner --project=<your-gcp-project> \                  --gcpTempLocation=gs://<your-gcs-bucket>/tmp \                  --inputFile=gs://apache-beam-samples/shakespeare/* --output=gs://<your-gcs-bucket>/counts" \     -Pdataflow-runner
检查结果

管道运行完成后,你可以查看输出。你会注意到有多个以count开头的输出文件。这些文件的确切数量由runner决定,这样方便runner进行高效灵活的分布式执行。

$ ls counts*
$ ls counts*
$ ls counts*
$ ls /tmp/counts*
$ ls counts*
$ gsutil ls gs://<your-gcs-bucket>/counts*

当您查看文件的内容时,您会看到它们包含唯一的单词和每个单词的出现次数。 文件中元素的顺序可能会和这里看到的不同,因为Beam模型通常不保证排序,以便runner优化执行效率。

$ more counts*api: 9bundled: 1old: 4Apache: 2The: 1limitations: 1Foundation: 1...
$ cat counts*BEAM: 1have: 1simple: 1skip: 4PAssert: 1...
$ more counts*The: 1api: 9old: 4Apache: 2limitations: 1bundled: 1Foundation: 1...
$ more /tmp/counts*The: 1api: 9old: 4Apache: 2limitations: 1bundled: 1Foundation: 1...
$ more counts*beam: 27SF: 1fat: 1job: 1limitations: 1require: 1of: 11profile: 10...
$ gsutil cat gs://<your-gcs-bucket>/counts*feature: 15smother'st: 1revelry: 1bashfulness: 1Bashful: 1Below: 2deserves: 32barrenly: 1...
下一步

如果您遇到任何问题,请随时与我们联系!

 

posted @ 2019-01-07 16:42  大头男  阅读(925)  评论(0编辑  收藏  举报