Builder pattern for FSDataOutputStream and its subclasses. It is used to create a new file or open an existing file on FileSystem for write.
The FSDataOutputStreamBuilder interface does not validate parameters and modify the state of FileSystem until build() is invoked.
Specify FSDataOutputStreamBuilder to create a file on FileSystem, equivalent to CreateFlag#CREATE.
Specify FSDataOutputStreamBuilder to append to an existing file on FileSystem, equivalent to CreateFlag#APPEND.
Specify FSDataOutputStreamBuilder to overwrite an existing file or not. If giving overwrite==true, it truncates an existing file, equivalent to CreateFlag#OVERWITE.
FSDataOutputStreamBuilder opt(String key, ...) FSDataOutputStreamBuilder must(String key, ...)
Set optional or mandatory parameters to the builder. Using opt() or must(), client can specify FS-specific parameters without inspecting the concrete type of FileSystem.
// Don't if (fs instanceof FooFileSystem) { FooFileSystem fs = (FooFileSystem) fs; out = dfs.createFile(path) .optionA() .optionB("value") .cache() .build() } else if (fs instanceof BarFileSystem) { ... } // Do out = fs.createFile(path) .permission(perm) .bufferSize(bufSize) .opt("foofs:option.a", true) .opt("foofs:option.b", "value") .opt("barfs:cache", true) .must("foofs:cache", true) .must("barfs:cache-size", 256 * 1024 * 1024) .build();
The concrete FileSystem and/or FSDataOutputStreamBuilder implementation MUST verify that implementation-agnostic parameters (i.e., “syncable”) or implementation-specific parameters (i.e., “foofs:cache”) are supported. FileSystem will satisfy optional parameters (via opt(key, ...)) on best effort. If the mandatory parameters (via must(key, ...)) can not be satisfied in the FileSystem, IllegalArgumentException must be thrown in build().
The behavior of resolving the conflicts between the parameters set by builder methods (i.e., bufferSize()) and opt()/must() is as follows:
The last option specified defines the value and its optional/mandatory state.
HdfsDataOutputStreamBuilder extends FSDataOutputStreamBuilder provides additional HDFS-specific parameters, for further customize file creation / append behavior.
Set favored DataNodes for new blocks.
Force closed blocks to the disk device. See CreateFlag#SYNC_BLOCK
Append data to a new block instead of the end of the last partial block.
Advise that a block replica NOT be written to the local DataNode.
Create a new file or append an existing file on the underlying FileSystem, and return FSDataOutputStream for write.
The following combinations of parameters are not supported:
if APPEND|OVERWRITE: raise HadoopIllegalArgumentException if CREATE|APPEND|OVERWRITE: raise HadoopIllegalArgumentExdeption
FileSystem may reject the request for other reasons and throw IOException, see FileSystem#create(path, ...) and FileSystem#append().