Beam Streaming管道不会将文件写入存储桶 [英] Beam streaming pipeline does not write files to bucket

查看:69
本文介绍了Beam Streaming管道不会将文件写入存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UI在GCP Dataflow上具有python流传输管道,该管道从PubSub读取数千条消息,如下所示:

UI have a python streaming pipeline on GCP Dataflow that reads thousands of messages from a PubSub, like this:

    with beam.Pipeline(options=pipeline_options) as p:
      lines = p | "read" >> ReadFromPubSub(topic=str(job_options.inputTopic))
      lines = lines | "decode" >> beam.Map(decode_message)
      lines = lines | "Parse" >> beam.Map(parse_json)
      lines = lines | beam.WindowInto(beam.window.FixedWindows(1*60))
      lines = lines | "Add device id key" >> beam.Map(lambda elem: (elem.get('id'), elem))
      lines = lines | "Group by key" >> beam.GroupByKey()
      lines = lines | "Abandon key" >> beam.Map(flatten)
      lines | "WriteToAvro" >> beam.io.WriteToAvro(job_options.outputLocation, schema=schema, file_name_suffix='.avro', mime_type='application/x-avro')

管道运行得很好,除非它从不产生任何输出.有什么想法吗?

The pipeline runs just fine, except it never produces any output. Any ideas why?

推荐答案

您的代码似乎有一些问题.首先,关于null/None(您已修复)和ints/floats(在注释中注明)存在一些格式错误的数据.最后, WriteToAvro 转换不能写入无界的PCollections.有一种解决方法,您可以在其中定义新的 WriteToFiles 转换,它能够写入无限制的PCollections.

It looks like there were a few problems with your code. First, there was some badly formatted data with regards to null/None (you fixed already) and ints/floats (called out in comments). Finally, the WriteToAvro transform cannot write unbounded PCollections. There is a work-around in which you define a new sink and use that with the WriteToFiles transform which is able to write unbounded PCollections.

请注意,在撰写本文(2020-06-18)时,该方法不适用于Apache Beam Python SDK< = 2.23.这是因为Python选取器无法反序列化选取的Avro模式(请参见 BEAM-6522 ).在这种情况下,这迫使解决方案改为使用FastAvro.如果您手动升级莳萝,则可以使用Avro>> 0.3.1.1 Avro到> = 1.9.0,但是请小心,因为目前尚未测试.

Note that as of the writing of this post (2020-06-18), this method does not work with the Apache Beam Python SDK <= 2.23. This is because the Python pickler cannot deserialize a pickled Avro schema (see BEAM-6522). In this case, this forces a solution to use FastAvro instead. You can use Avro if you manually upgrade dill to >= 0.3.1.1 and Avro to >= 1.9.0, but be careful as this is currently untested.

请注意,以下是解决方法:

With the caveats out of the way, here is the work-around:

from apache_beam.io.fileio import FileSink
from apache_beam.io.fileio import WriteToFiles
import fastavro

class AvroFileSink(FileSink):
    def __init__(self, schema, codec='deflate'):
        self._schema = schema
        self._codec = codec

    def open(self, fh):
        # This is called on every new bundle.
        self.writer = fastavro.write.Writer(fh, self._schema, self._codec)

    def write(self, record):
        # This is called on every element.
        self.writer.write(record)

    def flush(self):
        self.writer.flush()

此新接收器的用法如下:

This new sink is used like the following:

import apache_beam as beam

# Replace the following with your schema.
schema = fastavro.schema.parse_schema({
    'name': 'row',
    'namespace': 'test',
    'type': 'record',
    'fields': [
        {'name': 'a', 'type': 'int'},
    ],
})

# Create the sink. This will be used by the WriteToFiles transform to write
# individual elements to the Avro file.
sink = AvroFileSink(schema=schema)

with beam.Pipeline(...) as p:
    lines = p | beam.ReadFromPubSub(...)
    lines = ...

    # This is where your new sink gets used. The WriteToFiles transform takes
    # the sink and uses it to write to a directory defined by the path 
    # argument.
    lines | WriteToFiles(path=job_options.outputLocation, sink=sink)

这篇关于Beam Streaming管道不会将文件写入存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