# Writing CSV file headers

**URL:** <https://forum.cloverdx.com/t/writing-csv-file-headers/890>\
**Category:** CloverDX Platform\
**Created:** [July 13, 2011, 12:00am UTC](https://forum.cloverdx.com/t/writing-csv-file-headers/890 "2011-07-13T00:00:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![giorashc](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@giorashc](https://forum.cloverdx.com/u/giorashc)\
**Post date:** [July 13, 2011, 12:00am UTC](https://forum.cloverdx.com/t/writing-csv-file-headers/890/1 "2011-07-13T00:00:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giorashc](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@giorashc](https://forum.cloverdx.com/u/giorashc)\
**Post date:** [July 13, 2011, 3:27pm UTC](https://forum.cloverdx.com/t/writing-csv-file-headers/890/2 "2011-07-13T15:27:59Z")

</div>

and ofcourse i meant “write a csv file” not “right …” 🙂

---

<div class="post-metadata">

**Author:** ![avackova](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@avackova](https://forum.cloverdx.com/u/avackova)\
**Post date:** [July 14, 2011, 7:06am UTC](https://forum.cloverdx.com/t/writing-csv-file-headers/890/3 "2011-07-14T07:06:47Z")

</div>

You would need to add the graph branch, that does the task, before the writing:

DataGenerator → DataWriter

with the code like:

```auto
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.

---

<div class="post-metadata">

**Author:** ![giorashc](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@giorashc](https://forum.cloverdx.com/u/giorashc)\
**Post date:** [July 14, 2011, 8:29am UTC](https://forum.cloverdx.com/t/writing-csv-file-headers/890/4 "2011-07-14T08:29:40Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![avackova](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@avackova](https://forum.cloverdx.com/u/avackova)\
**Post date:** [July 14, 2011, 8:47am UTC](https://forum.cloverdx.com/t/writing-csv-file-headers/890/5 "2011-07-14T08:47:32Z")

</div>

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](https://cdn.cloverdx.com/forum/417%5F24e2d3b4fe5f4a0b0f4184a24f9addc6/DataWithHeader.grf)

---

<div class="post-metadata">

**Author:** ![giorashc](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@giorashc](https://forum.cloverdx.com/u/giorashc)\
**Post date:** [July 18, 2011, 1:56pm UTC](https://forum.cloverdx.com/t/writing-csv-file-headers/890/6 "2011-07-18T13:56:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![avackova](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@avackova](https://forum.cloverdx.com/u/avackova)\
**Post date:** [July 18, 2011, 2:24pm UTC](https://forum.cloverdx.com/t/writing-csv-file-headers/890/7 "2011-07-18T14:24:39Z")

</div>

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).
