camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brad Johnson <brad.john...@mediadriver.com>
Subject Re: Cut file
Date Wed, 03 Aug 2016 21:08:53 GMT
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 <Lesgens59@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.
>>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message