You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by mike_hoffy <mi...@hotmail.com> on 2015/01/18 17:18:20 UTC

Camel CSV Component Not Outputting Headers Correctly

I have the following 2.14.1 route:

               from("activemq:queue:ABC_FULFILLMENT_REQUEST")
                     .aggregate(new
ABCFulfillmentCenterAggregationStrategy())
                     .xpath(
                           "/o:Order/o:OrderType/o:FulfillmentCenter = '"
                                 +
com.pluralsight.orderfulfillment.generated.FulfillmentCenter.FULFILLMENT_CENTER_ONE.value()
                                 + "'", Boolean.class, namespace)
                     .completionInterval(10000)
                     .beanRef("aBCFulfillmentProcessor",
"processAggregate").marshal()
                     .csv().to("file://" +
filePath).to("mock:direct:result");

My aggregation strategy correlates XML where the fulfillmentcenter element's
text is of a certain value. This works correctly. My message translator then
takes the exchange and processes it:

       public List<Map&lt;String, Object>> processAggregate(List orders)
throws Exception {
      log.info("Processing the aggregate");
      List<Map&lt;String, Object>> results = new ArrayList<Map&lt;String,
Object>>();

      // 1 - Add the header first
      Map<String, Object> header = new HashMap<String, Object>();
      header.put("orderNumber", "Order Number");
      header.put("firstName", "First Name");
      header.put("lastName", "Last Name");
      results.add(header);

      try {
         if (orders != null) {
            // 2 - Add each order ID
            for (int i = 0; i < orders.size(); i++) {
               com.pluralsight.orderfulfillment.generated.Order order =
unmarshallOrder((String) orders
                     .get(i));
               Map<String, Object> row = new HashMap<String, Object>();
               row.put("orderNumber",
order.getOrderType().getOrderNumber());
               row.put("firstName", order.getOrderType().getFirstName());
               row.put("lastname", order.getOrderType().getLastName());
               results.add(row);
            }
         }
      } catch (Exception e) {
         log.error(
               "An error occurred while trying to process messages for the
abc fulfillment center: "
                     + e.getMessage(), e);
         throw e;
      }
      return results;
   }

As you can see, I'm accepting the list of orders as the parameter from the
body of the exchange. First, I add a header to the list of maps I plan to
return. I then unmarshal each XML from the input list and build maps to add
to the return list. 

Once message processing is complete, I marshall the results into CSV and
then send the contents to a file. 

The content that results in the body below is:

Last Name,Order Number,First Name
,1003,Jane,Smith
,1004,Larry,Horse
,1005,Michael,Tester

I expected the header to be in the correct sequence; however, its not. Also,
an extra comma is output to the left. Now, if I don't add the map for the
header, I get the following:

1003,Smith,Jane
1004,Horse,Larry
1005,Tester,Michael

Here are my questions:

1) Is marshalling the csv then sending the exchange to a file an accepted
approach? Or should I be using some other component in between the csv and
file components. 
2) How do I maintain the order of elements in a row. For instance, I want to
make sure that the columns are order number, first name, last name; however,
the map keys are not the way I would want to assure order. Any guidance
would be appreciated. 

Let me know if there is anything else I can provide. Thank you in advance
for your help. 



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-CSV-Component-Not-Outputting-Headers-Correctly-tp5761848.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel CSV Component Not Outputting Headers Correctly

Posted by mike_hoffy <mi...@hotmail.com>.
Figured it out. The lastName key needed to be the correct case (lastName
instead of lastname). 



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-CSV-Component-Not-Outputting-Headers-Correctly-tp5761848p5761927.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel CSV Component Not Outputting Headers Correctly

Posted by mike_hoffy <mi...@hotmail.com>.
Thank you Minh. I'm actually not using Bindy; however, your comment is
helpful. 



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-CSV-Component-Not-Outputting-Headers-Correctly-tp5761848p5761926.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel CSV Component Not Outputting Headers Correctly

Posted by Minh Tran <da...@gmail.com>.
I would expect Bindy to simply use an iterator on your Map<String, Object> row instance. In which case, the order is never guaranteed. Try a SortedMap instead or use POJOs with the DataField's pos attribute.

