camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > CSV
Date Mon, 19 Mar 2012 11:17:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/CSV">CSV</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bvahdat">Babak
Vahdat</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Using autogenColumns, configRef and strategyRef attributes inside XML DSL <br>*Available
as of Camel 2.9.2 / 2.10* <br> <br>You can customize the CSV [Data Format] to
make use of your own CVSConfig and/or CVSStrategy. Also note that the default value for autogenColumns
is true. The following example should illustrates this. <br> <br>{code:xml} <br>&lt;route&gt;
<br>  &lt;from uri=&quot;direct:start&quot; /&gt; <br>  &lt;marshal&gt;
<br>    &lt;!-- make use of a strategy other than the default one which is &#39;org.apache.commons.csv.CSVStrategy.DEFAULT_STRATEGY&#39;
--&gt; <br>    &lt;csv autogenColumns=&quot;false&quot; delimiter=&quot;|&quot;
configRef=&quot;csvConfig&quot; strategyRef=&quot;excelStrategy&quot; /&gt;
<br>  &lt;/marshal&gt; <br>  &lt;convertBodyTo type=&quot;java.lang.String&quot;
/&gt; <br>  &lt;to uri=&quot;mock:result&quot; /&gt; <br>&lt;/route&gt;
<br> <br>&lt;bean id=&quot;csvConfig&quot; class=&quot;org.apache.commons.csv.writer.CSVConfig&quot;&gt;
<br>  &lt;property name=&quot;fields&quot;&gt; <br>    &lt;list&gt;
<br>      &lt;bean class=&quot;org.apache.commons.csv.writer.CSVField&quot;&gt;
<br>        &lt;property name=&quot;name&quot; value=&quot;orderId&quot;
/&gt; <br>      &lt;/bean&gt; <br>      &lt;bean class=&quot;org.apache.commons.csv.writer.CSVField&quot;&gt;
<br>        &lt;property name=&quot;name&quot; value=&quot;amount&quot;
/&gt; <br>      &lt;/bean&gt; <br>    &lt;/list&gt; <br>
 &lt;/property&gt; <br>&lt;/bean&gt; <br> <br>&lt;bean
id=&quot;excelStrategy&quot; class=&quot;org.springframework.beans.factory.config.FieldRetrievingFactoryBean&quot;&gt;
<br>  &lt;property name=&quot;staticField&quot; value=&quot;org.apache.commons.csv.CSVStrategy.EXCEL_STRATEGY&quot;
/&gt; <br>&lt;/bean&gt; <br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Dependencies <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="CSV-CSV"></a>CSV</h2>

<p>The CSV <a href="/confluence/display/CAMEL/Data+Format" title="Data Format">Data
Format</a> uses <a href="http://commons.apache.org/sandbox/csv/" class="external-link"
rel="nofollow">Apache Commons CSV</a> to handle CSV payloads (Comma Separated Values)
such as those exported/imported by Excel.</p>

<h3><a name="CSV-Options"></a>Options</h3>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> config </td>
<td class='confluenceTd'> CSVConfig </td>
<td class='confluenceTd'> Can be used to set a custom CSVConfig object. </td>
</tr>
<tr>
<td class='confluenceTd'> strategy </td>
<td class='confluenceTd'> CSVStrategy </td>
<td class='confluenceTd'> Camel uses by default <tt>CSVStrategy.DEFAULT_STRATEGY</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> autogenColumns </td>
<td class='confluenceTd'> boolean </td>
<td class='confluenceTd'> <b>Camel 1.6.1/2.0:</b> Is default true. By default,
columns are autogenerated in the resulting CSV. Subsequent messages use the previously created
columns with new fields being added at the end of the line. </td>
</tr>
<tr>
<td class='confluenceTd'> delimiter </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> Is default <tt>,</tt>.
Can be used to configure the delimiter, if it's not the comma. </td>
</tr>
</tbody></table>
</div>


<h3><a name="CSV-MarshallingaMaptoCSV"></a>Marshalling a Map to CSV</h3>
<p>The component allows you to marshal a Java Map (or any other message type that can
be <a href="/confluence/display/CAMEL/Type+Converter" title="Type Converter">converted</a>
in a Map) into a CSV payload.</p>

