【问题标题】:How to skip CSV header line with camel-beanio如何使用 camel-beanio 跳过 CSV 标题行
【发布时间】:2015-10-31 18:56:37
【问题描述】:

从 apache 使用 camel-beanio 时如何跳过 CSV 标题行?

我用于映射的 XML 文件如下所示:

<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>

但要让我的代码运行,我必须手动删除第一行(标题行)。如何自动跳过标题行?

【问题讨论】:

    标签: csv header apache-camel bean-io


    【解决方案1】:

    要读取 CSV 文件并忽略第一个标题行,可以将标题的第一个字段值定义为 commentsCSV Stream

    CSV 示例:

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

    映射文件:

    <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>
    

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

    另一种方法是使用camel-bindy 代替camel-beanio 和新选项skipFirstLine(参见https://camel.apache.org/components/latest/bindy-dataformat.html#_1_csvrecord

    【讨论】:

      【解决方案2】:

      快捷方式:

      一旦定义了 BeanReader 来读取/处理记录,就使用它的 skip 方法和 count 1 来跳过标题。

      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)
      

      请参阅http://beanio.org/2.0/docs/reference/index.html#TheMappingFile。 跳过方法签名:

       public int skip(int count) throws BeanReaderException;  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 1970-01-01
        • 2018-01-30
        • 2022-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多