On 19/01/2015, at 3:18 AM, mike_hoffy <mi...@hotmail.com> wrote:

> I have the following 2.14.1 route:
> 
>               from("activemq:queue:ABC_FULFILLMENT_REQUEST")
>                     .aggregate(new
> ABCFulfillmentCenterAggregationStrategy())
>                     .xpath(
>                           "/o:Order/o:OrderType/o:FulfillmentCenter = '"
>                                 +
> com.pluralsight.orderfulfillment.generated.FulfillmentCenter.FULFILLMENT_CENTER_ONE.value()
>                                 + "'", Boolean.class, namespace)
>                     .completionInterval(10000)
>                     .beanRef("aBCFulfillmentProcessor",
> "processAggregate").marshal()
>                     .csv().to("file://" +
> filePath).to("mock:direct:result");
> 
> My aggregation strategy correlates XML where the fulfillmentcenter element's
> text is of a certain value. This works correctly. My message translator then
> takes the exchange and processes it:
> 
>       public List<Map&lt;String, Object>> processAggregate(List orders)
> throws Exception {
>      log.info("Processing the aggregate");
>      List<Map&lt;String, Object>> results = new ArrayList<Map&lt;String,
> Object>>();
> 
>      // 1 - Add the header first
>      Map<String, Object> header = new HashMap<String, Object>();
>      header.put("orderNumber", "Order Number");
>      header.put("firstName", "First Name");
>      header.put("lastName", "Last Name");
>      results.add(header);
> 
>      try {
>         if (orders != null) {
>            // 2 - Add each order ID
>            for (int i = 0; i < orders.size(); i++) {
>               com.pluralsight.orderfulfillment.generated.Order order =
> unmarshallOrder((String) orders
>                     .get(i));
>               Map<String, Object> row = new HashMap<String, Object>();
>               row.put("orderNumber",
> order.getOrderType().getOrderNumber());
>               row.put("firstName", order.getOrderType().getFirstName());
>               row.put("lastname", order.getOrderType().getLastName());
>               results.add(row);
>            }
>         }
>      } catch (Exception e) {
>         log.error(
>               "An error occurred while trying to process messages for the
> abc fulfillment center: "
>                     + e.getMessage(), e);
>         throw e;
>      }
>      return results;
>   }
> 
> As you can see, I'm accepting the list of orders as the parameter from the
> body of the exchange. First, I add a header to the list of maps I plan to
> return. I then unmarshal each XML from the input list and build maps to add
> to the return list. 
> 
> Once message processing is complete, I marshall the results into CSV and
> then send the contents to a file. 
> 
> The content that results in the body below is:
> 
> Last Name,Order Number,First Name
> ,1003,Jane,Smith
> ,1004,Larry,Horse
> ,1005,Michael,Tester
> 
> I expected the header to be in the correct sequence; however, its not. Also,
> an extra comma is output to the left. Now, if I don't add the map for the
> header, I get the following:
> 
> 1003,Smith,Jane
> 1004,Horse,Larry
> 1005,Tester,Michael
> 
> Here are my questions:
> 
> 1) Is marshalling the csv then sending the exchange to a file an accepted
> approach? Or should I be using some other component in between the csv and
> file components. 
> 2) How do I maintain the order of elements in a row. For instance, I want to
> make sure that the columns are order number, first name, last name; however,
> the map keys are not the way I would want to assure order. Any guidance
> would be appreciated. 
> 
> Let me know if there is anything else I can provide. Thank you in advance
> for your help. 
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-CSV-Component-Not-Outputting-Headers-Correctly-tp5761848.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Camel CSV Component Not Outputting Headers Correctly

Posted by mike_hoffy <mi...@hotmail.com>.
Claus,

Thank you for the prompt response. I have some improvement as the results
are now ordered; however, I'm getting an additional comma in the values:

Order Number,First Name,Last Name
1003,Jane,,Smith
1004,Larry,,Horse
1005,Michael,,Tester

I put a wiretap prior to marshalling and have the following:

