This is not implemented at this moment. However you can implement/extend it yourself (see attached notebook for an example+test with my version of apache_beam).
This is based on a note in the docstring of the superclass FileSink
, mentioning that you should overwrite the open
function:
The new class that works for my version of 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
You can subsequently use it as follows:
beam.io.Write(TextFileSinkWithHeader('./names_w_headers',header="names"))
See the notebook for the complete overview.