<p>An example: if you send a message with this map...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Map&lt;<span class="code-object">String</span>,
<span class="code-object">Object</span>&gt; body = <span class="code-keyword">new</span>
HashMap&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt;();
body.put(<span class="code-quote">"foo"</span>, <span class="code-quote">"abc"</span>);
body.put(<span class="code-quote">"bar"</span>, 123);
</pre>
</div></div>
<p>... through this route ...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>).
    marshal().csv().
    to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>... you will end up with a String containing this CSV message</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>abc,123</pre>
</div></div>
<p>Sending the Map below through this route will result in a CSV message that looks
like <tt>foo,bar</tt></p>

<h3><a name="CSV-UnmarshallingaCSVmessageintoaJavaList"></a>Unmarshalling
a CSV message into a Java List</h3>
<p>Unmarshalling will transform a CSV messsage into a Java List with CSV file lines
(containing another List with all the field values).</p>

<p>An example: we have a CSV file with names of persons, their IQ and their current
activity.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-sql">Jack Dalton, 115, mad at Averell
Joe Dalton, 105, calming Joe
William Dalton, 105, keeping Joe <span class="code-keyword">from</span> killing
Averell
Averell Dalton, 80, playing with Rantanplan
Lucky Luke, 120, capturing the Daltons
</pre>
</div></div>

<p>We can now use the CSV component to unmarshal this file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"file:src/test/resources/?fileName=daltons.csv&amp;noop=<span
class="code-keyword">true</span>"</span>).
    unmarshal().csv().
    to(<span class="code-quote">"mock:daltons"</span>);
</pre>
</div></div>

<p>The resulting message will contain a <tt>List&lt;List&lt;String&gt;&gt;</tt>
like...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">List&lt;List&lt;<span class="code-object">String</span>&gt;&gt;
data = (List&lt;List&lt;<span class="code-object">String</span>&gt;&gt;)
exchange.getIn().getBody();
<span class="code-keyword">for</span> (List&lt;<span class="code-object">String</span>&gt;
line : data) {
    LOG.debug(<span class="code-object">String</span>.format(<span class="code-quote">"%s
has an IQ of %s and is currently %s"</span>,
                            line.get(0), line.get(1), line.get(2)));
}
</pre>
</div></div>

<h3><a name="CSV-MarshallingaList%3CMap%3EtoCSV"></a>Marshalling a List&lt;Map&gt;
to CSV</h3>
<p><b>Available as of Camel 2.1</b></p>

<p>If you have multiple rows of data you want to be marshalled into CSV format you can
now store the message payload as a <tt>List&lt;Map&lt;String, Object&gt;&gt;</tt>
object where the list contains a Map for each row.</p>

<h3><a name="CSV-FilePollerofCSV%2Cthenunmarshaling"></a>File Poller of
CSV, then unmarshaling</h3>

<p>Given a bean which can handle the incoming data...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>MyCsvHandler.java</b></div><div
class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// Some comments here
</span><span class="code-keyword">public</span> void doHandleCsvData(List&lt;List&lt;<span
class="code-object">String</span>&gt;&gt; csvData)
{
    <span class="code-comment">// <span class="code-keyword">do</span> magic
here
</span>}
</pre>
</div></div> 

<p>... your route then looks as follows</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- poll every
10 seconds --&gt;</span></span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"file:///some/path/to/pickup/csvfiles?delete=true&amp;amp;consumer.delay=10000"</span>
/&gt;</span>
        <span class="code-tag">&lt;unmarshal&gt;</span><span class="code-tag">&lt;csv
/&gt;</span><span class="code-tag">&lt;/unmarshal&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:myCsvHandler?method=doHandleCsvData"</span>
/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>


<h3><a name="CSV-Marshalingwithapipeasdelimiter"></a>Marshaling with a pipe
as delimiter</h3>

<p>Using the Spring/XML DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
  <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>
/&gt;</span>
  <span class="code-tag">&lt;marshal&gt;</span>
    <span class="code-tag">&lt;csv delimiter=<span class="code-quote">"|"</span>
/&gt;</span>
  <span class="code-tag">&lt;/marshal&gt;</span>
  <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:myCsvHandler?method=doHandleCsv"</span>
/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>Or the Java DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CsvDataFormat csv = <span class="code-keyword">new</span> CsvDataFormat();
CSVConfig config = <span class="code-keyword">new</span> CSVConfig();
config.setDelimiter('|');
csv.setConfig(config);

