Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Applies to:

SAP PI developers.

Summary

According SAP PI help Link, “you can generally implement a multi-mapping with all types of mapping programs”.

Intention of this Wiki page is to point out that all types of mapping programs can be used to generate multi-mapping (1: N)..

Author(s):  

   
Name: Raghu Vamseedhar Reddy .K

Company:  Infosys Limited   
Created on:    8/29/2011
Author(s) Bio
I am SAP Certified Development Associate - Process Integration with SAP Netweaver (PI 7.1). I have about 5 year experience of SAP XI/PI implementation and support.

I am also, Sun Certified Programmer for the Java Platform, Standard Edition 6. I have good understanding of XML, XSLT, SOAP, WSDL, ABAP. I am an active member in SAP SND.

Introduction

PI considers a mapping (Graphical, Java, ABAP) as multi-mapping when,

  1. Mapping is able to able to generate the multi-mapping envelope, and
  2. Occurrence of service interfaces in Operational mapping is other than 1.

Each Multi-Mapping program should create following envelope:-

<?xml version="1.0" encoding="UTF-8"?>
<sm:Messages xmlns:sm="http://sap.com/xi/XI/SplitAndMerge">
                <Message1> <MT_type>Payload1</MT_type> </Message1>
                <Message2> <MT_type>Payload2</MT_type> </Message2>
                <Message3> <MT_type>Payload3</MT_type> </Message3>
</sm:Messages>

Note :-

  • Please pay attention to name-space http://sap.com/xi/XI/SplitAndMerge it is constant and Messages -- Message1, Message2, Message3.
  • Each Message should have root XML element (it can be anything, <root> or <A> or <MT_type>). i.e., <Message1> <root>Payload1</root> </Message1>.
  • Number of messages produced by mapping should be equal to number of service interfaces which are referred in Operational mapping target side. 
  • i.e., if in Operational Mapping you have referred only one service interface then mapping should produce only Message1.

Below Java Mapping will generate this XML output:-

<?xml version="1.0" encoding="UTF-8"?>
<sm:Messages xmlns:sm="http://sap.com/xi/XI/SplitAndMerge">
<Message1>
  <root>Payload1</root>
  <root>Payload2</root>
  <root>Payload3</root>
</Message1>
</sm:Messages>

Java Mapping 

MultiMapping_JavaMapping
package com.javaMapping;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MultiMapping_JavaMapping extends AbstractTransformation {

    @Override
    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
        try {
            InputStream inputstream = transformationInput.getInputPayload().getInputStream();
            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();

            //Getting input payload into strInContent. strInContent is not used in this example. 
            //But in your mapping, based on mapping logic it should be used.
            byte[] b = new byte[inputstream.available()];
            inputstream.read(b);
            String strInContent = new String(b);

            //strStart and strEnd are constants.
            String strStart = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:Messages xmlns:ns0=\"http://sap.com/xi/XI/SplitAndMerge\">";
            //Edit according to requirement
            //Edit code to fill strMessage1 with the desired Payload1 and so on. You might need to use strInContent to construct output payload.
            String strMessage1 = "Payload1";
            String strMessage2 = "Payload2";
            String strMessage3 = "Payload3";
            String strEnd = "</ns0:Messages>";

            //Edit according to requirement.
            //Constructing the multi-mapping envelope.
            String strOutContent = strStart + "<Message1> " + "<root>" + strMessage1 + "</root>" + " <root>" + strMessage2 + "</root> " 
                                   + " <root>" + strMessage3 + "</root> " + "</Message1>" + strEnd;

            //Writing strOutContent to output
            outputstream.write(strOutContent.getBytes());
        } catch (IOException ioException) {
            //If some exception is occurred it is written to mapping trace.
            getTrace().addDebugMessage(ioException.getMessage());
            //stopping the mapping by throwing exception.
            throw new StreamTransformationException(ioException.getMessage());
        }
    }
}

When this interface (File to File) is run end to end, it will pick up one file and produce 3 output files.

First file content

<?xml version="1.0" encoding="utf-8" ?><root>Payload1</root>

Second file content

<?xml version="1.0" encoding="utf-8" ?><root>Payload2</root>

Third file content
<?xml version="1.0" encoding="utf-8" ?><root>Payload3</root>

Reference.

You can follow all steps mentioned in this Graphical multi-mapping Link, except graphical mapping (instead implement java mapping).

XSLT multi-mapping Link.

Java multi-mapping (this blog) has the advantages of Java mappings i.e., archiving complex mapping logics.

Use cases

  1. Java Mapping for multi-mapping to split the flat file to different flat files. i.e. let say, if input file is of 15 MB, then it can be split into 3 separate 5 MB files.
  2. XML file can be split. Let say a proxy can handle 999 records at a time. If payload is having 1200 records, then split the message into 2 XML messages. First with 999 records and second with 201 records.
  3. Files rename can be done using variable substitution. i.e. multiple files can be created with dynamic file name, using variable substitution. Note: DynamicConfiguration, cannot be used.

2 Comments

  1. Construct well-formed XML. There are 5 predefined entity references in XML, http://www.w3schools.com/xml/xml_syntax.asp. Add below replace logic, after reading plain text from input.

    strInContent = strInContent.replaceAll("&", "&amp;").replaceAll("<", "&lt;");

     

  2. HI Raghu,

    First of all i want to ask you one question  how come you are so good in JAVA mapping/SAP PI ? (smile)

    Okie now  my question .

    i am just trying to write my first java mapping in eclipse , in that case i wan to read a file from desktop and write the output after java mapping into another file.

    For that in my class i have written Fileinputstream in=new FileinputStream("input.txt") and Fileoutputstream in=new FileoutputStream("output.txt") in main method.

     how do i pass that file content data to transform method which takes paramater as(TransformationInput transformationInput, TransformationOutput transformationOutput) and once i apply all my logic to the content how to transfer back it to Fileoutputstream?

    It would be very helpful if you show me using the same above example .considering input Payload is in some file and writin my java mapping output into another file.


    Br,

    Manoj