Script plugins

Datahub documentation home

You can write message readers and message filters in JavaScript.

To do this, set the message reader to com.datahub.metrici.ScriptPlugIn, and set the message reader config to your script.

Your script will have a global variable called "attributes", which is a map with get and put operations. From this you can read:

Message readers are passed the attribute "data" which contains a string representation of the message data, and should set the attribute "records" to an array of record objects, or the JSON equivalent.

var out = [];
// do something with data here
attributes.put('records',out); // (could use JSON.stringify(out), but that would be less efficient)

Message filters are passed the attribute "record" which contains the record. This may be a string or a JavaScript object, depending on the underlying data. Message filters can leave the record unchanged, or set the attribute "record" to a different record, or to null to delete the record from the input. If the parseJSON option is set in the message filter, message filters can simply update the record, without setting the record attribute again.

You can read the message options object from the string returned by attributes.get('options'). If options have not been set, this will be set to null.

The scripts use the scripting engine built into Java. This allows you to access Java classes to do what you need.

The script-based message reader only allows you to return all records as a JavaScript array of JSON-formatted string. This is fine for a single or a small number of records, but could cause memory problems for large records. In this case, a combination of a reader and a filter may work better.

The online data portal allows you to test message reader scripts. This tester does not provide access to other Java classes, but will let you test script-based data transformations.