Top Description Inners Fields Constructors Methods
org.apache.avro.file

public Class DataFileWriter<D>

extends Object
implements Closeable, Flushable
Class Inheritance
All Implemented Interfaces
java.io.Flushable, java.io.Closeable, java.lang.AutoCloseable
Static Imports
java.nio.charset.StandardCharsets.UTF_8

Stores in a file a sequence of data conforming to a schema. The schema is stored in the file with the data. Each datum in a file is of the same schema. Data is written with a DatumWriter. Data is grouped into blocks. A synchronization marker is written between blocks, so that files may be split. Blocks may be compressed. Extensible metadata is stored at the end of the file. Files may be appended to.
See Also
DataFileReader

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
DataFileWriter.AppendWriteException

Thrown by append(Object) when an exception occurs while writing a datum to the buffer.

private class

Field Summary

Modifier and TypeField and Description
private long
private NonCopyingByteArrayOutputStream
private BinaryEncoder
private Codec
private DatumWriter<D>
private boolean
private boolean
private final Map<String, byte[]>
private DataFileWriter<D>.BufferedFileOutputStream
private Schema
private byte[]
private int
private OutputStream
private BinaryEncoder

Constructor Summary

AccessConstructor and Description
public
DataFileWriter(DatumWriter<D> dout)

Construct a writer, not yet open.

Method Summary

Modifier and TypeMethod and Description
public void
append(D datum)

Append a datum to the file.

public void
appendAllFrom(DataFileStream<D> otherFile, boolean recompress)

Appends data from another file.

public void
appendEncoded(ByteBuffer datum)

Expert: Append a pre-encoded datum to the file.

public DataFileWriter<D>
appendTo(File file)

Open a writer appending to an existing file.

public DataFileWriter<D>
appendTo(SeekableInput
reading the existing file.
in
,
OutputStream
positioned at the end of the existing file.
out
)

Open a writer appending to an existing file.

private void
private void
private int
public void
close()

Implements java.io.Closeable.close.

Flush and close the file.
public DataFileWriter<D>
create(Schema schema, File file)

Open a new file for data matching a schema with a random sync.

public DataFileWriter<D>
create(Schema schema, OutputStream outs)

Open a new file for data matching a schema with a random sync.

public DataFileWriter<D>
create(Schema schema, OutputStream outs, byte[] sync)

Open a new file for data matching a schema with an explicit sync.

public void
flush()

Implements java.io.Flushable.flush.

Calls sync() and then flushes the current state of the file.
public void
fSync()

If this writer was instantiated using a File or using an Syncable instance, this method flushes all buffers for this writer to disk.

private static byte[]
private void
public boolean

Returns:

- true if this writer flushes the block to the stream every time a sync marker is written. Else returns false.
isFlushOnEveryBlock
()

public static boolean
private void
resetBufferTo(int size)

public DataFileWriter<D>
setCodec(CodecFactory c)

Configures this writer to use the given codec.

public void
setFlushOnEveryBlock(boolean
- If set to false, this writer will not flush the block to the stream until flush() is explicitly called.
flushOnEveryBlock
)

Set whether this writer should flush the block to the stream every time a sync marker is written.

public DataFileWriter<D>
setMeta(String key, byte[] value)

Set a metadata property.

public DataFileWriter<D>
setMeta(String key, String value)

Set a metadata property.

public DataFileWriter<D>
setMeta(String key, long value)

Set a metadata property.

private DataFileWriter<D>
setMetaInternal(String key, byte[] value)

private DataFileWriter<D>
public DataFileWriter<D>

Returns:

this DataFileWriter
setSyncInterval
(int
the approximate number of uncompressed bytes to write in each block
syncInterval
)

Set the synchronization interval for this file, in bytes.

public long
sync()

Return the current position as a value that may be passed to DataFileReader#seek(long).

