cancel
Showing results for 
Search instead for 
Did you mean: 

Merging output of one document in next

krupalibshah
Contributor

I am working on a pipeline where I am reading data from CSV generator.

I have 2 types of data H line and I line for each one I need to do some mapping, which is fine. the final output I need to sent to SAP with a format H + I

So I need to merge the H line to each I line and then post to SAP.
Can someone help me achieve this? As all the values are in an independent document I am not able to do it easily. I tried Join and Union followed bu Group by but still not getting desired result.

image

image

image

Desired result:

image

/KS

10 REPLIES 10

jaybodra
New Contributor III

My understanding about the question is you have a csv with a header and line(s). you are processing them differently and after that you want the data in CSV style first line is header and rest is content (line/s).

If so I would like to suggest two different solutions :

  1. use a json splitter right after the CSV snap. you can configure it as:
    image

this can take sometime during insertion but each line will have a header


  1. after doing union you can use a script snap. Here is my jython (python) code. You can use JS or ruby:

#Import the interface required by the Script snap.
from com.snaplogic.scripting.language import ScriptHook
import java.util

class TransformScript(ScriptHook):
def init(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log

#The “execute()” method is called once when the pipeline is started
# and allowed to process its inputs or just send data to its outputs.
def execute(self):
self.log.info(“Executing Transform script”)
wrapper = java.util.ArrayList()

   while self.input.hasNext():
       try:
           # Read the next document, wrap it in a map and write out the wrapper
           in_doc = self.input.next()
         
           #wrapper['original'] = in_doc
           
           header = in_doc.get('DOCUMENTHEADER')
           
           line_acc = in_doc.get('ACCOUNTGL')
           line_cur = in_doc.get('CURRENCYAMOUNT')
           
           if (header != None):
              wrapper.add(0, header)
           if (line_acc != None):
               wrapper.add(line_acc)
           if (line_cur != None):
               wrapper.add(line_cur)

       except Exception as e:
           errWrapper = {
               'errMsg' : str(e.args)
           }
           self.log.error("Error in python script")
           self.error.write(errWrapper)
           
   self.output.write(in_doc, wrapper)
   self.log.info("Finished executing the Transform script")

#The Script Snap will look for a ScriptHook object in the “hook”
#variable. The snap will then call the hook’s “execute” method.
hook = TransformScript(input, output, error, log)

the output will be an array which has header at 0th index follwed by lines

Hope this helps!