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.
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
Java Mapping in Exchange Infrastructure (download the mwv file).
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.
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
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.
Nice blog Raghu. We should practice involving more video tutorials. It makes easy to understand any hands on tutorials.
~ Nabendu.
Well Done Raghu.
Cheers
Pavan
Nice Blog.Thanks for sharing π
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