use of org.embulk.spi.TransactionalFileOutput in project embulk by embulk.
the class LocalFileOutputPlugin method open.
@Override
public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) {
PluginTask task = taskSource.loadTask(PluginTask.class);
final String pathPrefix = task.getPathPrefix();
final String pathSuffix = task.getFileNameExtension();
final String sequenceFormat = task.getSequenceFormat();
return new TransactionalFileOutput() {
private final List<String> fileNames = new ArrayList<>();
private int fileIndex = 0;
private FileOutputStream output = null;
public void nextFile() {
closeFile();
String path = pathPrefix + String.format(sequenceFormat, taskIndex, fileIndex) + pathSuffix;
log.info("Writing local file '{}'", path);
fileNames.add(path);
try {
output = new FileOutputStream(new File(path));
} catch (FileNotFoundException ex) {
// TODO exception class
throw new RuntimeException(ex);
}
fileIndex++;
}
private void closeFile() {
if (output != null) {
try {
output.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
public void add(Buffer buffer) {
try {
output.write(buffer.array(), buffer.offset(), buffer.limit());
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
buffer.release();
}
}
public void finish() {
closeFile();
}
public void close() {
closeFile();
}
public void abort() {
}
public TaskReport commit() {
TaskReport report = Exec.newTaskReport();
// report.set("file_sizes", fileSizes);
return report;
}
};
}
Aggregations