【问题标题】:apache_beam.io.WriteToText() name of file as template parameterapache_beam.io.WriteToText() 文件名作为模板参数
【发布时间】:2020-12-25 22:20:10
【问题描述】:

我有一个在 DataFlow 上执行的 apache_beam 模板。我需要将要写入的文件的名称作为模板参数传递。类似的东西

gcloud dataflow jobs run JOB_NAME \
    --gcs-location gs://YOUR_BUCKET_NAME/templates/MyTemplate \
    --parameters output_file_name=OUTPUT_NAME , input_file_name=INPUT_NAME 

output_file 的值可以通过 PipelineOptions 之类的方式访问

class MyOptions(PipelineOptions):
@classmethod
def _add_argparse_args(cls,parser):
    parser.add_value_provider_argument('--output_file_name', type=str,
          help='Output Name')
    parser.add_value_provider_argument('--input_file_name', type=str,
          help='Input Name')
    

如果我需要在 apache_beam.io.WriteToText 中使用 output_name 类似的东西

   optlist=PipelineOptions()
    my_options=optlist.view_as(MyOptions)
    p = apache_beam.Pipeline(options=optlist)
    (p
    | 'create'            >> apache_beam.Create(["1","2","3"])
    | beam.io.WriteToText(my_options.output_file_name.get())

    )
    p.run()

这是不允许的,因为我们只能将 my_options.output_name.get() 读取为运行时值,并且会抛出错误,如果我们尝试将此代码部署为模板。

考虑到这是一个非常常见的用例,我认为可能有一个通用的库或解决方案。有人有解决办法吗?

【问题讨论】:

    标签: python-3.x google-cloud-dataflow apache-beam


    【解决方案1】:

    对于这种情况,您可以使用Flex Templates。从模板创建管道时,它们对哪些参数可以变化没有任何限制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2016-11-18
      相关资源
      最近更新 更多