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

bojanvelevski
Valued Contributor

Hello @thilkip,

You can try and use Group by N snap. Set the group size to 0 and this is what you get:

image

Regards,
Bojan

yes, but Group by is not supported in Ultra tasks

Sorry, I missed that information.

We tried with script as below. it works in design mode but not when we call ultra tasks. When we call ultra tasks the script snap doesnt provide any output

execute : function () {
this.log.info(โ€œExecuting Transform Scriptโ€);
var outDoc = new LinkedHashMap();
var inDoc=null;
var items = new ArrayList();
var i=0;

	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.
			inDoc = this.input.next();
			i=i+1;
			items.add(inDoc);

			outDoc.put("index", i);         
			
			if ((!this.input.hasNext()) || (this.input.hasNext()==false))
			{   
				outDoc.put("data", items);
				this.output.write(inDoc, outDoc);
			}
			
		}
		catch (err) {
			var errDoc = new LinkedHashMap();
			errDoc.put("error", err);
			this.log.error(err);
			this.error.write(errDoc);
		}
	}
     
    this.log.info("Script executed");