Write to Several Data Writers at Once

This example shows you how to write to several files at once in Java using the MultiWriter class.

The demo code creates some test data in memory and uses a MultiWriter to write it to both a CSV file and Excel file using a single method call. However, other readers/writers can be used instead of or in addition to CSVWriter and ExcelWriter.

Please refer to open and close several readers/writers at once which shows how to open/close several readers/writers in a single method call.

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.core.MultiWriter;
import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.core.RecordList;
import com.northconcepts.datapipeline.csv.CSVWriter;
import com.northconcepts.datapipeline.excel.ExcelDocument;
import com.northconcepts.datapipeline.excel.ExcelWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.memory.MemoryReader;

public class WriteToSeveralDataWritersAtOnce {

    public static void main(String[] args) {
        Record record1 = new Record();
        record1.setField("name", "John Wayne");
        record1.setField("balance", 156.35);

        Record record2 = new Record();
        record2.setField("name", "Peter Parker");
        record2.setField("balance", 0.96);
        
        DataReader reader = new MemoryReader(new RecordList(record1, record2));
        
        DataWriter writer1 = new CSVWriter(new File("example/data/output/credit-balance-03.csv"));
        
        ExcelDocument document = new ExcelDocument();
        DataWriter writer2 = new ExcelWriter(document)
                .setSheetName("balance");
        
        Job.run(reader, new MultiWriter(writer1, writer2));
        
        document.save(new File("example/data/output/credit-balance-03.xls"));
    }
    
}

Code Walkthrough

  1. Two Record objects are created in memory with the fields name and balance set to some test values.
  2. A RecordList is created and these Record objects are added to it.
  3. A MemoryReader is created to read this RecordList.
  4. A new CSVWriter is created corresponding to the output CSV file credit-balance-03.csv.
  5. ExcelWriter and ExcelDocument classes are created corresponding to the output Excel file.
  6. A new MultiWriter class is created and the CSVWriter and ExcelWriter classes are added to it via the MultiWriter.add method.
  7. Data is transferred from memory to the output CSV file and Excel file via JobTemplate.DEFAULT.transfer method.
  8. The Excel document is finally saved at its output path credit-balance-03.xls

MultiWriter

A MultiWriter class can be used to write to several output files at once. It has an add(DataWriter) method using which several destination writers can be added to the MultiWriter. Writing any output to the MultiWriter internally writes data to the target writers.

Output CSV file

name,balance
John Wayne,156.35
Peter Parker,0.96

Output Excel file

Mobile Analytics