Exchange[ExchangePattern: InOnly, BodyType: java.util.ArrayList, Body:
[{orderNumber=Order Number, firstName=First Name, lastName=Last Name},
{orderNumber=1003, firstName=Jane, lastname=Smith}, {orderNumber=1004,
firstName=Larry, lastname=Horse}, {orderNumber=1005, firstName=Michael,
lastname=Tester}]]

I'll try to look into the code for how this is being marshalled, but again,
any help would be appreciated. 



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-CSV-Component-Not-Outputting-Headers-Correctly-tp5761848p5761925.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel CSV Component Not Outputting Headers Correctly

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Use a LinkedHashMap to ensure the map is ordered

On Sun, Jan 18, 2015 at 5:18 PM, mike_hoffy <mi...@hotmail.com> wrote:
> I have the following 2.14.1 route:
>
>                from("activemq:queue:ABC_FULFILLMENT_REQUEST")
>                      .aggregate(new
> ABCFulfillmentCenterAggregationStrategy())
>                      .xpath(
>                            "/o:Order/o:OrderType/o:FulfillmentCenter = '"
>                                  +
> com.pluralsight.orderfulfillment.generated.FulfillmentCenter.FULFILLMENT_CENTER_ONE.value()
>                                  + "'", Boolean.class, namespace)
>                      .completionInterval(10000)
>                      .beanRef("aBCFulfillmentProcessor",
> "processAggregate").marshal()
>                      .csv().to("file://" +
> filePath).to("mock:direct:result");
>
> My aggregation strategy correlates XML where the fulfillmentcenter element's
> text is of a certain value. This works correctly. My message translator then
> takes the exchange and processes it:
>
>        public List<Map<String, Object>> processAggregate(List orders)
> throws Exception {
>       log.info("Processing the aggregate");
>       List<Map<String, Object>> results = new ArrayList<Map<String,
> Object>>();
>
>       // 1 - Add the header first
>       Map<String, Object> header = new HashMap<String, Object>();
>       header.put("orderNumber", "Order Number");
>       header.put("firstName", "First Name");
>       header.put("lastName", "Last Name");
>       results.add(header);
>
>       try {
>          if (orders != null) {
>             // 2 - Add each order ID
>             for (int i = 0; i < orders.size(); i++) {
>                com.pluralsight.orderfulfillment.generated.Order order =
> unmarshallOrder((String) orders
>                      .get(i));
>                Map<String, Object> row = new HashMap<String, Object>();
>                row.put("orderNumber",
> order.getOrderType().getOrderNumber());
>                row.put("firstName", order.getOrderType().getFirstName());
>                row.put("lastname", order.getOrderType().getLastName());
>                results.add(row);
>             }
>          }
>       } catch (Exception e) {
>          log.error(
>                "An error occurred while trying to process messages for the
> abc fulfillment center: "
>                      + e.getMessage(), e);
>          throw e;
>       }
>       return results;
>    }
>
> As you can see, I'm accepting the list of orders as the parameter from the
> body of the exchange. First, I add a header to the list of maps I plan to
> return. I then unmarshal each XML from the input list and build maps to add
> to the return list.
>
> Once message processing is complete, I marshall the results into CSV and
> then send the contents to a file.
>
> The content that results in the body below is:
>
> Last Name,Order Number,First Name
> ,1003,Jane,Smith
> ,1004,Larry,Horse
> ,1005,Michael,Tester
>
> I expected the header to be in the correct sequence; however, its not. Also,
> an extra comma is output to the left. Now, if I don't add the map for the
> header, I get the following:
>
> 1003,Smith,Jane
> 1004,Horse,Larry
> 1005,Tester,Michael
>
> Here are my questions:
>
> 1) Is marshalling the csv then sending the exchange to a file an accepted
> approach? Or should I be using some other component in between the csv and
> file components.
> 2) How do I maintain the order of elements in a row. For instance, I want to
> make sure that the columns are order number, first name, last name; however,
> the map keys are not the way I would want to assure order. Any guidance
> would be appreciated.
>
> Let me know if there is anything else I can provide. Thank you in advance
> for your help.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-CSV-Component-Not-Outputting-Headers-Correctly-tp5761848.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



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