【问题标题】:How to use 'add_value_provider_argument' to initialise runtime parameter?如何使用“add_value_provider_argument”初始化运行时参数?
【发布时间】:2017-11-21 16:22:58
【问题描述】:

以官方文档《创建模板》为例: https://cloud.google.com/dataflow/docs/templates/creating-templates

class WordcountOptions(PipelineOptions):
@classmethod
def _add_argparse_args(cls, parser):
  # Use add_value_provider_argument for arguments to be templatable
  # Use add_argument as usual for non-templatable arguments
  parser.add_value_provider_argument(
      '--input',
      default='gs://dataflow-samples/shakespeare/kinglear.txt',
      help='Path of the file to read from')
  parser.add_argument(
      '--output',
      required=True,
      help='Output file to write results to.')

pipeline_options = PipelineOptions(['--output', 'some/output_path'])
p = beam.Pipeline(options=pipeline_options)
wordcount_options = pipeline_options.view_as(WordcountOptions)
lines = p | 'read' >> ReadFromText(wordcount_options.input)

wordcount_options.input 是一个 RuntimeValueProvider。我想使用运行时指定的值(执行模板),所以我需要使用wordcount_options.input.value。但是,它在创建模板时没有属性“值”。它只有“default_value”。我尝试在创建模板时指定一个值(以便我现在和以后都可以使用它),但无论我在运行时指定什么值,它都只使用我在创建模板时指定的先前值。

(基本上,我的输入是一个pickle文件,所以我不能直接使用wordcount_options.input。)

【问题讨论】:

    标签: python google-cloud-dataflow apache-beam


    【解决方案1】:

    链接示例正下方是Using ValueProvider in your functions 部分。

    文档显示使用ValueProvider 参数上的.get() 方法来检索运行时值。

    请注意,该值不能在管道构建期间使用,因为它尚未从模板中注入。你应该只在运行时方法中调用ValueProvider.get(),例如DoFn.process()

    【讨论】:

      猜你喜欢
      • 2021-05-18
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      相关资源
      最近更新 更多