创建经典数据流模板时由于某种原因,模板未写入 template_location

问题描述 投票:0回答:1

由于某种原因,模板未写入 template_location。当我运行第一个命令时,管道被执行(奇怪),但模板从未生成。可能是什么原因?

我正在使用下面的cmd来创建模板

python -m my_first_beam.py \
  --runner DataflowRunner \
  --project my-project \
  --staging_location gs://gk_beam/staging \
  --temp_location gs://gk_beam/temp \
  --template_location gs://gk_beam/templates/my_first_beam \
  --region europe-west3

CMD 执行没有错误,但存储桶中没有创建模板

google-cloud-platform google-cloud-dataflow apache-beam
1个回答
0
投票

通过 apache beam python 创建经典模板时,您应该记住通过以下方法进行 gcloud 身份验证。

  1. 作为用户 - 如果您希望本地应用程序暂时使用 您自己的 API 访问用户凭据,请运行以下命令 -

gcloud auth 应用程序-默认登录

  1. 或作为服务帐户 - 尝试将 GOOGLE_APPLICATION_CREDENTIALS 设置为 Python 代码中的服务帐户 json 密钥文件。应该可以。

os.environ.setdefault(“GOOGLE_APPLICATION_CREDENTIALS”,“path_of_service_account_key”)

然后只尝试运行创建模板命令。应该可以正常工作。

添加示例以供参考:

Apache Beam Python 代码:

# classic_template.py

import apache_beam as beam
from apache_beam.io import ReadFromText
from apache_beam.options.pipeline_options import SetupOptions, PipelineOptions

os.environ.setdefault("GOOGLE_APPLICATION_CREDENTIALS", "path_of_service_account_key")

class MyPipelineOptions(PipelineOptions):
    @classmethod
    def _add_argparse_args(cls, parser):
        parser.add_value_provider_argument('--input',
                                           type=str,
                                           help='Path of the file to read from')
        
        parser.add_value_provider_argument('--output',
                                           type=str,
                                           help='Output file to write results to.')
        

pipeline_options = PipelineOptions()
pipeline_options.view_as(SetupOptions).save_main_session = True
beam_options = pipeline_options.view_as(MyPipelineOptions)


pipeline = beam.Pipeline(options=beam_options)

lines = (pipeline | 'read' >> ReadFromText(beam_options.input))

pipeline.run().wait_until_finish()

创建经典模板的命令:

python classic_template.py \
--runner DataflowRunner 
--temp_location location_for_temp_file_storage \
--template_location location_to_store_classic_template.json \
--save_main_session \
--project PROJECT_ID \
--region REGION

命令将被执行,您将看到模板文件在 template_location 处创建。

如有任何疑问,请在此询问。

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.