【问题标题】:Apache Beam unable to identify global function in script GCPApache Beam 无法识别脚本 GCP 中的全局函数
【发布时间】:2020-01-19 22:03:01
【问题描述】:

我正在开展一个在 GCP 上创建流处理预测引擎的项目。我主要从这个 repo here 中学习。但是,当我尝试执行脚本 blogposts/got_sentiment/4_streaming_pipeline/streaming_tweet.py 时,我不断收到错误

NameError: name 'estimate' is not defined [while running 'generatedPtransform-129']

我的函数如下所示

from __future__ import absolute_import

import argparse
import datetime
import json
import logging

import numpy as np

import apache_beam as beam
import apache_beam.transforms.window as window
from apache_beam.io.gcp.bigquery import parse_table_schema_from_json
from apache_beam.options.pipeline_options import StandardOptions, GoogleCloudOptions, SetupOptions, PipelineOptions
from apache_beam.transforms.util import BatchElements

from googleapiclient import discovery

def init():
    ........

def estimate_cmle():

   init()
   .....

def estimate(instances):

   estimate_cmle()
   ......


def run(argv=None):
....

   output = (lines
                     | 'assign window key' >> beam.WindowInto(window.FixedWindows(10))
                     | 'batch into n batches' >> BatchElements(min_batch_size=49, max_batch_size=50)
                     | 'predict sentiment' >> beam.FlatMap(lambda messages: estimate(messages))
                     )

.....


f __name__ == '__main__':
    logging.getLogger().setLevel(logging.INFO)
    run()

这是梁似乎无法识别估计函数的地方,尽管我在同一个脚本中创建它。

编辑

尝试使用beam.FlatMap(estimate) 出现错误

name 'estimate_cmle' is not defined [while running 'generatedPtransform-1208']

【问题讨论】:

  • 您是否尝试过使用 beam.FlatMap(estimate) 而不是 beam.FlatMap(lambda messages: estimate(messages))?
  • 我试过了。现在它给了我estimate_cmle not found :(
  • 您在哪里以及如何使用estimate_cmle?
  • 在估计函数中,estimate_cmle 被用于调用 CMLE 上的模型 API 以获取预测
  • 您使用的是哪个 python 版本?我在 github 上看到了代码,它应该可以工作

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


【解决方案1】:

看看这两部分:

函数定义

def estimate(instances):
......

函数调用

beam.FlatMap(lambda messages: estimate(messages,estimate_cmle))

您的调用需要一个带有 2 个参数的函数,您声明的函数只有一个。您的 python 脚本只包含一个带有 1 个参数的估计值,并且没有定义带有 2 个参数的函数。

在 repo 中的示例中,调用仅包含 1 个参数,因此可以正常工作。解决这个问题,它应该可以工作了

【讨论】:

  • 对不起,这是我正在尝试的东西。即使没有争论,我也会遇到同样的错误
猜你喜欢
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 2016-06-02
  • 2018-03-11
相关资源
最近更新 更多