on 05-20-2016 6:08 AM
Hi all,
My requirement is that I need to read only the first field(field1 in below example) of the first row in the txt file . After reading the same, the channel should not read the later rows since this would help in performance and the required field is only the first one.
File is something like this:
field1;field2;field3;field4
field1;field2;field3;field4
field1;field2;field3;field4
.
.
.
field1;field2;field3;field4
I want to do this using File content conversion but tried all ways.
Kindly let me know if I'm missing something.
Thanks,
Saumya
Hi Saumya,
I guess you tried Keyfield method.
The keyfield value for the first row will only create the first record at FCC level.
Let the sender supply a key field value to identify the first row.
You can check this blog for more detail.
Later on in message mapping level you can extract the first field value.
2nd solution
------------------
Use no FCC at sender channel level.
you can do away with FCC if you do not have a key field value.
Create a message type with only one field in it.
MT_Data
-CompleteFileData
MT_FirstField
-fieldValue
Create an inbound and outbound Service interface
SI_Outbound and SI_Inbound each one will refer to MT_Data and MT_FirstField as message type respectively.
Create an Operation Mapping.
Create a java mapping with following code. the code reads only the first line of the file. Thus processing time gets reduced.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
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 GetFirstFieldValue extends AbstractTransformation{
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
try {
extractFirstField(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());
} catch (Exception e) {
e.printStackTrace();
throw new StreamTransformationException(e.getMessage());
// TODO Auto-generated catch block
}
}
private void extractFirstField(InputStream in,
OutputStream out) throws StreamTransformationException{
// TODO Auto-generated method stub
try
{
InputStreamReader isReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isReader);
String s=(reader.readLine().split(";"))[0];
s="<MT_FirstField><fieldValue>"+s+"</fieldValue></MT_FirstField>";
out.write(s.toString().getBytes("UTF-8"));
in.close();
out.close();
return ;
}
catch (Exception e) {
e.printStackTrace();
throw new StreamTransformationException(e.getMessage());
// TODO Auto-generated catch block
}
}
public static void main(String[] args) throws FileNotFoundException, StreamTransformationException {
// TODO Auto-generated method stub
String zipFile="C:/apps/test.in";
String Output="C:/apps/testout.xml";
InputStream in=new FileInputStream(zipFile);
OutputStream out=new FileOutputStream(Output);
GetFirstFieldValue r=new GetFirstFieldValue();
r.extractFirstField(in,out);
}
}
If in input we have this file
field1;field2;field3;field4
field1;field2;field3;field4
field1;field2;field3;field4
The output will be
-------------------------------------
<?xml version="1.0"?>
<MT_FirstField>
<fieldValue>field1</fieldValue>
</MT_FirstField>
Regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I haven't tried it by myself but you can give it a try once.
I prepared it using this link,
Converting Text Format in the Sender File/FTP Adapter to XML - Advanced Adapter Engine - SAP Library
NameA.fieldFixedLengths = 6 (Length of your field1)
Or
NameA.fieldFixedLengths = 6 (Length of your field1),0
NameA.fieldSeparator = end of file (-1)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Soumya,
Could u pls add screenshot of FCC implemented and the sample file to get more clarity?
Pranil.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Pranil,
I did some hit and trial. But now I'm doubtful if it is even possible to do this using FCC. I tried to make use of 'Recordsets per Message' as 1 but then I need to stop the creation of many messages for each row.
File is a simple one. The example I have given. I have replaced the actual values with field1,field2 etc.
Hello Saumya,
Do you know how many lines will be there in the file? If it is fixed, then you can use Document Offset parameter and mention the number of lines, which can be skipped.
If you know, there will be 'n' lines in the file, then mention 'n-1' as value in Document Offset and this will avoid reading of each and every line. Also set Recordsets per message as 1.
This is the solution i can think of right now.
Regards,
Nitin
Hi Nitin,
Thanks for the suggestion. However, I already tried both ways but to no help. I don't have a fixed number of lines so can't use Document offset. Also, using Recordset per message, it will create 1 message for every line which will create havoc if there are too many lines. So that wouldn't be recommended.
Should I try some other method then? Will adapter modules work?
Hi Saumya,
The adapter is reading the file line by line and converting the line to XML, as long as the line not equal to null then adapter will read the line and convert to XML, there is no option to stop the loop at specific line number.
Even the adapter module also will not help because at the time of module processor execution the complete file read by adapter and created the message.
Regards,
Praveen.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.