Apache Beam 是一個開源的統一編程模型,旨在構建複雜的數據處理管道。它支撐批處理跟流處理,可能跨多個大年夜數據履行引擎無縫運轉。本文將具體介紹 Apache Beam 的道理、基本利用、高等利用,並經由過程示例展示其上風。
官網鏈接
Apache Beam 官方網站:https://beam.apache.org/
道理概述
Apache Beam 的核心不雅點包含 Pipeline、PCollection、PTransform 跟 Runner。
Pipeline
Pipeline 代表全部數據處理任務。
PCollection
PCollection 代表數據集,可能是無限的(批處理)或無窮的(流處理)。
PTransform
PTransform 代表數據轉換操縱。
Runner
Runner 擔任履行 Pipeline,可能是當地履行或分佈式履行(如 Google Cloud Dataflow、Apache Flink 等)。
Apache Beam 的架構計劃上實現了前後端分別,前端是差別言語的 SDKs,後端是大年夜數據履行引擎。經由過程 Beam,開辟人員可能利用統一的 API 編寫數據處理邏輯,然後在多種履行引擎上運轉。
基本利用
增加依附
在 Maven 項目中,可能經由過程增加以下依附來利用 Apache Beam:
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-core</artifactId>
<version>2.36.</version>
</dependency>
批處理示例:單詞計數
PipelineOptions options = PipelineOptionsFactory.create();
Pipeline pipeline = Pipeline.create(options);
PCollection<String> lines = pipeline.apply(TextIO.read().from("gs://dataflow-templates/samples/wc/lines.txt"))
.apply(ParDo.of(new DoFn<String, String>() {
@ProcessElement
public void processElement(ProcessContext c) {
c.output(c.element().toLowerCase().split("\\s+"));
}
}));
PCollection<String> words = lines.apply(FlatMapElements.of(new SimpleFunction<String, String>() {
@Override
public String apply(String element) {
return element;
}
}));
PCollection<Long> counts = words.apply(Counting.ofElements());
counts.apply(TextIO.write().to("gs://dataflow-templates/samples/wc/output"));
pipeline.run().waitUntilFinish();
流處理示例:從 Kafka 讀取數據
PipelineOptions options = PipelineOptionsFactory.create();
Pipeline pipeline = Pipeline.create(options);
PCollection<String> lines = pipeline.apply(KafkaIO.readStrings()
.withBootstrapServers("kafka:9092")
.withTopic("input")
.withStartUpDelayMs(10000));
PCollection<String> words = lines.apply(ParDo.of(new DoFn<String, String>() {
@ProcessElement
public void processElement(ProcessContext c) {
c.output(c.element().toLowerCase().split("\\s+"));
}
}));
PCollection<String> wordCounts = words.apply(Counting.ofElements());
wordCounts.apply(TextIO.write().to("gs://dataflow-templates/samples/wc/output"));
長處
Apache Beam 存在以下長處:
- 統一編程模型:對批處理跟流媒體用例利用單個編程模型。
- 便利:支撐多個 pipelines 情況,包含:Apache Apex、Apache Flink、Apache Spark 跟 Google Cloud Dataflow。
- 可擴大年夜:編寫跟分享新的 SDKs,IO 連接器跟 transformation 庫。
結論
Apache Beam 是一個功能富強的東西,可能幫助開辟人員構建高效的大年夜數據處理管道。經由過程其統一的編程模型跟跨多個履行引擎的支撐,Apache Beam 為開辟人員供給了一個機動且富強的平台來處理複雜的數據處理任務。