CSV Parsing

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

CSV Parsing

Matt Raible
I have a String that's delimited by ~. This string is returned from a
database query. I'm able to parse it with the following code in my route:

@Override
public void configure() throws Exception {
CsvDataFormat csv = new CsvDataFormat();
csv.setDelimiter("~");
                ...

.to("bean:myStoredProcedure")

.process(myJdbcProcessor)

.unmarshal(csv)

.process(mySearchCsvProcessor)

Today, I encountered a use case where not one string would be returned, but
multiple strings. To handle this, I've changed myJdbcProcessor to have the
following:


List<String> results = jdbcTemplate.queryForList("...", String.class);

if (results.size() == 1) {
exchange.getIn().setBody(results.get(0), String.class);
} else {
exchange.getIn().setBody(results, List.class);
}

When the result size is 1, everything works as expected. However, when
there are multiple results, the csv unmarshal blows up with the following
error:

org.apache.camel.InvalidPayloadException: No body available of type:
java.io.InputStream

Is it possible to parse multiple lines with unmarshal(csv)?

I tried the following, but that didn't work b/c it only gets the first line:

exchange.getIn().setBody(StringUtils.collectionToCommaDelimitedString(results),
String.class);


Thanks,

Matt
Reply | Threaded
Open this post in threaded view
|

Re: CSV Parsing

Matt Raible
Nevermind, figured it out:

                if (results.size() == 1) {
                        exchange.getIn().setBody(results.get(0), String.class);
                } else {
                        exchange.getIn().setBody(StringUtils.collectionToDelimitedString(results, "\n"), String.class);
                }

On Sep 10, 2014, at 5:44 PM, Matt Raible <[hidden email]> wrote:

> I have a String that's delimited by ~. This string is returned from a database query. I'm able to parse it with the following code in my route:
>
> @Override
> public void configure() throws Exception {
> CsvDataFormat csv = new CsvDataFormat();
> csv.setDelimiter("~");
>                 ...
> .to("bean:myStoredProcedure")
> .process(myJdbcProcessor)
> .unmarshal(csv)
> .process(mySearchCsvProcessor)
>
> Today, I encountered a use case where not one string would be returned, but multiple strings. To handle this, I've changed myJdbcProcessor to have the following:
>
> List<String> results = jdbcTemplate.queryForList("...", String.class);
>
> if (results.size() == 1) {
> exchange.getIn().setBody(results.get(0), String.class);
> } else {
> exchange.getIn().setBody(results, List.class);
> }
>
> When the result size is 1, everything works as expected. However, when there are multiple results, the csv unmarshal blows up with the following error:
>
> org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream
>
> Is it possible to parse multiple lines with unmarshal(csv)?
>
> I tried the following, but that didn't work b/c it only gets the first line:
>
> exchange.getIn().setBody(StringUtils.collectionToCommaDelimitedString(results), String.class);
>
>
> Thanks,
>
> Matt

Reply | Threaded
Open this post in threaded view
|

Re: CSV Parsing

Claus Ibsen-2
In reply to this post by Matt Raible
Hi

You can also use the splitter eip

On Thu, Sep 11, 2014 at 1:44 AM, Matt Raible <[hidden email]> wrote:

> I have a String that's delimited by ~. This string is returned from a
> database query. I'm able to parse it with the following code in my route:
>
> @Override
> public void configure() throws Exception {
> CsvDataFormat csv = new CsvDataFormat();
> csv.setDelimiter("~");
>                 ...
>
> .to("bean:myStoredProcedure")
>
> .process(myJdbcProcessor)
>
> .unmarshal(csv)
>
> .process(mySearchCsvProcessor)
>
> Today, I encountered a use case where not one string would be returned, but
> multiple strings. To handle this, I've changed myJdbcProcessor to have the
> following:
>
>
> List<String> results = jdbcTemplate.queryForList("...", String.class);
>
> if (results.size() == 1) {
> exchange.getIn().setBody(results.get(0), String.class);
> } else {
> exchange.getIn().setBody(results, List.class);
> }
>
> When the result size is 1, everything works as expected. However, when
> there are multiple results, the csv unmarshal blows up with the following
> error:
>
> org.apache.camel.InvalidPayloadException: No body available of type:
> java.io.InputStream
>
> Is it possible to parse multiple lines with unmarshal(csv)?
>
> I tried the following, but that didn't work b/c it only gets the first line:
>
> exchange.getIn().setBody(StringUtils.collectionToCommaDelimitedString(results),
> String.class);
>
>
> Thanks,
>
> Matt



--
Claus Ibsen
-----------------
Red Hat, Inc.
Email: [hidden email]
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/
Reply | Threaded
Open this post in threaded view
|

RE: CSV Parsing

rgupta7
You can try using
<unmarshal>
<bindy packages="org.fusesource.camel" type="Csv" />.
</unmarshal>

Thanks
RG
-----Original Message-----
From: Claus Ibsen [mailto:[hidden email]]
Sent: Thursday, September 11, 2014 11:10 AM
To: [hidden email]
Subject: Re: CSV Parsing

Hi

You can also use the splitter eip

On Thu, Sep 11, 2014 at 1:44 AM, Matt Raible <[hidden email]> wrote:

> I have a String that's delimited by ~. This string is returned from a
> database query. I'm able to parse it with the following code in my route:
>
> @Override
> public void configure() throws Exception {
> CsvDataFormat csv = new CsvDataFormat();
> csv.setDelimiter("~");
>                 ...
>
> .to("bean:myStoredProcedure")
>
> .process(myJdbcProcessor)
>
> .unmarshal(csv)
>
> .process(mySearchCsvProcessor)
>
> Today, I encountered a use case where not one string would be returned, but
> multiple strings. To handle this, I've changed myJdbcProcessor to have the
> following:
>
>
> List<String> results = jdbcTemplate.queryForList("...", String.class);
>
> if (results.size() == 1) {
> exchange.getIn().setBody(results.get(0), String.class);
> } else {
> exchange.getIn().setBody(results, List.class);
> }
>
> When the result size is 1, everything works as expected. However, when
> there are multiple results, the csv unmarshal blows up with the following
> error:
>
> org.apache.camel.InvalidPayloadException: No body available of type:
> java.io.InputStream
>
> Is it possible to parse multiple lines with unmarshal(csv)?
>
> I tried the following, but that didn't work b/c it only gets the first line:
>
> exchange.getIn().setBody(StringUtils.collectionToCommaDelimitedString(results),
> String.class);
>
>
> Thanks,
>
> Matt



--
Claus Ibsen
-----------------
Red Hat, Inc.
Email: [hidden email]
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/