Skip to Content
Author's profile photo Raghu Vamseedhar Reddy Kadipi Reddy

How to create Java Mapping in SAP PI / PO

This blog will complement other blogs on ‘Java Mapping’ in SCN.

Direct link: How to create Java Mapping in SAP PI PO – YouTube

Sample Java Mapping used in the video: –


package com.map;
import java.io.*;
import com.sap.aii.mapping.api.*;
public class Test_JavaMapping extends AbstractTransformation {
    @Override
    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
        try {
            InputStream inputstream = transformationInput.getInputPayload().getInputStream();
            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
            // Copy Input content to Output content
            byte[] b = new byte[inputstream.available()];
            inputstream.read(b);
       
            outputstream.write("Prefixing this line to input. Test_JavaMapping. \r\n".getBytes());
            outputstream.write(b);
        } catch (Exception exception) {
            getTrace().addDebugMessage(exception.getMessage());
            throw new StreamTransformationException(exception.toString());
        }
    }
}



Sample text file used in the video:-


Name,ID,Phone
AAAA,11,123456789
δΈ–η•Œ,22,987654321
BBBB,33,777777777



Helpful links: –

NWDS Download Links – Java Development – SCN Wiki

SAP Support Portal S user id is required to download NWDS.

Not well-formed XML – & issue

JAVA Mapping – Managing Services in the Enterprise Services Repository – SAP Library

PI Mapping and Lookup API – SAP Javadocs

Trail: Java API for XML Processing (JAXP) (The Java Tutorials)

Dynamic file name for pass-through scenario – Process Integration – SCN Wiki

Multi-Mapping using Java Mapping – Process Integration – SCN Wiki

Reading Binary File Using ICO

Java Mapping in Exchange Infrastructure (download the mwv file).

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Raghu Vamseedhar Reddy Kadipi Reddy
      Raghu Vamseedhar Reddy Kadipi Reddy
      Blog Post Author

      Manoj,

      πŸ™‚ .

           As you are developing your first Java mapping. Please develop two Java mappings shown in above video, it will be a good start. Then go through links provided in the blog. Please search SCN for more blogs on this topic.

         

           FYI.... SAP does not recommend performing IO operation (java.io) in a mapping. i.e.., SAP does not recommend file read/write in Java mapping.

      Author's profile photo Manoj K
      Manoj K

      Thanks Raghu πŸ™‚

      Raghu , if possible please post a blog for java mapping of how to trace a XML (like child nodes, attributes..etc..) .

      A simple post like taking data from 2 source node from and adding to single target node would be very helpful for javamapping beginners.

      Br,

      Manoj

      Author's profile photo Praveen Gandepalli
      Praveen Gandepalli

      Hi Manoj,

      You can use below code to test locally in the eclipse.In eclipse the main method will be called but in PI main method will not be called so same code you can use it in PI and locally in eclipse.

      package za.co.pnp.sap.pi.mapping.pmt.statement;

      import java.io.FileInputStream;

      import java.io.FileNotFoundException;

      import java.io.FileOutputStream;

      import java.io.InputStream;

      import java.io.OutputStream;

      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;

      public class Test_JavaMapping extends AbstractTransformation {

          public static void main(String[] args) throws StreamTransformationException {

              FileInputStream fin = null;

              FileOutputStream fout = null;

              try {

                  fin = new FileInputStream("source.txt");

                  fout = new FileOutputStream("target.txt");

              } catch (FileNotFoundException e) {

                  e.printStackTrace();

              }

              Test_JavaMapping javaMap = new Test_JavaMapping();

              javaMap.execute(fin, fout);

          }

          @Override

          public void transform(TransformationInput transformationIn, TransformationOutput transformationOut)

                  throws StreamTransformationException {

              execute(transformationIn.getInputPayload().getInputStream(), transformationOut.getOutputPayload()

                      .getOutputStream());

          }

          public void execute(InputStream inputStream, OutputStream outputStream) throws StreamTransformationException {

              try {

                  // Copy Input content to Output content

                  byte[] b = new byte[inputStream.available()];

                  inputStream.read(b);

                  outputStream.write("Prefixing this line to input. Test_JavaMapping. \r\n".getBytes());

                  outputStream.write(b);

              } catch (Exception exception) {

                  getTrace().addDebugMessage(exception.getMessage());

                  throw new StreamTransformationException(exception.toString());

              }

          }

      }

      Regards,

      Praveen.

      Author's profile photo Nabendu Sen
      Nabendu Sen

      Nice blog Raghu. We should practice involving more video tutorials. It makes easy to understand any hands on tutorials.

      ~ Nabendu.

      Author's profile photo Pavan kumar
      Pavan kumar

      Well Done Raghu.

      Cheers

      Pavan

      Author's profile photo piuser p
      piuser p

      Nice Blog.Thanks for sharing πŸ™‚

      Author's profile photo Former Member
      Former Member

      Hi Raghu,

      I have a scenario File 2 File.

      "I need to encrypt the input file(MT101) by using .dll file (.net framework)  and process the encrypted file to target."

      please suggest possibilities to archive this requirement.

      Thanks in advance.

      Regards

      Sampath N