Search in sources :

Example 1 with TransactionalFileOutput

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;
        }
    };
}
Also used : Buffer(org.embulk.spi.Buffer) TaskReport(org.embulk.config.TaskReport) FileOutputStream(java.io.FileOutputStream) TransactionalFileOutput(org.embulk.spi.TransactionalFileOutput) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TaskReport (org.embulk.config.TaskReport)1 Buffer (org.embulk.spi.Buffer)1 TransactionalFileOutput (org.embulk.spi.TransactionalFileOutput)1