簡體   English   中英

在 GCP Console 上創建 Dataflow 作業期間提供參數時出錯

[英]Error when providing arguments during Dataflow Job creation on GCP Console

自 2021 年 10 月 06 日起,我的 GCP Dataflow 模板文件正在獲取模板創建期間提供的參數值(當我在本地機器上運行 .py 文件以在 GCP 存儲上創建模板文件時)並且沒有獲取參數在基於此相同模板文件的作業創建期間提供。 如果我在模板創建期間不提供任何值,它們會假定一個 RuntimeValueProvider(當不使用 args 的默認值時),而不是在作業創建期間提供的值。

創建作業期間提供的參數存儲在 Dataflow 作業會話中。 如果我打開作業,請轉到右側欄並打開“管道選項”,這樣在創建作業期間提供的正確值就在那里,但它們沒有到達代碼。

我在 GCP 控制台中以經典方式從模板運行我的代碼:

gcloud dataflow jobs run JOB_NAME --gcs-location gs://LOCATION/TEMPLATE/FILE --region REGION --project PROJ_NAME --worker-machine-type MACHINE_TYPE --parameters PARAM_1=PARAM_1_VALUE,PARAM_2=PARAM_2_VALUE

我使用的是 SDK 2.32.0,在代碼內部我使用的是“parser.add_value_provider_argument”而不是“parser.add_argument”。 但是我使用“parser.add_argument”對其進行了測試,但沒有成功。 對於這兩種情況,我的代碼假設我運行 .py 文件時的參數值。

示例 1

import apache_beam.io.gcp.gcsfilesystem as gcs
from apache_beam.options.pipeline_options import PipelineOptions
class MyOptions(PipelineOptions):
    @classmethod
    def _add_argparse_args(cls, parser):
        parser.add_value_provider_argument('--PARAM_1', 
            type=str)
        parser.add_value_provider_argument('--PARAM_2', 
            type=str)
beam_options = PipelineOptions()
args = beam_options.view_as(MyOptions)

# Some business operations with args that are always assuming the  values provided during template creation
options = {'project': PROJECT,
           'runner': 'DataflowRunner',
           'region': REGION,
           'staging_location': 'gs://{}/temp'.format(BUCKET),
           'temp_location': 'gs://{}/temp'.format(BUCKET),
           'template_location': 'gs://{}/template/batch_poc'.format(BUCKET)}
pipeline_options = PipelineOptions.from_dictionary(options)

with beam.Pipeline(options = pipeline_options) as p:
    lines = (p
            | beam...
            )

示例 2(與示例 1 相同,但使用默認值)

# ... same as example 1
class MyOptions(PipelineOptions):
        @classmethod
        def _add_argparse_args(cls, parser):
            parser.add_value_provider_argument('--PARAM_1',
                default="test1",
                type=str)
            parser.add_value_provider_argument('--PARAM_2', 
                default="test2",
                type=str)
# ... same as example 1

在所有情況下,我在創建作業期間提供的參數都被忽略。

案例 1:當在本地機器上運行沒有 args 的示例 1(作為下面的 python 命令)並在 GCP 控制台上運行其模板時,兩種情況:args 和沒有 args(作為下面的第二個命令)。 PARAM_1_VALUE 和 PARAM_2_VALUE 中的值相同: RuntimeValueProvider(...)

LOCALHOST> python3 code.py

GCP> gcloud dataflow jobs run ...
OR
GCP> gcloud dataflow jobs run ... --parameters PARAM_1=another_test_1,PARAM_2=another_test_2

案例 2:當在本地機器上使用 args 運行示例 1(作為下面的 python 命令)並在 GCP 控制台上運行其模板時,兩種情況:args 和沒有 args(作為下面的第二個命令)。 PARAM_1_VALUE 和 PARAM_2_VALUE 中的值與模板創建期間傳遞的值相同:another_test_{value} 而不是 another_another_test_{value}

LOCALHOST> python3 code.py --PARAM_1 another_test_1 --PARAM_2 another_test_2

GCP> gcloud dataflow jobs run ...
OR
GCP> gcloud dataflow jobs run ... --parameters PARAM_1=another_another_test_1,PARAM_2=another_another_test_2

案例 3:當在本地機器上運行沒有 args 的示例 2(作為下面的 python 命令)並在 GCP 控制台上運行其模板時,兩種情況:args 和沒有 args(作為下面的第二個命令)。 PARAM_1_VALUE 和 PARAM_2_VALUE 中的值是默認值。

LOCALHOST> python3 code.py

GCP> gcloud dataflow jobs run ...
OR
GCP> gcloud dataflow jobs run ... --parameters PARAM_1=another_test_1,PARAM_2=another_test_2

案例 4:當在本地機器上使用 args 運行示例 2(作為下面的 python 命令)並在 GCP 控制台上運行其模板時,兩種情況:args 和沒有 args(作為下面的第二個命令)。 它的發生與情況 2 相同。

注意:我更新了兩個庫:apache-beam 和 apache-beam[gcp]

請注意,在管道構建期間不能使用“--PARAM_1_VALUE”、“--PARAM_1_VALUE”...值。 根據1

“RuntimeValueProvider 是默認的 ValueProvider 類型。 RuntimeValueProvider 允許您的管道接受僅在管道執行期間可用的值。 該值在管道構建期間不可用,因此您無法使用該值來更改管道的工作流圖。”

該文檔顯示在 ValueProvider 參數上使用 .get() 方法允許您在運行時檢索值並在您的函數中使用它。 字面上地:

“要在您自己的函數中使用運行時參數值,請更新函數以使用 ValueProvider 參數。”

這里,ValueProvider.get() 在運行時方法 DoFn.process() 內被調用。

基於此,我建議您在2 之后更改代碼並重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM