【发布时间】:2022-01-06 13:29:03
【问题描述】:
我尝试处理来自 BigQuery 的数据。
我使用 Apache Beam 创建了一个管道,如下所示:
nlp = fr_core_news_lg.load()
class CleanText(beam.DoFn):
def process(self, row):
row['descriptioncleaned'] = ' '.join(unidecode.unidecode(str(row['description'])).lower().translate(str.maketrans(string.punctuation, ' '*len(string.punctuation))).split())
yield row
class LemmaText(beam.DoFn):
def process(self, row):
doc = nlp(row['descriptioncleaned'], disable=["tagger", "parser", "attribute_ruler", "ner", "textcat"])
row['descriptionlemmatized'] = ' '.join(list(set([token.lemma_ for token in doc])))
yield row
with beam.Pipeline(runner="direct", options=options) as pipeline:
soft = pipeline \
| "GetRows" >> beam.io.ReadFromBigQuery(table=table_spec, gcs_location="gs://mygs") \
| "CleanText" >> beam.ParDo(CleanText()) \
| "LemmaText" >> beam.ParDo(LemmaText()) \
| 'WriteToBigQuery' >> beam.io.WriteToBigQuery('mybq', custom_gcs_temp_location="gs://mygs", create_disposition="CREATE_IF_NEEDED", write_disposition="WRITE_TRUNCATE")
基本上,它从我的 BigQuery 表中加载数据,清理其中一列(字符串类型),并使用 Spacy Lemmatizer 对其进行词形还原。我有大约。 8M 行,每个字符串都很大,大约。 300 字。
最后总结起来需要 15 个多小时才能完成。我们必须每天运行它。
我真的不明白为什么在应该以并行方式运行的 DataFlow 上运行这么长时间。
我已经使用了 Spacy 的 pipe,但我无法真正让它与 Apache Beam 一起使用。
有没有办法使用 DataFlow 加速 Spacy 处理或更好地并行化?
【问题讨论】:
-
处理一行平均需要 6.75 毫秒(800 万个单词需要 15 小时),而每行包含约 300 个要词形化的单词。这听起来很合理。要进一步提高并行度,您可以增加工作人员数量。根据您当前的设置,如果自动扩缩没有按预期扩展,您可以使用较小的机器类型来保持 CPU 繁忙,并间接允许 Dataflow 作业扩展更多工作人员。
标签: python google-bigquery google-cloud-dataflow apache-beam spacy