camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Matèrne (jhm) <apa...@materne.de>
Subject AW: How to Parse .tsv(tab separated value) file
Date Wed, 22 Feb 2017 12:06:40 GMT
Simply specify @CsvRecord(separator="\t").

Jan



package de.materne.camel.sandbox.tsv;

import java.util.Optional;

import org.apache.camel.EndpointInject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class TabSeperatedValuesTest extends CamelTestSupport {

	@EndpointInject(uri="mock:end")
	private MockEndpoint mock;

	
	
	@Test
	public void testTabSeperatedValues() throws InterruptedException {
		mock.expectedMessageCount(4);
		String data = createTestData();
		
		template.sendBody("direct:start", data);
		
		mock.assertIsSatisfied();
		Optional<Project> firstProject =
mock.getExchanges().stream()
			.map( exchange ->
exchange.getIn().getBody(Project.class) )
			.filter( project -> "Apache
Camel".equals(project.name) )
			.findFirst();
		assertTrue(firstProject.isPresent());
		assertEquals("http://camel.apache.org/",
firstProject.get().url);
	}
	
	
	
	private String createTestData() {
		return "Apache Camel \t http://camel.apache.org/ \t
Enterprise Integration \n"
		 	 + "Apache ServiceMix \t
http://servicemix.apache.org/ \t Integration Container \n"
  	 	     + "Apache Karaf \t http://karaf.apache.org/ \t modern
and polymorphic container \n"
  	 	     + "Apache Maven \t https://maven.apache.org/ \t Java
Buildtool \n";
	}
	
	

	@Override
	protected RouteBuilder createRouteBuilder() throws Exception {
		DataFormat bindy = new BindyCsvDataFormat(Project.class);
		return new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				// input: multiline string, separated by
newline
				from("direct:start")
	
.split(bodyAs(String.class).tokenize("\n"))
					// now we have multiple Strings
					.unmarshal(bindy)
					// now we multiple Project-instances
					.to("mock:end");
			}
		};
	}
	
	
	
	@CsvRecord(separator="\t")
	public static class Project {
		@DataField(pos=1, trim=true)
		String name;
		@DataField(pos=2, trim=true)
		String url;
		@DataField(pos=3, trim=true)
		String description;
	}

}

> -----Urspr√ľngliche Nachricht-----
> Von: Gunjara [mailto:mgunjara.galaxe@gmail.com]
> Gesendet: Mittwoch, 22. Februar 2017 11:18
> An: users@camel.apache.org
> Betreff: How to Parse .tsv(tab separated value) file
> 
> HI All,
> if there any way to parse .tsv file , like .csv using Bindy . It is
> possible to use camel Bindy for parsing .tsv file.
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-
> Parse-tsv-tab-separated-value-file-tp5794268.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Mime
View raw message