【发布时间】:2018-08-18 00:36:59
【问题描述】:
我正在为 python 使用 apache beam(python 版本 2.7),当我将代码上传到 Google App Engine Flexible 时,我总是收到错误:ImportError: No module named main。当我调用端点/server 时,我可以在数据流控制台中看到此错误。
当我在本地执行我的代码时,它在我的 gcloud 数据流中完美运行,但是当我在 GAE Flex 中执行它时,我得到了上面指定的错误。
这是我的代码:
import apache_beam as beam
import logging
logging.basicConfig(level=logging.DEBUG)
from flask import Flask
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import StandardOptions, SetupOptions
from apache_beam.options.pipeline_options import GoogleCloudOptions
from apache_beam.io import WriteToText
from apache_beam.io import ReadFromText
PROJECT_ID = 'PROJECT_ID'
JOB_NAME = 'test-job-name-l'
BUCKET_URL = 'gs://backup-bucket'
app = Flask(__name__)
@app.route('/')
def start():
return "Welcome to datamigration"
@app.route('/server')
def start1():
run()
return "It works"
class FindWords(beam.DoFn):
def process(self, element):
import re as regex
return regex.findall(r"[A-Za-z\']+", element)
class CountWordsTransform(beam.PTransform):
def expand(self, p_collection):
return (p_collection
| "Split" >> (beam.ParDo(FindWords()).with_input_types(unicode))
| "PairWithOne" >> beam.Map(lambda word: (word, 1))
| "GroupBy" >> beam.GroupByKey()
| "AggregateGroups" >> beam.Map(lambda (word, ones): (word, sum(ones))))
def run():
pipeline_options = PipelineOptions()
pipeline_options.view_as(SetupOptions).save_main_session = True
pipeline_options.view_as(
SetupOptions).requirements_file = "requirements.txt"
google_cloud_options = pipeline_options.view_as(GoogleCloudOptions)
google_cloud_options.project = PROJECT_ID
google_cloud_options.job_name = JOB_NAME
google_cloud_options.staging_location = BUCKET_URL + '/staging'
google_cloud_options.temp_location = BUCKET_URL + '/temp'
pipeline_options.view_as(StandardOptions).runner = 'DataflowRunner'
pipeline = beam.Pipeline(options=pipeline_options)
(pipeline
| "Load" >> ReadFromText(BUCKET_URL + "/file.txt")
| "Count Words" >> CountWordsTransform()
| "Save" >> WriteToText(BUCKET_URL + '/result/test')
)
pipeline.run()
if __name__ == '__main__':
app.run(port=8080, debug=True)
这是我总是得到的完整错误:
Error:
Dataflow pipeline failed. State: FAILED, Error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/dataflow_worker/batchworker.py", line 642, in do_work
work_executor.execute()
File "/usr/local/lib/python2.7/dist-packages/dataflow_worker/executor.py", line 156, in execute
op.start()
File "apache_beam/runners/worker/operations.py", line 351, in apache_beam.runners.worker.operations.DoOperation.start
def start(self):
File "apache_beam/runners/worker/operations.py", line 352, in apache_beam.runners.worker.operations.DoOperation.start
with self.scoped_start_state:
File "apache_beam/runners/worker/operations.py", line 357, in apache_beam.runners.worker.operations.DoOperation.start
pickler.loads(self.spec.serialized_fn))
File "/usr/local/lib/python2.7/dist-packages/apache_beam/internal/pickler.py", line 232, in loads
return dill.loads(s)
File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 277, in loads
return load(file)
File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 266, in load
obj = pik.load()
File "/usr/lib/python2.7/pickle.py", line 864, in load
dispatch[key](self)
File "/usr/lib/python2.7/pickle.py", line 1096, in load_global
klass = self.find_class(module, name)
File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 423, in find_class
return StockUnpickler.find_class(self, module, name)
File "/usr/lib/python2.7/pickle.py", line 1130, in find_class
__import__(module)
ImportError: No module named main
我的 app.yaml:
runtime: python
env: flex
service: ms-somename
threadsafe: true
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 2
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
还有我的 requirements.txt
google-cloud-datastore==1.3.0
google-cloud-dataflow==2.5.0
google-apitools==0.5.16
googledatastore==7.0.1
apache-beam==2.5.0
apache-beam[gcp]==2.5.0
Flask==0.12.2
gunicorn==19.9.0
【问题讨论】:
标签: python google-cloud-platform apache-beam dataflow app-engine-flexible