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 Thu, 21 Mar 2013 21:52: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/~muellerc">Christian
Mueller</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" >from(&quot;direct:start&quot;)
<br>  .unmarshal(csv) <br></td></tr>
            <tr><td class="diff-unchanged" >  .to(&quot;bean:myCsvHandler?method=doHandleCsv&quot;);
<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >from(&quot;direct:start&quot;)
<br>  .unmarshal(csv) <br></td></tr>
            <tr><td class="diff-unchanged" >  .to(&quot;bean:myCsvHandler?method=doHandleCsv&quot;);
<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{code}
<br>CsvDataFormat csv = new CsvDataFormat(); <br>CSVConfig csvConfig = new CSVConfig();
<br>csvConfig.setDelimiter(&quot;;&quot;); <br>csv.setConfig(csvConfig);
<br> <br>from(&quot;direct:start&quot;) <br>  .unmarshal(csv) <br>
 .to(&quot;bean:myCsvHandler?method=doHandleCsv&quot;); <br>{code} <br>
<br>{info:title=Issue in CSVConfig} <br>It looks like that <br>{code} <br>CSVConfig
csvConfig = new CSVConfig(); <br>csvConfig.setDelimiter(&#39;;&#39;); <br>{code}
<br>doesn&#39;t work. You have to set the delimiter as a String! <br>{info}
<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/proper/commons-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 <tt>CSVConfig</tt>
object. </td>
</tr>
<tr>
<td class='confluenceTd'> strategy </td>
<td class='confluenceTd'> CSVStrategy </td>
<td class='confluenceTd'> Can be used to set a custom <tt>CSVStrategy</tt>;
the default is <tt>CSVStrategy.DEFAULT_STRATEGY</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> autogenColumns </td>
<td class='confluenceTd'> boolean </td>
<td class='confluenceTd'> Whether or not columns are auto-generated in the resulting
CSV. The default value is <tt>true</tt>; 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> The column delimiter to use;
the default value is "<tt>,</tt>". </td>
</tr>
<tr>
<td class='confluenceTd'> skipFirstLine </td>
<td class='confluenceTd'> boolean </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Whether or not to skip the
first line of CSV input when unmarshalling (e.g. if the content has headers on the first line);
the default value is <tt>false</tt>. </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-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 <tt>CSVConfig</tt> and/or
<tt>CSVStrategy</tt>. Also note that the default value of the <tt>autogenColumns</tt>
option is true. The following example should illustrate this customization.</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-UsingskipFirstLineoptionwhileunmarshaling"></a>Using skipFirstLine
option while unmarshaling</h3>
<p><b>Available as of Camel 2.10</b></p>

<p>You can instruct the CSV <a href="/confluence/display/CAMEL/Data+Format" title="Data
Format">Data Format</a> to skip the first line which contains the CSV headers. 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 skipFirstLine=<span class="code-quote">"true"</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();
csv.setSkipFirstLine(<span class="code-keyword">true</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-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>

<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 csvConfig = <span class="code-keyword">new</span> CSVConfig();
csvConfig.setDelimiter(<span class="code-quote">";"</span>);
csv.setConfig(csvConfig);

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='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Issue in
CSVConfig</b><br />It looks like that
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CSVConfig csvConfig = <span class="code-keyword">new</span> CSVConfig();
csvConfig.setDelimiter(';');
</pre>
</div></div>
<p>doesn't work. You have to set the delimiter as a String!</p></td></tr></table></div>

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

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

<p>If you use Maven you can just add the following to your pom.xml, substituting the
version number for the latest and 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;x.x.x&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=24&originalVersion=23">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