Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring批处理-未能初始化读取器_Java_Xml_Spring_Batch Processing - Fatal编程技术网

Java Spring批处理-未能初始化读取器

Java Spring批处理-未能初始化读取器,java,xml,spring,batch-processing,Java,Xml,Spring,Batch Processing,解决方案:需要转换的.csv文件的文件路径 这是错误的 我在尝试运行批处理spring应用程序时遇到一些问题 目标:我想读取一个csv文件并将其加载到一个类中(我的老板告诉我的),我可以肯定的是,我使用的代码样本直接向MySQL收费 在这里,我发布了我的项目图片: -这是文件- App.java package springbach; import org.springframework.batch.core.Job; import org.springframework.batch.co

解决方案:需要转换的.csv文件的文件路径 这是错误的


我在尝试运行批处理spring应用程序时遇到一些问题

目标:我想读取一个csv文件并将其加载到一个类中(我的老板告诉我的),我可以肯定的是,我使用的代码样本直接向MySQL收费

在这里,我发布了我的项目图片:

-这是文件-

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>

异常声明它无法解析类路径资源。因此,您可以尝试使用文件:springbachdata.csv或类路径:springbachdata.csv,如果springbachdata.csv位于资源文件夹下。

确保您试图读取的文件存在于给定路径中。文件:C:/Java/workspace/springbach/src/main/Java/springbachdata.csvAlso如果您的文件位于类路径中,则使用类路径:/springbatchdata.csvt确认它是该文件,你是怎么解决的?你能把答案贴出来吗?是什么解决了你的问题?
<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>
<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>
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