Customer Portal

Writing CSV file headers

Comments 6

  • Avatar
    giorashc
    0
    Comment actions Permalink
    and ofcourse i meant "write a csv file" not "right ...." :)
  • Avatar
    avackova
    0
    Comment actions Permalink
    You would need to add the graph branch, that does the task, before the writing:

    DataGenerator --> DataWriter

    with the code like:
    import org.jetel.component.DataRecordGenerate;
    import org.jetel.data.DataRecord;
    import org.jetel.exception.ComponentNotReadyException;
    import org.jetel.exception.TransformException;
    import org.jetel.metadata.DataRecordMetadata;


    public class CreateHeader extends DataRecordGenerate {

    DataRecordMetadata outputMetadata;

    @Override
    public boolean init() throws ComponentNotReadyException {
    super.init();
    outputMetadata = getDataRecordMetadata("Metadata0");
    return true;
    }

    @Override
    public int generate(DataRecord[] arg0) throws TransformException {
    for (int i = 0; i < arg0[0].getNumFields(); i++) {
    arg0[0].getField(i).fromString(transformName(i));
    }
    return 0;
    }

    private String transformName(int fieldNumber) {
    StringBuffer name = new StringBuffer(outputMetadata.getField(fieldNumber).getName());
    for (int i = 1; i < name.length(); i++) {//i starts with 1 as we don't want space before the first letter
    if (Character.isUpperCase(name.charAt(i))) {
    name.insert(i, ' ');
    i++;//move after capital letter
    }
    }
    return name.toString();
    }

    }

    Don't forget to change append attribute to true in the Writer, that writes data.
  • Avatar
    giorashc
    0
    Comment actions Permalink
    thanks for the reply avackova.

    How do I use this datagenerator in my graph ? (As I see it does not have any input ports only one output port)
    I am new to the Clover so branching a graph is not something I am familiar with.

    My graph reads a large number of records and transforms them into a csv file using a universal writer.

    I do not undestand how to incorporate your example with my graph. (which looks in short like : reader-->reformat-->aggregate-->writer)

    Can you please explain ?

    Thanks alot !
  • Avatar
    avackova
    0
    Comment actions Permalink
    Please try the attached graph.

    Note, that:
    • Metadata on Edge2 corresponds with metadata on Edge1, but it have all fields of the type string

    • Both DataWriters have the same fileURL, but the append attribute on the second Writer is set to true
  • Avatar
    giorashc
    0
    Comment actions Permalink
    Thanks for the reply looks like what I was looking for.
    One more thing though, how do I make sure the headers will always be written before the actual data ?

    Thanks
  • Avatar
    avackova
    0
    Comment actions Permalink
    Hello,
    the branch DataGenerator --> Writer needs to run in earlier phase, than the rest of the graph (in attached example header is saved in phase 0 and data in phase 1).

Please sign in to leave a comment.