Spring 批处理 - 读取 CSV 文件并写入 MySQL 数据库给出 int 和 Date 数据类型的错误

Spring Batch- Read an CSV file and write to MySQL Database giving error for int and Date datatypeses

我正在尝试执行 Spring 批处理示例 – CSV 文件到 MySQL 数据库。

当我 运行 App.java 显示以下错误时。

java.lang.NumberFormatException: Unparseable number: Clicks
java.lang.IllegalArgumentException: Unparseable date: "Order Date", format: [dd/MM/yyyy]

谁能告诉我哪里做错了..

源代码:

report.csv

OrderDate,Impressions,Clicks,Earning
6/1/13, "139,237", 37, 227.21
6/2/13, "149,582", 55, 234.71
6/3/13, "457,425", 132, 211.48
6/4/13, "466,870", 141, 298.40
6/5/13,"472,385",194,281.35
    ......

resources/spring/batch/config/database.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

  <!-- connect to database -->
  <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>

  <bean id="transactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

  <!-- create job-meta tables automatically -->
  <jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
    <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
  </jdbc:initialize-database>

</beans

resources/spring/batch/config/context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

  <!-- stored job-metadata in database -->
  <bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseType" value="mysql" />
  </bean>

  <!-- stored job-metadata in memory -->
  <!-- 
  <bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
  </bean>
   -->

  <bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
  </bean>

</beans>

resources/spring/batch/jobs/job-report.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:batch="http://www.springframework.org/schema/batch" 
        xmlns:task="http://www.springframework.org/schema/task"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/batch
        http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

      <bean id="report" class="com.mkyong.model.Report" scope="prototype" />

      <batch:job id="reportJob">
        <batch:step id="step1">
          <batch:tasklet>
            <batch:chunk reader="cvsFileItemReader" writer="mysqlItemWriter"
                commit-interval="2">
            </batch:chunk>
          </batch:tasklet>
        </batch:step>
      </batch:job>

      <bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">

        <!-- Read a csv file -->
        <property name="resource" value="classpath:cvs/report.csv" />

        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
              <!-- split it -->
              <property name="lineTokenizer">
                    <bean
                  class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="date,impressions,clicks,earning" />
                </bean>
              </property>
              <property name="fieldSetMapper">   


                  <!-- map to an object -->
   <bean class="com.mkyong.ReportFieldSetMapper" />     
              </property>

              </bean>
          </property>

      </bean>

      <bean id="mysqlItemWriter"
        class="org.springframework.batch.item.database.JdbcBatchItemWriter">
        <property name="dataSource" ref="dataSource" />
        <property name="sql">
          <value>
                <![CDATA[        
                    insert into RAW_REPORT(DATE,IMPRESSIONS,CLICKS,EARNING) 
                values (:date, :impressions, :clicks, :earning)
                ]]>
          </value>
        </property>
        <!-- It will take care matching between object property and sql name parameter -->
        <property name="itemSqlParameterSourceProvider">
            <bean
            class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
        </property>
      </bean>

    </beans>

com/mkyong/model/Report.java

package com.mkyong.model;

public class Report {

    private Date orderDate;
    private String impressions;
    private int clicks;
    private String earning;

    //getter and setter methods


}

com/mkyong/App.java

package com.mkyong;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
  public static void main(String[] args) {

    String[] springConfig  = 
        {   "spring/batch/config/database.xml", 
            "spring/batch/config/context.xml",
            "spring/batch/jobs/job-report.xml" 
        };

    ApplicationContext context = 
        new ClassPathXmlApplicationContext(springConfig);

    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    Job job = (Job) context.getBean("reportJob");

    try {

        JobExecution execution = jobLauncher.run(job, new JobParameters());
        System.out.println("Exit Status : " + execution.getStatus());

    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Done");

  }
}

ReportFieldSetMapper.java

package com.mkyong;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import com.mkyong.model.Report;

public class ReportFieldSetMapper implements FieldSetMapper<Report> {

    @Override
    public Report mapFieldSet(FieldSet fieldSet) throws BindException {

        Report report = new Report();

        report.setOrderDate(fieldSet.readDate(0,"dd/MM/yyyy"));
        report.setImpressions(fieldSet.readString (1));
        report.setClicks;(fieldSet.readInt(2));
        report.setEarning(fieldSet.readString(3));

    }

}

您似乎在尝试解析 header 行

<property name="linesToSkip" value="1"></property> 添加到您的 bean <bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">