You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Louisa <Le...@hotmail.fr> on 2016/08/03 18:30:25 UTC

Cut file

Hello everybody, 

I need your help. I use Talend ESB and I want to make java beans to cut
file. 

For example, I have this flat file: 

    11886 1855 0000004309000 
    11886 1855 0000057370000 
    11886 1856 0000057374001     
    11886 1856 0000057375000   

In my example I want 2 files (messages), a filter of "1855" and "1856" (It's
the number of orders). 

First file: 

    11886 1855 0000004309000 
    11886 1855 0000057370000[/CODE] 

Second file: 

    11886 1856 0000057374001     
    11886 1856 0000057375000[/CODE] 

But I don't know the number of orders per file (depending on the file). 

If i have three orders (three lines each) in my original file ==> I want
three files with the 3 lines of each order. 

If i have four orders in my original file ==> I want four files. 

If i have five orders in my original file ==> I want five files. 

and so on ....................... 

This is my start but it return nothing: 

package beans; 
    
    import java.io.BufferedReader; 
    import java.io.BufferedWriter; 
    import java.io.ByteArrayInputStream; 
    import java.io.File; 
    import java.io.FileWriter; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.util.HashMap; 
    import java.util.Iterator; 
    import java.util.Map; 
    import java.util.Set; 
    import java.util.TreeSet; 
    
    import org.apache.camel.*; 
    
    
    public class bean_test implements Processor{ 
    
        private static final String ENDPOINT = "aggregateEndpoint"; 
        private static final int NUMERO_SITE_START_POSITION = 46; 
        private static final int NUMERO_SITE_END_POSITION = 55; 
    
    
        @Override 
        public void process(Exchange exchange) throws Exception { 
    
            ProducerTemplate producerTemplate =
exchange.getContext().createProducerTemplate(); 
            String endpoint = exchange.getIn().getHeader(ENDPOINT,
String.class); 
            InputStream is = new
ByteArrayInputStream(exchange.getIn().getBody(String.class).getBytes()); 
            aggregateBody(producerTemplate, is, endpoint, new
HashMap<String, Object>(exchange.getIn().getHeaders())); 
    
        } 
    
        private void aggregateBody(ProducerTemplate producerTemplate,
InputStream content, String endpoint, Map<String, Object> headers){ 
            BufferedReader br = new BufferedReader(new
InputStreamReader(content)); 
            String line; 
            Set<String> order=new TreeSet<String>(); 
    
            try { 
                String lineId = null;   
                while((line = br.readLine()) != null){ 
                    lineId = line.substring(NUMERO_SITE_START_POSITION,
NUMERO_SITE_END_POSITION); 
                    order.add(lineId); 
                } 
    
                for(int i=0;i<order.size();i++){ 
                    String key = "file" + i; 
                    File F = new File(key); 
                    Iterator it = order.iterator(); 
                    FileWriter fw = new FileWriter(F.getAbsoluteFile()); 
                    BufferedWriter bw = new BufferedWriter(fw); 
    
                    while((line = br.readLine()) != null){ 
                        while(it.hasNext()){ 
                            lineId =
line.substring(NUMERO_SITE_START_POSITION, NUMERO_SITE_END_POSITION); 
                            if (lineId.equals(it.next())) { 
                                bw.write(line); 
                            } 
                        } 
    
                    } 
                } 
    
    
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
            finally{ 
                try { 
                    if(br != null)br.close(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
        } 
    } 

can you help me please ? 

Thank you in advance.



--
View this message in context: http://camel.465427.n5.nabble.com/Cut-file-tp5786003.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Cut file

Posted by Brad Johnson <br...@mediadriver.com>.
Were you able to get this to work?

On Wed, Aug 3, 2016 at 4:30 PM, Brad Johnson <br...@mediadriver.com>
wrote:

> Definitely the easiest way to do this is to create a simple bean an
> marshal the text.  I don't know how many fields you have if it is just the
> three or not but essentially if you create small Java bean you can
> unmarshall the split content into a bean, ask it for the identifier file
> and set that to the header "fileName" or any other variable you might want
> to use, marshall it back into text and then write it out to the fileName by
> appending.  I put .txt on the end but you could do whatever you want.
>
> from("file:/inbox").split(body()).unmarshal(dataFormat).
> setHeader("fileName",simple("${body.identifier}")).marshal(
> dataFormat).to("file:/outbox/?fileName=${header.fileName}.
> txt&fileExist=Append")
>
> That's done without an IDE so the likelihood of it running directly is
> low. But the idea is right.  It reads the file, splits the lines, unmarshal
> into the bean so you can access the fields and use the bean to tell you
> what the file name should be and then append to it.  So you don't have to
> have a sorted list and you don't have to  know how many orders there are in
> adavance. The dataFormat can be Bindy, Beanio or any other suitable format
> reader.  The only thing I didn't show there was marshaling it out and
> assume in this case a toString on the bean to write out the proper format.
> But you can do it anyway you'd like.
>
> http://camel.apache.org/file2.html
> http://camel.apache.org/beanio.html
>
> On Wed, Aug 3, 2016 at 4:08 PM, Brad Johnson <brad.johnson@mediadriver.com
> > wrote:
>
>> Hang on let me rewrite I misread to think you were looking for  static
>> field.s
>>
>> On Wed, Aug 3, 2016 at 4:07 PM, Brad Johnson <
>> brad.johnson@mediadriver.com> wrote:
>>
>>> In your route builder do something like follows.
>>>
>>> from("file:/inbox").split(body())
>>> .choice(when(
>>> //use a filter or a simple expression to check to see if 1855 is present
>>> in the record/line and write it out.
>>> .to("file:outbox/1855/...append..
>>> //end the when
>>> //otherwise write to 1856 or do a second when for 1856 and then write it
>>> out and use otherwise to write out an error message.
>>>
>>> The "when" can use a simple expression or you can create a Java bean and
>>> call it to check if the record is an 1855.  I'm not sure what you are
>>> ultimately doing with these but you may want to read these into data beans
>>> using something like the Camel Beanio component and then you could actually
>>> have a method right on the bean to return a Boolean if it is an 1855 or
>>> 1856.
>>>
>>>
>>> If you end up using Processors and Exchanges in the raw you probably
>>> should think of it as a code smell.  Unless you really have a very good
>>> idea of why you HAVE to use them they generally are unnecessary and mean
>>> you're doing things the hard way.  I don't recall the last time I had to
>>> use an actual Camel Exchange.
>>>
>>> Brad
>>>
>>> On Wed, Aug 3, 2016 at 1:30 PM, Louisa <Le...@hotmail.fr> wrote:
>>>
>>>> Hello everybody,
>>>>
>>>> I need your help. I use Talend ESB and I want to make java beans to cut
>>>> file.
>>>>
>>>> For example, I have this flat file:
>>>>
>>>>     11886 1855 0000004309000
>>>>     11886 1855 0000057370000
>>>>     11886 1856 0000057374001
>>>>     11886 1856 0000057375000
>>>>
>>>> In my example I want 2 files (messages), a filter of "1855" and "1856"
>>>> (It's
>>>> the number of orders).
>>>>
>>>> First file:
>>>>
>>>>     11886 1855 0000004309000
>>>>     11886 1855 0000057370000[/CODE]
>>>>
>>>> Second file:
>>>>
>>>>     11886 1856 0000057374001
>>>>     11886 1856 0000057375000[/CODE]
>>>>
>>>> But I don't know the number of orders per file (depending on the file).
>>>>
>>>> If i have three orders (three lines each) in my original file ==> I want
>>>> three files with the 3 lines of each order.
>>>>
>>>> If i have four orders in my original file ==> I want four files.
>>>>
>>>> If i have five orders in my original file ==> I want five files.
>>>>
>>>> and so on .......................
>>>>
>>>> This is my start but it return nothing:
>>>>
>>>> package beans;
>>>>
>>>>     import java.io.BufferedReader;
>>>>     import java.io.BufferedWriter;
>>>>     import java.io.ByteArrayInputStream;
>>>>     import java.io.File;
>>>>     import java.io.FileWriter;
>>>>     import java.io.IOException;
>>>>     import java.io.InputStream;
>>>>     import java.io.InputStreamReader;
>>>>     import java.util.HashMap;
>>>>     import java.util.Iterator;
>>>>     import java.util.Map;
>>>>     import java.util.Set;
>>>>     import java.util.TreeSet;
>>>>
>>>>     import org.apache.camel.*;
>>>>
>>>>
>>>>     public class bean_test implements Processor{
>>>>
>>>>         private static final String ENDPOINT = "aggregateEndpoint";
>>>>         private static final int NUMERO_SITE_START_POSITION = 46;
>>>>         private static final int NUMERO_SITE_END_POSITION = 55;
>>>>
>>>>
>>>>         @Override
>>>>         public void process(Exchange exchange) throws Exception {
>>>>
>>>>             ProducerTemplate producerTemplate =
>>>> exchange.getContext().createProducerTemplate();
>>>>             String endpoint = exchange.getIn().getHeader(ENDPOINT,
>>>> String.class);
>>>>             InputStream is = new
>>>> ByteArrayInputStream(exchange.getIn().getBody(String.class).
>>>> getBytes());
>>>>             aggregateBody(producerTemplate, is, endpoint, new
>>>> HashMap<String, Object>(exchange.getIn().getHeaders()));
>>>>
>>>>         }
>>>>
>>>>         private void aggregateBody(ProducerTemplate producerTemplate,
>>>> InputStream content, String endpoint, Map<String, Object> headers){
>>>>             BufferedReader br = new BufferedReader(new
>>>> InputStreamReader(content));
>>>>             String line;
>>>>             Set<String> order=new TreeSet<String>();
>>>>
>>>>             try {
>>>>                 String lineId = null;
>>>>                 while((line = br.readLine()) != null){
>>>>                     lineId = line.substring(NUMERO_SITE_START_POSITION,
>>>> NUMERO_SITE_END_POSITION);
>>>>                     order.add(lineId);
>>>>                 }
>>>>
>>>>                 for(int i=0;i<order.size();i++){
>>>>                     String key = "file" + i;
>>>>                     File F = new File(key);
>>>>                     Iterator it = order.iterator();
>>>>                     FileWriter fw = new FileWriter(F.getAbsoluteFile()
>>>> );
>>>>                     BufferedWriter bw = new BufferedWriter(fw);
>>>>
>>>>                     while((line = br.readLine()) != null){
>>>>                         while(it.hasNext()){
>>>>                             lineId =
>>>> line.substring(NUMERO_SITE_START_POSITION, NUMERO_SITE_END_POSITION);
>>>>                             if (lineId.equals(it.next())) {
>>>>                                 bw.write(line);
>>>>                             }
>>>>                         }
>>>>
>>>>                     }
>>>>                 }
>>>>
>>>>
>>>>             } catch (IOException e) {
>>>>                 e.printStackTrace();
>>>>             }
>>>>             finally{
>>>>                 try {
>>>>                     if(br != null)br.close();
>>>>                 } catch (IOException e) {
>>>>                     e.printStackTrace();
>>>>                 }
>>>>             }
>>>>         }
>>>>     }
>>>>
>>>> can you help me please ?
>>>>
>>>> Thank you in advance.
>>>>
>>>>
>>>>
>>>> --
>>>> View this message in context: http://camel.465427.n5.nabble.
>>>> com/Cut-file-tp5786003.html
>>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>>
>>>
>>>
>>
>

Re: Cut file

Posted by Brad Johnson <br...@mediadriver.com>.
Definitely the easiest way to do this is to create a simple bean an marshal
the text.  I don't know how many fields you have if it is just the three or
not but essentially if you create small Java bean you can unmarshall the
split content into a bean, ask it for the identifier file and set that to
the header "fileName" or any other variable you might want to use, marshall
it back into text and then write it out to the fileName by appending.  I
put .txt on the end but you could do whatever you want.

from("file:/inbox").split(body()).unmarshal(dataFormat).setHeader("fileName",simple("${body.identifier}")).marshal(dataFormat).to("file:/outbox/?fileName=${header.fileName}.txt&fileExist=Append")

That's done without an IDE so the likelihood of it running directly is low.
But the idea is right.  It reads the file, splits the lines, unmarshal into
the bean so you can access the fields and use the bean to tell you what the
file name should be and then append to it.  So you don't have to have a
sorted list and you don't have to  know how many orders there are in
adavance. The dataFormat can be Bindy, Beanio or any other suitable format
reader.  The only thing I didn't show there was marshaling it out and
assume in this case a toString on the bean to write out the proper format.
But you can do it anyway you'd like.

http://camel.apache.org/file2.html
http://camel.apache.org/beanio.html

On Wed, Aug 3, 2016 at 4:08 PM, Brad Johnson <br...@mediadriver.com>
wrote:

> Hang on let me rewrite I misread to think you were looking for  static
> field.s
>
> On Wed, Aug 3, 2016 at 4:07 PM, Brad Johnson <brad.johnson@mediadriver.com
> > wrote:
>
>> In your route builder do something like follows.
>>
>> from("file:/inbox").split(body())
>> .choice(when(
>> //use a filter or a simple expression to check to see if 1855 is present
>> in the record/line and write it out.
>> .to("file:outbox/1855/...append..
>> //end the when
>> //otherwise write to 1856 or do a second when for 1856 and then write it
>> out and use otherwise to write out an error message.
>>
>> The "when" can use a simple expression or you can create a Java bean and
>> call it to check if the record is an 1855.  I'm not sure what you are
>> ultimately doing with these but you may want to read these into data beans
>> using something like the Camel Beanio component and then you could actually
>> have a method right on the bean to return a Boolean if it is an 1855 or
>> 1856.
>>
>>
>> If you end up using Processors and Exchanges in the raw you probably
>> should think of it as a code smell.  Unless you really have a very good
>> idea of why you HAVE to use them they generally are unnecessary and mean
>> you're doing things the hard way.  I don't recall the last time I had to
>> use an actual Camel Exchange.
>>
>> Brad
>>
>> On Wed, Aug 3, 2016 at 1:30 PM, Louisa <Le...@hotmail.fr> wrote:
>>
>>> Hello everybody,
>>>
>>> I need your help. I use Talend ESB and I want to make java beans to cut
>>> file.
>>>
>>> For example, I have this flat file:
>>>
>>>     11886 1855 0000004309000
>>>     11886 1855 0000057370000
>>>     11886 1856 0000057374001
>>>     11886 1856 0000057375000
>>>
>>> In my example I want 2 files (messages), a filter of "1855" and "1856"
>>> (It's
>>> the number of orders).
>>>
>>> First file:
>>>
>>>     11886 1855 0000004309000
>>>     11886 1855 0000057370000[/CODE]
>>>
>>> Second file:
>>>
>>>     11886 1856 0000057374001
>>>     11886 1856 0000057375000[/CODE]
>>>
>>> But I don't know the number of orders per file (depending on the file).
>>>
>>> If i have three orders (three lines each) in my original file ==> I want
>>> three files with the 3 lines of each order.
>>>
>>> If i have four orders in my original file ==> I want four files.
>>>
>>> If i have five orders in my original file ==> I want five files.
>>>
>>> and so on .......................
>>>
>>> This is my start but it return nothing:
>>>
>>> package beans;
>>>
>>>     import java.io.BufferedReader;
>>>     import java.io.BufferedWriter;
>>>     import java.io.ByteArrayInputStream;
>>>     import java.io.File;
>>>     import java.io.FileWriter;
>>>     import java.io.IOException;
>>>     import java.io.InputStream;
>>>     import java.io.InputStreamReader;
>>>     import java.util.HashMap;
>>>     import java.util.Iterator;
>>>     import java.util.Map;
>>>     import java.util.Set;
>>>     import java.util.TreeSet;
>>>
>>>     import org.apache.camel.*;
>>>
>>>
>>>     public class bean_test implements Processor{
>>>
>>>         private static final String ENDPOINT = "aggregateEndpoint";
>>>         private static final int NUMERO_SITE_START_POSITION = 46;
>>>         private static final int NUMERO_SITE_END_POSITION = 55;
>>>
>>>
>>>         @Override
>>>         public void process(Exchange exchange) throws Exception {
>>>
>>>             ProducerTemplate producerTemplate =
>>> exchange.getContext().createProducerTemplate();
>>>             String endpoint = exchange.getIn().getHeader(ENDPOINT,
>>> String.class);
>>>             InputStream is = new
>>> ByteArrayInputStream(exchange.getIn().getBody(String.class).getBytes());
>>>             aggregateBody(producerTemplate, is, endpoint, new
>>> HashMap<String, Object>(exchange.getIn().getHeaders()));
>>>
>>>         }
>>>
>>>         private void aggregateBody(ProducerTemplate producerTemplate,
>>> InputStream content, String endpoint, Map<String, Object> headers){
>>>             BufferedReader br = new BufferedReader(new
>>> InputStreamReader(content));
>>>             String line;
>>>             Set<String> order=new TreeSet<String>();
>>>
>>>             try {
>>>                 String lineId = null;
>>>                 while((line = br.readLine()) != null){
>>>                     lineId = line.substring(NUMERO_SITE_START_POSITION,
>>> NUMERO_SITE_END_POSITION);
>>>                     order.add(lineId);
>>>                 }
>>>
>>>                 for(int i=0;i<order.size();i++){
>>>                     String key = "file" + i;
>>>                     File F = new File(key);
>>>                     Iterator it = order.iterator();
>>>                     FileWriter fw = new FileWriter(F.getAbsoluteFile());
>>>                     BufferedWriter bw = new BufferedWriter(fw);
>>>
>>>                     while((line = br.readLine()) != null){
>>>                         while(it.hasNext()){
>>>                             lineId =
>>> line.substring(NUMERO_SITE_START_POSITION, NUMERO_SITE_END_POSITION);
>>>                             if (lineId.equals(it.next())) {
>>>                                 bw.write(line);
>>>                             }
>>>                         }
>>>
>>>                     }
>>>                 }
>>>
>>>
>>>             } catch (IOException e) {
>>>                 e.printStackTrace();
>>>             }
>>>             finally{
>>>                 try {
>>>                     if(br != null)br.close();
>>>                 } catch (IOException e) {
>>>                     e.printStackTrace();
>>>                 }
>>>             }
>>>         }
>>>     }
>>>
>>> can you help me please ?
>>>
>>> Thank you in advance.
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://camel.465427.n5.nabble.com/Cut-file-tp5786003.html
>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>
>>
>>
>

Re: Cut file

Posted by Brad Johnson <br...@mediadriver.com>.
Hang on let me rewrite I misread to think you were looking for  static
field.s

On Wed, Aug 3, 2016 at 4:07 PM, Brad Johnson <br...@mediadriver.com>
wrote:

> In your route builder do something like follows.
>
> from("file:/inbox").split(body())
> .choice(when(
> //use a filter or a simple expression to check to see if 1855 is present
> in the record/line and write it out.
> .to("file:outbox/1855/...append..
> //end the when
> //otherwise write to 1856 or do a second when for 1856 and then write it
> out and use otherwise to write out an error message.
>
> The "when" can use a simple expression or you can create a Java bean and
> call it to check if the record is an 1855.  I'm not sure what you are
> ultimately doing with these but you may want to read these into data beans
> using something like the Camel Beanio component and then you could actually
> have a method right on the bean to return a Boolean if it is an 1855 or
> 1856.
>
>
> If you end up using Processors and Exchanges in the raw you probably
> should think of it as a code smell.  Unless you really have a very good
> idea of why you HAVE to use them they generally are unnecessary and mean
> you're doing things the hard way.  I don't recall the last time I had to
> use an actual Camel Exchange.
>
> Brad
>
> On Wed, Aug 3, 2016 at 1:30 PM, Louisa <Le...@hotmail.fr> wrote:
>
>> Hello everybody,
>>
>> I need your help. I use Talend ESB and I want to make java beans to cut
>> file.
>>
>> For example, I have this flat file:
>>
>>     11886 1855 0000004309000
>>     11886 1855 0000057370000
>>     11886 1856 0000057374001
>>     11886 1856 0000057375000
>>
>> In my example I want 2 files (messages), a filter of "1855" and "1856"
>> (It's
>> the number of orders).
>>
>> First file:
>>
>>     11886 1855 0000004309000
>>     11886 1855 0000057370000[/CODE]
>>
>> Second file:
>>
>>     11886 1856 0000057374001
>>     11886 1856 0000057375000[/CODE]
>>
>> But I don't know the number of orders per file (depending on the file).
>>
>> If i have three orders (three lines each) in my original file ==> I want
>> three files with the 3 lines of each order.
>>
>> If i have four orders in my original file ==> I want four files.
>>
>> If i have five orders in my original file ==> I want five files.
>>
>> and so on .......................
>>
>> This is my start but it return nothing:
>>
>> package beans;
>>
>>     import java.io.BufferedReader;
>>     import java.io.BufferedWriter;
>>     import java.io.ByteArrayInputStream;
>>     import java.io.File;
>>     import java.io.FileWriter;
>>     import java.io.IOException;
>>     import java.io.InputStream;
>>     import java.io.InputStreamReader;
>>     import java.util.HashMap;
>>     import java.util.Iterator;
>>     import java.util.Map;
>>     import java.util.Set;
>>     import java.util.TreeSet;
>>
>>     import org.apache.camel.*;
>>
>>
>>     public class bean_test implements Processor{
>>
>>         private static final String ENDPOINT = "aggregateEndpoint";
>>         private static final int NUMERO_SITE_START_POSITION = 46;
>>         private static final int NUMERO_SITE_END_POSITION = 55;
>>
>>
>>         @Override
>>         public void process(Exchange exchange) throws Exception {
>>
>>             ProducerTemplate producerTemplate =
>> exchange.getContext().createProducerTemplate();
>>             String endpoint = exchange.getIn().getHeader(ENDPOINT,
>> String.class);
>>             InputStream is = new
>> ByteArrayInputStream(exchange.getIn().getBody(String.class).getBytes());
>>             aggregateBody(producerTemplate, is, endpoint, new
>> HashMap<String, Object>(exchange.getIn().getHeaders()));
>>
>>         }
>>
>>         private void aggregateBody(ProducerTemplate producerTemplate,
>> InputStream content, String endpoint, Map<String, Object> headers){
>>             BufferedReader br = new BufferedReader(new
>> InputStreamReader(content));
>>             String line;
>>             Set<String> order=new TreeSet<String>();
>>
>>             try {
>>                 String lineId = null;
>>                 while((line = br.readLine()) != null){
>>                     lineId = line.substring(NUMERO_SITE_START_POSITION,
>> NUMERO_SITE_END_POSITION);
>>                     order.add(lineId);
>>                 }
>>
>>                 for(int i=0;i<order.size();i++){
>>                     String key = "file" + i;
>>                     File F = new File(key);
>>                     Iterator it = order.iterator();
>>                     FileWriter fw = new FileWriter(F.getAbsoluteFile());
>>                     BufferedWriter bw = new BufferedWriter(fw);
>>
>>                     while((line = br.readLine()) != null){
>>                         while(it.hasNext()){
>>                             lineId =
>> line.substring(NUMERO_SITE_START_POSITION, NUMERO_SITE_END_POSITION);
>>                             if (lineId.equals(it.next())) {
>>                                 bw.write(line);
>>                             }
>>                         }
>>
>>                     }
>>                 }
>>
>>
>>             } catch (IOException e) {
>>                 e.printStackTrace();
>>             }
>>             finally{
>>                 try {
>>                     if(br != null)br.close();
>>                 } catch (IOException e) {
>>                     e.printStackTrace();
>>                 }
>>             }
>>         }
>>     }
>>
>> can you help me please ?
>>
>> Thank you in advance.
>>
>>
>>
>> --
>> View this message in context:
>> http://camel.465427.n5.nabble.com/Cut-file-tp5786003.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>
>

Re: Cut file

Posted by Brad Johnson <br...@mediadriver.com>.
In your route builder do something like follows.

from("file:/inbox").split(body())
.choice(when(
//use a filter or a simple expression to check to see if 1855 is present in
the record/line and write it out.
.to("file:outbox/1855/...append..
//end the when
//otherwise write to 1856 or do a second when for 1856 and then write it
out and use otherwise to write out an error message.

The "when" can use a simple expression or you can create a Java bean and
call it to check if the record is an 1855.  I'm not sure what you are
ultimately doing with these but you may want to read these into data beans
using something like the Camel Beanio component and then you could actually
have a method right on the bean to return a Boolean if it is an 1855 or
1856.


If you end up using Processors and Exchanges in the raw you probably should
think of it as a code smell.  Unless you really have a very good idea of
why you HAVE to use them they generally are unnecessary and mean you're
doing things the hard way.  I don't recall the last time I had to use an
actual Camel Exchange.

Brad

On Wed, Aug 3, 2016 at 1:30 PM, Louisa <Le...@hotmail.fr> wrote:

> Hello everybody,
>
> I need your help. I use Talend ESB and I want to make java beans to cut
> file.
>
> For example, I have this flat file:
>
>     11886 1855 0000004309000
>     11886 1855 0000057370000
>     11886 1856 0000057374001
>     11886 1856 0000057375000
>
> In my example I want 2 files (messages), a filter of "1855" and "1856"
> (It's
> the number of orders).
>
> First file:
>
>     11886 1855 0000004309000
>     11886 1855 0000057370000[/CODE]
>
> Second file:
>
>     11886 1856 0000057374001
>     11886 1856 0000057375000[/CODE]
>
> But I don't know the number of orders per file (depending on the file).
>
> If i have three orders (three lines each) in my original file ==> I want
> three files with the 3 lines of each order.
>
> If i have four orders in my original file ==> I want four files.
>
> If i have five orders in my original file ==> I want five files.
>
> and so on .......................
>
> This is my start but it return nothing:
>
> package beans;
>
>     import java.io.BufferedReader;
>     import java.io.BufferedWriter;
>     import java.io.ByteArrayInputStream;
>     import java.io.File;
>     import java.io.FileWriter;
>     import java.io.IOException;
>     import java.io.InputStream;
>     import java.io.InputStreamReader;
>     import java.util.HashMap;
>     import java.util.Iterator;
>     import java.util.Map;
>     import java.util.Set;
>     import java.util.TreeSet;
>
>     import org.apache.camel.*;
>
>
>     public class bean_test implements Processor{
>
>         private static final String ENDPOINT = "aggregateEndpoint";
>         private static final int NUMERO_SITE_START_POSITION = 46;
>         private static final int NUMERO_SITE_END_POSITION = 55;
>
>
>         @Override
>         public void process(Exchange exchange) throws Exception {
>
>             ProducerTemplate producerTemplate =
> exchange.getContext().createProducerTemplate();
>             String endpoint = exchange.getIn().getHeader(ENDPOINT,
> String.class);
>             InputStream is = new
> ByteArrayInputStream(exchange.getIn().getBody(String.class).getBytes());
>             aggregateBody(producerTemplate, is, endpoint, new
> HashMap<String, Object>(exchange.getIn().getHeaders()));
>
>         }
>
>         private void aggregateBody(ProducerTemplate producerTemplate,
> InputStream content, String endpoint, Map<String, Object> headers){
>             BufferedReader br = new BufferedReader(new
> InputStreamReader(content));
>             String line;
>             Set<String> order=new TreeSet<String>();
>
>             try {
>                 String lineId = null;
>                 while((line = br.readLine()) != null){
>                     lineId = line.substring(NUMERO_SITE_START_POSITION,
> NUMERO_SITE_END_POSITION);
>                     order.add(lineId);
>                 }
>
>                 for(int i=0;i<order.size();i++){
>                     String key = "file" + i;
>                     File F = new File(key);
>                     Iterator it = order.iterator();
>                     FileWriter fw = new FileWriter(F.getAbsoluteFile());
>                     BufferedWriter bw = new BufferedWriter(fw);
>
>                     while((line = br.readLine()) != null){
>                         while(it.hasNext()){
>                             lineId =
> line.substring(NUMERO_SITE_START_POSITION, NUMERO_SITE_END_POSITION);
>                             if (lineId.equals(it.next())) {
>                                 bw.write(line);
>                             }
>                         }
>
>                     }
>                 }
>
>
>             } catch (IOException e) {
>                 e.printStackTrace();
>             }
>             finally{
>                 try {
>                     if(br != null)br.close();
>                 } catch (IOException e) {
>                     e.printStackTrace();
>                 }
>             }
>         }
>     }
>
> can you help me please ?
>
> Thank you in advance.
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Cut-file-tp5786003.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>