Skip to content

Routes: CSV Writer

TekMonks edited this page Mar 14, 2019 · 1 revision

The CSV File Writer

The CSV File Writer route node is used to write data to files in CSV format. The csv file write node writes the entire message to the file at once. However, it is capable of appending, thus, writing multiple messages to the same file in append mode, if configured to do so.

This node will parse the parse the JSON data itself, and convert it to CSV. The property message.content contains the JSON data to write out as CSV.

Each property of the message.content is converted into a CSV value and written out. The message keys are converted to CSV header row, if no custom headers are specified.

If custom headers are specified, then each header is assigned a value which enumerates to the same position as the header in the incoming message. Thus, it is safer to not specify custom headers, but use the JSON message keys as the headers.

The code block below documents the format for the CSV File Writer output node.

"route0":{
	"type": "csvwriter",
	"dependencies":["listener"],
	"headers":["firstname", "lastname"],
	"path":"c:/test/json2csv.csv",
	"encoding":"utf8",
	"timeout":5000
}

The headers property specifies the CSV headers, as discussed above. This is a dangerous property to use and not recommended.

The path property is used to point to the file to write to.

The encoding property indicates file encoding to use.

The timeout is in milliseconds. It indicates the number of seconds the csv file writer waits, for any output, before it closes the file handle. This is used for enhancing the performance and reduces the open file handles the code needs to maintain. For example, if millions of messages are being written to the file, the csv file writer with a 5 second timeout will not keep opening new file handles for each message, as long as that message arrives within a 5 second window.

The following code block shows the csv file writer being used together to transform and write an incoming REST message to a CSV file.

{
    "flow": {
		"name":"REST API to write to a local CSV file",
		"disabled":true,
		"autoGC":false
    },
    "listener": {
		"type":"rest_listener", 
		"isMessageGenerator": true,
		"host": "127.0.0.1",
		"port":9090,
		"url":"/tocsv",
		"allow_origin": "*",
		"timeout": 120000
    },
    "route0":{
		"type": "csvwriter",
		"dependencies":["listener"],
		"headers":["firstname", "lastname"],
		"path":"c:/test/json2csv.csv",
		"encoding":"utf8",
		"timeout":5000
    },
    "route1": {
		"type":"js",
		"dependencies":"route0",
		"js":"message.content = {}; message.content.result=true; "
    },
    "route2": {
		"type":"js",
		"dependencies":"route0.error",
		"js":"message.content = {}; message.content.result=false; "
    },
    "output": {
		"type":"rest_responder",
		"dependencies":[["route1"], ["route2"]]
    },
    "garbagecollector": {
		"type": "simple",
		"dependencies":"output",
		"isMessageConsumer": true
    }
}