private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blockCountback to summary
private long blockCount
bufferback to summary
private NonCopyingByteArrayOutputStream buffer
bufOutback to summary
private BinaryEncoder bufOut
codecback to summary
private Codec codec
doutback to summary
private DatumWriter<D> dout
flushOnEveryBlockback to summary
private boolean flushOnEveryBlock
isOpenback to summary
private boolean isOpen
metaback to summary
private final Map<String, byte[]> meta
outback to summary
private DataFileWriter<D>.BufferedFileOutputStream out
schemaback to summary
private Schema schema
syncback to summary
private byte[] sync
syncIntervalback to summary
private int syncInterval
underlyingStreamback to summary
private OutputStream underlyingStream
voutback to summary
private BinaryEncoder vout

Constructor Detail

DataFileWriterback to summary
public DataFileWriter(DatumWriter<D> dout)

Construct a writer, not yet open.

Method Detail

appendback to summary
public void append(D datum) throws IOException

Append a datum to the file.

See Also
AppendWriteException
appendAllFromback to summary
public void appendAllFrom(DataFileStream<D> otherFile, boolean recompress) throws IOException

Appends data from another file. otherFile must have the same schema. Data blocks will be copied without de-serializing data. If the codecs of the two files are compatible, data blocks are copied directly without decompression. If the codecs are not compatible, blocks from otherFile are uncompressed and then compressed using this file's codec.

If the recompress flag is set all blocks are decompressed and then compressed using this file's codec. This is useful when the two files have compatible compression codecs but different codec options. For example, one might append a file compressed with deflate at compression level 1 to a file with deflate at compression level 7. If recompress is false, blocks will be copied without changing the compression level. If true, they will be converted to the new compression level.

appendEncodedback to summary
public void appendEncoded(ByteBuffer datum) throws IOException

Expert: Append a pre-encoded datum to the file. No validation is performed to check that the encoding conforms to the file's schema. Appending non-conforming data may result in an unreadable file.

appendToback to summary
public DataFileWriter<D> appendTo(File file) throws IOException

Open a writer appending to an existing file.

appendToback to summary
public DataFileWriter<D> appendTo(SeekableInput in, OutputStream out) throws IOException

Open a writer appending to an existing file. Since 1.9.0 this method does not close in.

Parameters
in:SeekableInput

reading the existing file.

out:OutputStream

positioned at the end of the existing file.

assertNotOpenback to summary
private void assertNotOpen()
assertOpenback to summary
private void assertOpen()
bufferInUseback to summary
private int bufferInUse()
closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Flush and close the file.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.Closeable.close.

if an I/O error occurs

createback to summary
public DataFileWriter<D> create(Schema schema, File file) throws IOException

Open a new file for data matching a schema with a random sync.

createback to summary
public DataFileWriter<D> create(Schema schema, OutputStream outs) throws IOException

Open a new file for data matching a schema with a random sync.

createback to summary
public DataFileWriter<D> create(Schema schema, OutputStream outs, byte[] sync) throws IOException

Open a new file for data matching a schema with an explicit sync.

flushback to summary
public void flush() throws IOException

Implements java.io.Flushable.flush.

Calls sync() and then flushes the current state of the file.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.Flushable.flush.

If an I/O error occurs

fSyncback to summary
public void fSync() throws IOException

If this writer was instantiated using a File or using an Syncable instance, this method flushes all buffers for this writer to disk. In other cases, this method behaves exactly like flush().

generateSyncback to summary
private static byte[] generateSync()
initback to summary
private void init(OutputStream outs) throws IOException
isFlushOnEveryBlockback to summary
public boolean isFlushOnEveryBlock()
Returns:boolean

- true if this writer flushes the block to the stream every time a sync marker is written. Else returns false.

isReservedMetaback to summary
public static boolean isReservedMeta(String key)
resetBufferToback to summary
private void resetBufferTo(int size) throws IOException
setCodecback to summary
public DataFileWriter<D> setCodec(CodecFactory c)