from(<span class="code-quote">"direct:start"</span>)
  .marshal(csv)
  .convertBodyTo(<span class="code-object">String</span>.class)
.to(<span class="code-quote">"bean:myCsvHandler?method=doHandleCsv"</span>);
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CsvDataFormat csv = <span class="code-keyword">new</span> CsvDataFormat();
csv.setDelimiter(<span class="code-quote">"|"</span>);

from(<span class="code-quote">"direct:start"</span>)
  .marshal(csv)
  .convertBodyTo(<span class="code-object">String</span>.class)
.to(<span class="code-quote">"bean:myCsvHandler?method=doHandleCsv"</span>);
</pre>
</div></div>

<h3><a name="CSV-Unmarshalingwithapipeasdelimiter"></a>Unmarshaling with
a pipe as delimiter</h3>

<p>Using the Spring/XML DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
  <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>
/&gt;</span>
  <span class="code-tag">&lt;unmarshal&gt;</span>
    <span class="code-tag">&lt;csv delimiter=<span class="code-quote">"|"</span>
/&gt;</span>
  <span class="code-tag">&lt;/unmarshal&gt;</span>
  <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:myCsvHandler?method=doHandleCsv"</span>
/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>Or the Java DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CsvDataFormat csv = <span class="code-keyword">new</span> CsvDataFormat();
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
strategy.setDelimiter('|');
csv.setStrategy(strategy);

from(<span class="code-quote">"direct:start"</span>)
  .unmarshal(csv)
.to(<span class="code-quote">"bean:myCsvHandler?method=doHandleCsv"</span>);
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CsvDataFormat csv = <span class="code-keyword">new</span> CsvDataFormat();
csv.setDelimiter(<span class="code-quote">"|"</span>);

from(<span class="code-quote">"direct:start"</span>)
  .unmarshal(csv)
.to(<span class="code-quote">"bean:myCsvHandler?method=doHandleCsv"</span>);
</pre>
</div></div>

<h3><a name="CSV-UsingautogenColumns%2CconfigRefandstrategyRefattributesinsideXMLDSL"></a>Using
autogenColumns, configRef and strategyRef attributes inside XML DSL</h3>
<p><b>Available as of Camel 2.9.2 / 2.10</b></p>

<p>You can customize the CSV <a href="/confluence/display/CAMEL/Data+Format" title="Data
Format">Data Format</a> to make use of your own CVSConfig and/or CVSStrategy. Also
note that the default value for autogenColumns is true. The following example should illustrates
this.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
  <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>
/&gt;</span>
  <span class="code-tag">&lt;marshal&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- make use of
a strategy other than the default one which is 'org.apache.commons.csv.CSVStrategy.DEFAULT_STRATEGY'
--&gt;</span></span>
    <span class="code-tag">&lt;csv autogenColumns=<span class="code-quote">"false"</span>
delimiter=<span class="code-quote">"|"</span> configRef=<span class="code-quote">"csvConfig"</span>
strategyRef=<span class="code-quote">"excelStrategy"</span> /&gt;</span>
  <span class="code-tag">&lt;/marshal&gt;</span>
  <span class="code-tag">&lt;convertBodyTo type=<span class="code-quote">"java.lang.String"</span>
/&gt;</span>
  <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>
/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"csvConfig"</span>
class=<span class="code-quote">"org.apache.commons.csv.writer.CSVConfig"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"fields"</span>&gt;</span>
    <span class="code-tag">&lt;list&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.commons.csv.writer.CSVField"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"name"</span>
value=<span class="code-quote">"orderId"</span> /&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.commons.csv.writer.CSVField"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"name"</span>
value=<span class="code-quote">"amount"</span> /&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
    <span class="code-tag">&lt;/list&gt;</span>
  <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"excelStrategy"</span>
class=<span class="code-quote">"org.springframework.beans.factory.config.FieldRetrievingFactoryBean"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"staticField"</span>
value=<span class="code-quote">"org.apache.commons.csv.CSVStrategy.EXCEL_STRATEGY"</span>
/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<h3><a name="CSV-Dependencies"></a>Dependencies</h3>

<p>To use CSV in your camel routes you need to add the a dependency on <b>camel-csv</b>
which implements this data format. </p>

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-csv&lt;/artifactId&gt;
  &lt;version&gt;2.0.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CSV">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=80010&revisedVersion=16&originalVersion=15">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CSV?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message