Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: Tim MeagherHi,
The general process is to setup the output metadata for each of the outputs you are writing to in the "setup" method.
This involves:
For each output
- Construct a new metadata object, using:
output(<outputIndex>).newMetadata();
e.g.
RecordMetadata metadata = output(0).newMetadata();
- Setup the metadata for that output, by adding all of the fields that you want on the output, using:
metadata.add(new SimpleFieldMetadata("<FieldName", <FieldClass>);
e.g.
metadata.add(new SimpleFieldMetadata("MyNewField", java.lang.String.class);
metadata.add(new SimpleFieldMetadata("My Integer Field", java.lang.Integer.class);
Where <FieldClass> is the Class of the field
- For a "string" field, this is java.lang.String.class, for a "double" field, tihs is java.lang.Double.class (or just double.class), etc.
- For time, date, and datetime fields (which don't have direct corresponding java classes, use com.lavastorm.lang.Time.class, com.lavastorm.lang.Date.class, and com.lavastorm.lang.TimeStamp.class.
- For "unicode" types, this is com.lavastorm.lang.UnicodeString.class
- Assign the metadata to the output, using:
output(<outputIndex>.metadata(<metadataObject>);
- e.g.
output(0).metadata(metadata);
Then, open all of the outputs, using:
Once this is done, within the processAll method, where you are processing input data (or doing whatever other processing is required), you can write to each of these outputs.
In order to write to these outputs, you must:
- Get a new Record object from the output, using:
Record <recordObject> = output(<outputIndex>).metadata().newRecord();
e.g.
Record rec = output(0).metadata().newRecord();
- Set all of the values for the different fields, using:
<recordObject>.field(<fieldIndex>, <fieldValue>);
e.g.
rec.field(0, "My String Field");
rec.field(1, 123);
- Write the output record, using:
write(<outputIndex>, record);
e.g.
So in summary, if you wanted two fields on the node's first output - one string field called "MyNewField", one integer field called "My Integer Field", you'd set up using:
RecordMetadata rmd = output(0).newMetadata();
rmd.add(new SimpleFieldMetadata("MyNewField", java.lang.String.class);
rmd.add(new SimpleFieldMetadata("My Integer Field", int.class);
output(0).metadata(rmd);
openOutputs();
Then to write the values "fred" and 123 to to an output record, you would use:
Record rec = output(0).metadata().newRecord();
rec.field(0, "fred");
rec.field(1, 123);
write(0, rec);
This is talked through pretty well with examples in the Java Node Getting Started Guide and via the API, but if you have more questions, feel free to post here.
Hope this helps,
Tim.