【问题标题】:ImportError: No module named language_v1.gapic when running Dataflow jobImportError:运行 Dataflow 作业时没有名为 language_v1.gapic 的模块
【发布时间】:2019-08-14 14:10:13
【问题描述】:

我正在构建 Dataflow 作业以从云存储中获取数据并将其传递给 NLP API 以执行情绪分析并将结果导入 BigQuery

作业在本地成功运行(我没有使用数据流运行器)

import apache_beam as beam
import logging
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types


PROJECT = 'ProjectName'
schema = 'name : STRING, id : STRING, date : STRING,title : STRING, text: STRING,magnitude : STRING, score : STRING'
src_path = "gs://amazoncustreview/sentimentSource.csv"


class Sentiment(beam.DoFn):
    def process(self, element):
        element = element.split(",")
        client = language.LanguageServiceClient()
        document = types.Document(content=element[2],
                                  type=enums.Document.Type.PLAIN_TEXT)
        sentiment = client.analyze_sentiment(document).document_sentiment
        return [{
            'name': element[0],
            'title': element[1],
            'magnitude': sentiment.magnitude,
            'score': sentiment.score
        }]


def main():
    BUCKET = 'BucKet name'
    argv = [
      '--project={0}'.format(PROJECT),
      '--staging_location=gs://{0}/staging/'.format(BUCKET),
      '--temp_location=gs://{0}/staging/'.format(BUCKET),
      '--runner=DataflowRunner',
      '--job_name=examplejob2',
      '--save_main_session'
    ]
    p = beam.Pipeline(argv=argv)

    (p
       | 'ReadData' >> beam.io.textio.ReadFromText(src_path)
       | 'ParseCSV' >> beam.ParDo(Sentiment())
       | 'WriteToBigQuery' >> 
    beam.io.WriteToBigQuery('{0}:Dataset.table'.format(PROJECT),
        write_disposition=beam.io.BigQueryDisposition.WRITE_APPEND)
    )
    p.run()


if __name__ == '__main__':
    main()

这是我得到的错误,我尝试导入不同版本的谷歌云语言,但我的所有尝试都失败了。

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/dataflow_worker/batchworker.py", line 773, in run
    self._load_main_session(self.local_staging_directory)
  File "/usr/local/lib/python2.7/dist-packages/dataflow_worker/batchworker.py", line 489, in _load_main_session
    pickler.load_session(session_file)
  File "/usr/local/lib/python2.7/dist-packages/apache_beam/internal/pickler.py", line 280, in load_session
    return dill.load_session(file_path)
  File "/usr/local/lib/python2.7/dist-packages/dill/_dill.py", line 410, in load_session
    module = unpickler.load()
  File "/usr/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 1139, in load_reduce
    value = func(*args)
  File "/usr/local/lib/python2.7/dist-packages/dill/_dill.py", line 828, in _import_module
    return getattr(__import__(module, None, None, [obj]), obj)
ImportError: No module named language_v1.gapic

【问题讨论】:

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


    【解决方案1】:

    这似乎与安装在 Dataflow 工作器中的 google-cloud-language 版本不匹配。要解决它,请创建一个 requirements.txt 文件并添加 google-cloud-language==1.3.0 例如。

    然后,将'--requirements_file=requirements.txt' 添加到管道的选项参数中。

    我用this code 对其进行了测试,它对我有用:

    【讨论】:

    • 你知道一种方法来指定发送到 api 的请求数吗?
    • 过去我使用sleep 函数(Java example) 限制了一个步骤,这不是一个非常优雅的解决方案。如果您可以将几行/评论“批处理”到一个文档中,那么您可以查看start_bundlefinish_bundle 方法(例如datastoreio writes)。
    猜你喜欢
    • 2022-08-19
    • 2019-10-03
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2014-11-28
    • 2016-07-10
    • 2017-01-29
    相关资源
    最近更新 更多