6

Solution: The file path of the .csv file that needs to be transformed is wrong


i'm having some issues trying to run a batch spring application.

Objectives: I want to read a csv file and load it on a class (my boss told me that), what i know for sure is that the sample of code that i use, directly charges the data to MySQL.

Here i post a picture of my project:

https://i.sstatic.net/ieEmK.png

-Here are the files-

App.java

package springbach;

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  = 
        {   "database.xml", 
            "context.xml",
            "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");

  }
}

Report.java

package springbach;


public class Report {

    private String Date;
    private String Impressions;
    private String Clicks;
    private String Earning;


    public String getDate() {
        return Date;
    }
    public void setDate(String date) {
        Date = date;
    }
    public String getImpressions() {
        return Impressions;
    }
    public void setImpressions(String impressions) {
        Impressions = impressions;
    }
    public String getClicks() {
        return Clicks;
    }
    public void setClicks(String clicks) {
        Clicks = clicks;
    }
    public String getEarning() {
        return Earning;
    }
    public void setEarning(String earning) {
        Earning = earning;
    }

}

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>

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>

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="springbach.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="file:C:\Java\workspace\springbach\src\main\java\springbachdata.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">   
                 <!-- return back to reader, rather than a mapped object. -->
                 <!--
             <bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />
                  --> 
              <!-- map to an object -->
              <bean
                class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                <property name="prototypeBeanName" value="report" />
              </bean>           
          </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>

-Here is my console error-

Nov 13, 2013 9:24:52 AM org.springframework.batch.core.step.AbstractStep execute
SEVERE: Encountered an error executing the step
org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:142)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:306)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:192)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:137)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:152)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:131)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:134)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:127)
    at springbach.App.main(App.java:27)
Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): URL [file:C:/Java/workspace/springbach/src/main/java/springbachdata.csv]
    at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:251)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:139)
    ... 14 more

Nov 13, 2013 9:24:53 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=reportJob]] completed with the following parameters: [{}] and the following status: [FAILED]
Exit Status : FAILED
Done
7
  • Make sure that the file which you are trying to read exist in given path..file:C:/Java/workspace/springbach/src/main/java/springbachdata.csv Commented Nov 13, 2013 at 12:45
  • Also if your file is in your classpath then use classpath:/springbatchdata.csv Commented Nov 13, 2013 at 12:55
  • Thanks it was the file, now is working n.n Commented Nov 13, 2013 at 12:56
  • how you have fixed it? Commented Nov 13, 2013 at 12:57
  • 3
    Can you post the answer what fixed your problem ? Commented Apr 14, 2014 at 11:38

1 Answer 1

6

The exception states that it is not able to resolve class path resource. So you can try using file:springbachdata.csv or classpath:springbachdata.csv, if springbachdata.csv is under resource folder.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.