apache beam java api_Apache Beam Java SDK 快速入门

Apache Beam Java SDK 快速入门

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

设置开发环境

按照指定操作系统上Maven的

获取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

单个beam管道可以在多个

在您选择了需要使用的runner之后:

确保您已正确配置了该runner。

通过以下方式构建命令行:

使用 --runner= 选项制定选定好的runner(默认为

添加该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= --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://: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= \                  --gcpTempLocation=gs:///tmp \                  --inputFile=gs://apache-beam-samples/shakespeare/* --output=gs:///counts" \     -Pdataflow-runner

检查结果

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

$ ls counts*

$ ls counts*

$ ls counts*

$ ls /tmp/counts*

$ ls counts*

$ gsutil ls gs:///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:///counts*feature: 15smother'st: 1revelry: 1bashfulness: 1Bashful: 1Below: 2deserves: 32barrenly: 1...

下一步

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值