2

How to skip CSV header line when using camel-beanio from apache?

My XML file for mapping look like this:

<beanio>
<record name="myRecord" class="my.package.MyConditionClass">
 <field name="myField" position="1" />
 <field name="mylist" position="2" collection="list" type ="string"/>
 <segment name="conditions" class="my.package.MyConditionClass" nillable="true" collection="map" key="myKey">
 <field name="myKey" position="2">
 <field name="myValue" position="3">
</segment>
</record>
</beanio>

But to make my code run i must delete the first line (header line) manually. How do skip the header line automatically?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
nanachimi
  • 406
  • 6
  • 23

2 Answers2

1

To read a CSV file and ignore the first header line, you can define the first field value of the header as comments of the CSV Stream

Example of CSV :

toto;tata;titi
product1;1;18
product2;2;36
product3;5;102

The mapping file :

<beanio ...
    <stream name="dataStream" format="csv" >
        <parser>
            <property name="delimiter" value=";" />
            <!-- ignore header line -->
            <property name="comments" value="toto" /> 
        </parser>
        <record name="record" minOccurs="0" maxOccurs="unbounded" class="com.stackoverflow.Product" />
    </stream>
</beanio>

Source : http://beanio.org/2.0/docs/reference/index.html#CSVStreamFormat

Another way will be to use camel-bindy in place of camel-beanio and the new option skipFirstLine (see https://camel.apache.org/components/latest/bindy-dataformat.html#_1_csvrecord)

1

Shortcut:

As soon as define BeanReader to read/process the records, use it's skip method with count 1 to skip the header.

e.g.
   //  Define Reader to process records
   BeanReader beanReader = factory.createReader("STREAM",inputStreamReader);

   // Skip First Record
   beanReader.skip(1);

   // Process rest of Stream
   Object record;
   do {
      try {
          record = beanReader.read();
      }
      catch (BeanReaderException e) {
         e.printStackTrace();
      }
   } while(record !=null)

Refer http://beanio.org/2.0/docs/reference/index.html#TheMappingFile. Skip method signature:

 public int skip(int count) throws BeanReaderException;  
Ganesan R
  • 61
  • 4