我们在使用 Python 和 Java 编写的 GCP 数据流上运行 Beam 数据管道。一开始,我们有一些简单直接的 Python Beam 作业,效果非常好。因此,最近我们决定将更多的 Java Beam 作业转换为 Python Beam 作业。当我们有更复杂的工作时,尤其是需要在光束中开窗的工作时,我们注意到 python 工作比 java 工作明显慢,最终使用更多的 cpu 和内存并且成本更高。
一些示例 python 代码如下所示:
step1 = (
read_from_pub_sub
| "MapKey" >> beam.Map(lambda elem: (elem.data[key], elem))
| "WindowResults"
>> beam.WindowInto(
beam.window.SlidingWindows(360,90),
allowed_lateness=args.allowed_lateness,
)
| "GroupById" >> beam.GroupByKey()
Run Code Online (Sandbox Code Playgroud)
Java 代码如下:
PCollection<DataStructure> step1 =
message
.apply(
"MapKey",
MapElements.into(
TypeDescriptors.kvs(
TypeDescriptors.strings(), TypeDescriptor.of(DataStructure.class)))
.via(event -> KV.of(event.key, event)))
.apply(
"WindowResults",
Window.<KV<String, CustomInterval>>into(
SlidingWindows.of(Duration.standardSeconds(360))
.every(Duration.standardSeconds(90)))
.withAllowedLateness(Duration.standardSeconds(this.allowedLateness))
.discardingFiredPanes())
.apply("GroupById", GroupByKey.<String, DataStructure>create())
Run Code Online (Sandbox Code Playgroud)
我们注意到 Python 使用的 CPU …
python java sliding-window google-cloud-platform apache-beam