Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: Tim MeagherHey,
So further to the previous posts....
The main problem was the use of the following:
Record inRec = read(0);
...
inRec.field("TaskID",taskID);
write(0,inRec);
The inRec Record will have it's metadata based on the metadata of the input pin from which it is read. In this case, input 0.
However, the output pin 0 has a different metadata set.
This is based on the input metadata, but contains the additional field "TaskId".
The Record inRec will have it's metadata set such that there is no field "TaskId". Therefore, attempting to to set the field "TaskId" will result in an error.
The correct way, as Rocco rightly points out is to construct a new output Record based on the metadata of the output pin you are writing to, using:
Record outRec = output(outputIndex).newMetadata();
As a final note, the code:
outputRecord.copyFrom(inputRecord);
Is not the most efficient way to map all of the fields on the input record to the output record. This is because there is no mapping provided to the outputRecord for it to know which fields in the inputRecord should be placed on which output fields. Therefore, when populating each output record, it has to obtain all of the metadata from the inputRecord, and locate (i.e. do a textual search on the field name of each field in the output metadata) where that should be placed on the outputRecord.
The more efficient way to do this would be to do the following:
private int[] m_mapping;
public void setup() throws NodeFailedException
{
RecordMetadata metadata = output(0).newMetadata();
metadata.copyFrom(input(0).metadata());
metadata.add(new SimpleFieldMetadata("TaskID",java.lang.String.class));
output(0).metadata(metadata);
m_mapping = metadata.mapping(input(0).metadata());
openOutput(0);
}
public void processAll() throws NodeFailedException
{
Record outputRecord = output(0).metadata().newRecord();
String taskID = "10";
Record inputRecord = read(0);
int taskIdIndex = output(0).metadata().find("TaskID");
while (inputRecord != RecordInput.EOF)
{
outputRecord.copyFrom(inputRecord, m_mapping);
outputRecord.field(taskIdIndex,taskID);
write(0,outputRecord);
inputRecord = read(0);
}
}
Regards,
Tim.