You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Louisa <Le...@hotmail.fr> on 2016/08/03 14:37:02 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-tp5785993.html
Sent from the Camel Development mailing list archive at Nabble.com.
AW: Cut file
Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
Maybe as starting point ...
Jan
package de.materne.example.camel.custom_splitter;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class SplitterTest extends CamelTestSupport {
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri = "direct:start")
protected ProducerTemplate template;
@Test
public void test() throws InterruptedException {
resultEndpoint.expectedMessageCount(2);
template.sendBody(testdata());
assertMockEndpointsSatisfied();
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.split(bodyAs(String.class).tokenize("\n"))
.process( (exchange) ->
exchange.getIn().setHeader(
"orderNr",
exchange.getIn().getBody(String.class).split(" ")[1].trim())
)
.aggregate(header("orderNr"), new
UseLatestAggregationStrategy()).completionInterval(100)
.to("mock:result");
}
};
}
private String testdata() {
return "11886 1855 0000004309000\n"
+ "11886 1855 0000057370000\n"
+ "11886 1856 0000057374001\n"
+ "11886 1856 0000057375000\n";
}
}
> -----Ursprüngliche Nachricht-----
> Von: Louisa [mailto:Lesgens59@hotmail.fr]
> Gesendet: Mittwoch, 3. August 2016 16:37
> An: dev@camel.apache.org
> Betreff: 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-tp5785993.html
> Sent from the Camel Development mailing list archive at Nabble.com.