I am using Clover to process some data and right it to a csv file.
Thing is I want the csv headers (first line) to be named a bit differently than the fields given in the metadata.
e.g. If I have a field name NumberOfCustomers I want it to appear as “Number Of Customer” in the csv file header. (mind the space between the words).
Is it possible to achieve in Clover ?
Thanks
and ofcourse i meant “write a csv file” not “right …”
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.
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 !
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
-
DataWithHeader.grf
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
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).