【发布时间】:2018-03-17 10:12:11
【问题描述】:
我是 apache beam 的新手,正在探索 apache Beam 数据流的 python 版本。我想以特定顺序执行我的数据流任务,但它以并行模式执行所有任务。如何在 apache Beam python 中创建任务依赖?
示例代码:(在下面的代码中 sample.json 文件包含 5 行)
import apache_beam as beam
import logging
from apache_beam.options.pipeline_options import PipelineOptions
class Sample(beam.PTransform):
def __init__(self, index):
self.index = index
def expand(self, pcoll):
logging.info(self.index)
return pcoll
class LoadData(beam.DoFn):
def process(self, context):
logging.info("***")
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
pipeline = beam.Pipeline(options=PipelineOptions())
(pipeline
| "one" >> Sample(1)
| "two: Read" >> beam.io.ReadFromText('sample.json')
| "three: show" >> beam.ParDo(LoadData())
| "four: sample2" >> Sample(2)
)
pipeline.run().wait_until_finish()
我预计它将按照一、二、三、四的顺序执行。但它是以并行模式运行的。
以上代码的输出:
INFO:root:Missing pipeline option (runner). Executing pipeline using the
default runner: DirectRunner.
INFO:root:1
INFO:root:2
INFO:root:Running pipeline with DirectRunner.
INFO:root:***
INFO:root:***
INFO:root:***
INFO:root:***
INFO:root:***
【问题讨论】:
-
你想通过按顺序执行来完成什么?另外,我不确定您的“示例”转换应该做什么:在实施时,它什么也不做。还要记住,就像数据库查询计划一样,首先构建管道(当您看到来自 expand() 的日志记录时),然后由运行器优化并执行(当您看到 "* **").
-
@jkff 我想将数据从 biquery 加载到 elasticsearch。在我的示例转换中,我正在执行创建、重新索引、删除弹性搜索索引等操作。所以首先我需要创建一个临时索引,第二个加载数据和 ES 临时索引,第三个重新索引它,第四个删除我的临时索引。所以我想以有序的方式执行所有这些任务。但这里首先执行创建、重新索引和删除任务,最后执行加载数据。 (你可以看到最后显示的日志“*****”)
标签: python google-cloud-dataflow apache-beam dataflow apache-beam-io