How to copy an input schema with field changes in a custom Java operator
book
Article ID: KB0083834
calendar_today
Updated On:
Description
In the StreamBase Schema API, schema objects are immutable, so once created they are not allowed to be changed.
What is the most efficient way to create a new schema based on, but not identical to, a schema provided on an input port?
Resolution
The output schema for a port must be created from an array of fields. A schema cannot be cloned from another schema and then changed.
The process is:
1. In typecheck(), define a new list of fields.
ArrayList<Field> fields = new ArrayList<Field>();
2. Copy each field from the input schema. When you identify the field that needs to be different, make that change.
For example:
for (int i=0; i<getInputSchema(0).getFieldCount(); i++) {
// change fields from String to Int
if (getInputSchema(0).getField(i).getDataType() == DataType.STRING) {
// add it as an Int field
fields.add(Schema.createField(DataType.INT, getInputSchema(0).getField(i).getName()));
} else {
// add it as found
fields.add(getInputSchema(0).getField(i));
}
}
Your criteria for identifying the field to change, or remove, may be any characteristic of the field, such as its data type, name, or position in the schema. The order fields are added to the list are the order they will appear in the new schema. Fields of complex types (tuple and list-of-tuple) may need their own secondary ArrayList lists to completely define their fields for the create*() statement. New fields may be added the same way.
3. Set the output Schema
Schema tempSch = new Schema(null,fields);
setOutputSchema(0, tempSch);
4. In init(), get the resultant output schema for use in processTuple() and sendOutput()
outSch = getRuntimeOutputSchema(0);
Issue/Introduction
Java steps to copy a schema allowing for changes.
Feedback
thumb_up
Yes
thumb_down
No