cancel
Showing results for 
Search instead for 
Did you mean: 

Merge multiple documents to an array in ultra pipeline

thilkip
New Contributor

We need to merge multiple documents to single array and looking for help. Since it is ultra task, we are running out of options…

Input data:

[
{“Record”: “1”},
{“Record”: “2”},
{“Record”: “5”},
{“Record”: “4”},
{“Record”: “6”}
]

Expected Output:
[ “response”: [{“Record”: “1”}, {“Record”: “2”}, {“Record”: “5”}, {“Record”: “4”}, {“Record”: “6”}]
]

25 REPLIES 25

j_angelevski
Contributor III

Hi @thilkip ,
Use Pipeline Execute with the option Reuse executions to process documents set to disabled. Inside this pipeline put your input data and use group by n. Something like this:
image
image

yes. this will work but we dont want to call child as it is a ultra task and wanted to have ms response time

A triggered task has more latency than ultra, but doesn’t have any restriction on types of Snaps. Is the performance suitable for your use case?

not really, that is the challenge we are running into…looking to get the millisecond response from api using ultra task

j_angelevski
Contributor III

Use this script, it groups all inputs into single array:

execute : function () {
   this.log.info("Executing Transform Script");
    var groupByN = new ArrayList();
    var wrapper = {};
    while (this.input.hasNext()) {
        try {
            // Read the next input document, store it a new LinkedHashMap, and write this as an output document.
            // We must use a serializable Java type liked LinkedHashMap for each output instead of a native
            // JavaScript object so that downstream Snaps like Copy can process it correctly.
            var inDoc = this.input.next();

            groupByN.add(inDoc);
            wrapper = {"Data": groupByN};
        }
        catch (err) {
            var errDoc = new LinkedHashMap();
            errDoc.put("error", err);
            this.log.error(err);
            this.error.write(errDoc);
        }
    }
    this.output.write(wrapper);
    this.log.info("Script executed");
}

I tested it with ultra task and it seems it works.