2

我注意到在 java sdk 中,有一个函数可以让您编写 csv 文件的标题。 https://cloud.google.com/dataflow/java-sdk/JavaDoc/com/google/cloud/dataflow/sdk/io/TextIO.Write.html#withHeader-java.lang.String-

此功能是否反映在 python skd 上?

4

4 回答 4

7

您现在可以使用文本接收器写入文本并指定标题。

从文档中:

class apache_beam.io.textio.WriteToText(file_path_prefix, file_name_suffix='', append_trailing_newlines=True, num_shards=0, shard_name_template=None, coder=ToStringCoder, compression_type='auto', header=None)

因此,您可以使用以下代码:

beam.io.WriteToText(bucket_name, file_name_suffix='.csv', header='colname1, colname2')

如果您需要详细信息或检查其实施方式,可在此处获得完整的文档:https ://beam.apache.org/documentation/sdks/pydoc/2.0.0/_modules/apache_beam/io/textio.html#WriteToText

于 2017-06-27T17:04:18.483 回答
1

目前未实施。但是,您可以自己实现/扩展它(有关我的 apache_beam 版本的示例+测试,请参见随附的笔记本)。

这是基于超类的文档字符串中的注释FileSink,提到您应该覆盖该open函数:

适用于我的 apache_beam 版本的新类('0.3.0-incubating.dev'):

import apache_beam as beam
from apache_beam.io import TextFileSink
from apache_beam.io.fileio import ChannelFactory,CompressionTypes
from apache_beam import coders


class TextFileSinkWithHeader(TextFileSink):
    def __init__(self,
               file_path_prefix,
               file_name_suffix='',
               append_trailing_newlines=True,
               num_shards=0,
               shard_name_template=None,
               coder=coders.ToStringCoder(),
               compression_type=CompressionTypes.NO_COMPRESSION,
               header=None):
        super(TextFileSinkWithHeader, self).__init__(
            file_path_prefix,
            file_name_suffix=file_name_suffix,
            num_shards=num_shards,
            shard_name_template=shard_name_template,
            coder=coder,

            compression_type=compression_type,
            append_trailing_newlines=append_trailing_newlines)
        self.header = header

    def open(self, temp_path):
        channel_factory = ChannelFactory.open(
            temp_path,
            'wb',
            mime_type=self.mime_type)
        channel_factory.write(self.header+"\n")
        return channel_factory

您随后可以按如下方式使用它:

beam.io.Write(TextFileSinkWithHeader('./names_w_headers',header="names"))

有关完整概述,请参阅笔记本

于 2016-09-21T19:58:53.913 回答
0

Python SDK 中尚不存在此功能

于 2016-09-21T19:57:26.983 回答
0

对于 Python SDK:

beam.io.Write(beam.io.WriteToText(
                     file_path_prefix=os.path.join(OUTPUT_DIR),
                     file_name_suffix='.csv', header='colname1, colname2')
              )
于 2021-03-24T17:09:17.050 回答