Configures this writer to use the given codec. May not be reset after writes have begun.

setFlushOnEveryBlockback to summary
public void setFlushOnEveryBlock(boolean flushOnEveryBlock)

Set whether this writer should flush the block to the stream every time a sync marker is written. By default, the writer will flush the buffer each time a sync marker is written (if the block size limit is reached or the sync() is called.

Parameters
flushOnEveryBlock:boolean

- If set to false, this writer will not flush the block to the stream until flush() is explicitly called.

setMetaback to summary
public DataFileWriter<D> setMeta(String key, byte[] value)

Set a metadata property.

setMetaback to summary
public DataFileWriter<D> setMeta(String key, String value)

Set a metadata property.

setMetaback to summary
public DataFileWriter<D> setMeta(String key, long value)

Set a metadata property.

setMetaInternalback to summary
private DataFileWriter<D> setMetaInternal(String key, byte[] value)
setMetaInternalback to summary
private DataFileWriter<D> setMetaInternal(String key, String value)
setSyncIntervalback to summary
public DataFileWriter<D> setSyncInterval(int syncInterval)

Set the synchronization interval for this file, in bytes. Valid values range from 32 to 2^30 Suggested values are between 2K and 2M The stream is flushed by default at the end of each synchronization interval. If setFlushOnEveryBlock(boolean) is called with param set to false, then the block may not be flushed to the stream after the sync marker is written. In this case, the flush() must be called to flush the stream. Invalid values throw IllegalArgumentException

Parameters
syncInterval:int

the approximate number of uncompressed bytes to write in each block

Returns:DataFileWriter<D>

this DataFileWriter

syncback to summary
public long sync() throws IOException

Return the current position as a value that may be passed to DataFileReader#seek(long). Forces the end of the current block, emitting a synchronization marker. By default, this will also flush the block to the stream. If setFlushOnEveryBlock(boolean) is called with param set to false, then this method may not flush the block. In this case, the flush() must be called to flush the stream.

writeBlockback to summary
private void writeBlock() throws IOException
writeIfBlockFullback to summary
private void writeIfBlockFull() throws IOException
org.apache.avro.file back to summary

public Class DataFileWriter.AppendWriteException

extends RuntimeException
Class Inheritance

Thrown by append(Object) when an exception occurs while writing a datum to the buffer. When this is thrown, the file is unaltered and may continue to be appended to.

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AppendWriteExceptionback to summary
public AppendWriteException(Exception e)
org.apache.avro.file back to summary

private Class DataFileWriter.BufferedFileOutputStream

extends BufferedOutputStream
Class Inheritance

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private long
Inherited from java.io.BufferedOutputStream:
bufcount

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public synchronized void
flush()

Overrides java.io.BufferedOutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this buffered output stream.
public long
tell()

Inherited from java.io.BufferedOutputStream:
writewrite

Field Detail

positionback to summary
private long position

Constructor Detail

BufferedFileOutputStreamback to summary
public BufferedFileOutputStream(OutputStream out) throws IOException

Method Detail

flushback to summary
public synchronized void flush() throws IOException

Overrides java.io.BufferedOutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.BufferedOutputStream.flush.

Flushes this buffered output stream. This forces any buffered output bytes to be written out to the underlying output stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
tellback to summary
public long tell()
org.apache.avro.file back to summary

private Class DataFileWriter.BufferedFileOutputStream.PositionFilter

extends FilterOutputStream
Class Inheritance

Field Summary

Inherited from java.io.FilterOutputStream:
out

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
write(byte[]
the data.
b
,
int
the start offset in the data.
off
,
int
the number of bytes to write.
len
)

Overrides java.io.FilterOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.
Inherited from java.io.FilterOutputStream:
closeflushwritewrite

Constructor Detail

PositionFilterback to summary
public PositionFilter(OutputStream out) throws IOException

Method Detail

writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Overrides java.io.FilterOutputStream.write.

Doc from java.io.FilterOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.