【发布时间】:2020-08-01 12:06:21
【问题描述】:
我正在使用 Google Dataflow 上的 Apache Beam,我正在通过 lambda 函数调用函数情感,但我收到一个错误,即未定义函数名称。
output_tweets = (lines
| 'decode' >> beam.Map(lambda x: x.decode('utf-8'))
| 'assign window key' >> beam.WindowInto(window.FixedWindows(10))
| 'batch into n batches' >> BatchElements(min_batch_size=49, max_batch_size=50)
| 'sentiment analysis' >> beam.FlatMap(lambda x: sentiment(x))
)
这是我的 Apache Beam 调用,在最后一行中提到了函数情绪,这给我带来了问题。
函数代码如下(我觉得应该不重要):
def sentiment(messages):
if not isinstance(messages, list):
messages = [messages]
instances = list(map(lambda message: json.loads(message), messages))
lservice = discovery.build('language', 'v1beta1', developerKey = APIKEY)
for instance in instances['text']:
response = lservice.documents().analyzeSentiment(
body ={
'document': {
'type': 'PLAIN_TEXT',
'content': instance
}
}
).execute()
instance['polarity'] = response['documentSentiment']['polarity']
instance['magnitude'] = response['documentSentiment']['magnitude']
return instances
我得到以下回溯
File "stream.py", line 97, in <lambda>
NameError: name 'sentiment' is not defined [while running 'generatedPtransform-441']
有什么想法吗?
【问题讨论】:
标签: python google-cloud-dataflow apache-beam dataflow