How to Parse .tsv(tab separated value) file

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to Parse .tsv(tab separated value) file

Gunjara
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.
Reply | Threaded
Open this post in threaded view
|

Re: How to Parse .tsv(tab separated value) file

Tomohisa Igarashi
Hi,

Have you tried specifying '\t' as a separator?
http://camel.apache.org/bindy.html

Thanks,
Tomo

On 02/22/2017 07:18 PM, Gunjara wrote:

> 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.
>
Reply | Threaded
Open this post in threaded view
|

AW: How to Parse .tsv(tab separated value) file

Jan Matèrne (jhm)
In reply to this post by Gunjara
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:[hidden email]]
> Gesendet: Mittwoch, 22. Februar 2017 11:18
> An: [hidden email]
> 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.

Reply | Threaded
Open this post in threaded view
|

Re: AW: How to Parse .tsv(tab separated value) file

Gunjara
Thanks :)