我们如何将 Apache Beam 管道输出写入变量而不是文件?

Raj*_*roi 7 python pipeline python-3.x apache-beam

我需要处理数据管道中的一些值,并且需要稍后在程序中的某个位置使用该值。

这是一个简单的例子

import apache_beam as beam

p = beam.Pipeline()

resu=(
    p
    | beam.Create([1,3,5,3,5,3])
    | beam.CombineGlobally(beam.combiners.MeanCombineFn())
    | beam.io.WriteToText("result.txt")
)

p.run()
Run Code Online (Sandbox Code Playgroud)

现在计算平均值并将其放入文件“result.txt”中。如果我稍后需要在程序中使用平均值,我需要执行文件 io 操作。我想让结果作为变量进入内存。我该如何实现这一目标?

就像是

mean_value=resu.values()
# use mean_value as a regular variable
some_other_value=mean_value/2
Run Code Online (Sandbox Code Playgroud)