首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让itemWriter创建文件?

如何让itemWriter创建文件?
EN

Stack Overflow用户
提问于 2018-10-31 18:25:22
回答 1查看 1.6K关注 0票数 0

我的ItemWriter没有写任何文件。调试后,它在ItemProcessor处停止。我的项目应该从Mongo接收信息,然后读取它,处理它,然后创建.txt文件并将信息存储在该文件中。我做错了什么或者我错过了什么?

BatchConfig:

代码语言:javascript
复制
   @Configuration
   @EnableBatchProcessing
   public class BatchConfig {
private static final Logger LOG =LoggerFactory.getLogger(BatchConfig.class);

@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;

@Autowired
private PaymentPortalJobListener listener;
@Autowired
private MongoTemplate mongoTemplate;

@Bean
public MongoItemReader<PaymentAudit> mongoReader() throws 
UnexpectedInputException, ParseException {
    LOG.info("Inside Mongo Item Reader Method");
    MongoItemReader<PaymentAudit> reader = new MongoItemReader<PaymentAudit> 
 ();
    reader.setTemplate(mongoTemplate);
    reader.setCollection("paymentAudit");
    reader.setTargetType((Class<? extends PaymentAudit>) 
    PaymentAudit.class);
    reader.setFields("{rxFname, rxLname}");
    reader.setQuery("{rxFname, rxLname}");
    Map<String, Sort.Direction> sorts = new HashMap<String, Sort.Direction> 
    (1);
    sorts.put("rxFName", Sort.Direction.ASC);
    reader.setSort(sorts);
    return reader;
  }

 @Bean
 public ItemProcessor<PaymentAudit, PaymentAudit> processor() {
    LOG.info("Inside Processor Method");
    return new PaymentPortalNOSQLProcessor();

 }

@Bean
public ItemWriter<PaymentAudit> writer() {
    LOG.info("Inside Writer Method");
    return new PaymentPortalNOSQLWriter();
}

@Bean
Job job(JobBuilderFactory jbf, StepBuilderFactory sbf, 
 PaymentPortalNOSQLProcessor processor,
        ItemWriter<? super PaymentAudit> writer) {

    Step s1 = sbf.get("local").<PaymentAudit, 
 PaymentAudit>chunk(100).reader(mongoReader()).processor(processor)
            .writer(writer).listener(listener).build();

    return jbf.get("etl").incrementer(new 
  RunIdIncrementer()).start(s1).build();
}

}

PaymentPortalNOSQLWriter:

代码语言:javascript
复制
 @Bean
 public FileWriter Mongowriter(){
    FileWriter writer = null;
    try {
        String fullPath =OUTPUT_FILENAME + FILENAME_EXTN;
        writer = new FileWriter(fullPath);
        writer.write("[");
    } catch (IOException e) {
        LOG.error("Exception occured in MongoWriter() :: 
    ApplicationConfiguration", e);
    }
    return writer;
  }

  @Override
  public void write(List items) throws Exception {


  } 


 }

PaymentPortalJobListener

代码语言:javascript
复制
 @Autowired
private ApplicationContext appContext;


  @Override
  public void beforeStep(StepExecution stepExecution) {
      long checkpoint = System.currentTimeMillis();
    LOG.info("ExamResult Job starts at : {} " , checkpoint / (1_000 * 1.0));
  }

  @Override
   public ExitStatus afterStep(StepExecution stepExecution) {
    if(stepExecution.getStatus() == BatchStatus.COMPLETED){
        LOG.info("ExamResult job completed successfully");
        try {
            FileWriter saveWriter = 
   (FileWriter)appContext.getBean("paymentPortalWriter");
            saveWriter.write("{}");
            saveWriter.flush();
            saveWriter.close();
        } catch (BeansException | IOException e) {
            LOG.error("Exception occured in afterStep() :: PaymentPortal", 
   e);
        }
    }
    return stepExecution.getExitStatus();
}
}

PaymentPortalNOSQLProcessor:

代码语言:javascript
复制
  public class PaymentPortalNOSQLProcessor implements 
 ItemProcessor<PaymentAudit, PaymentAudit> {
 private static final Logger LOG = 
 LoggerFactory.getLogger(PaymentPortalNOSQLProcessor.class);

@Override
public PaymentAudit process(PaymentAudit bean) throws Exception {
    LOG.debug("Processor method");
    return bean;

}
}

分级建造:

代码语言:javascript
复制
   buildscript {
ext {
    springBootVersion = '2.0.5.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle- 
plugin:${springBootVersion}")
}
 }

 apply plugin: 'java'
 apply plugin: 'eclipse'
 apply plugin: 'org.springframework.boot'
 apply plugin: 'io.spring.dependency-management'

 group = 'com.example'
 version = '0.0.1-SNAPSHOT'
 sourceCompatibility = 1.8

 repositories {
mavenCentral()
 }


dependencies {
compile('org.springframework.boot:spring-boot-starter-batch')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')

//compile("org.hsqldb:hsqldb")



compile('org.springframework.boot:spring-boot-devtools')
compileOnly('org.projectlombok:lombok')
compile group:'org.springframework.boot', name:'spring-boot-configuration- 
 processor'
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.batch:spring-batch-test')
// https://mvnrepository.com/artifact/com.h2database/h2
//testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'

 }
EN

回答 1

Stack Overflow用户

发布于 2018-11-02 07:46:05

我在这里看到一个关于I/O的小混乱,所以我会尽量说明清楚。

Spring批处理的主要特性之一是声明性I/O,这意味着您不需要编写代码来处理I/O,而是声明要做什么,Spring批处理将为您完成I/O。您的Mongowriter bean包含实际编写数据的代码,但它不应该。

代码语言:javascript
复制
@Bean
public FlatFileItemWriter<String> flatFileItemWriter() {
    return new FlatFileItemWriterBuilder<String>()
            .name("flatFileItemWriter")
            .resource(new FileSystemResource("data.txt"))
            .lineAggregator(new PassThroughLineAggregator<>())
            .build();
}

此配置代码声明了我们需要编写的内容以及编写它的位置,而不是Spring批处理处理的实际写入(打开/关闭文件、调用写入器等)。顺便说一句,没有创建文件的问题是由于您的代码示例没有在编写器上调用open方法。如果您的作者配置正确,如上文所示,则应修复此问题。

第二点是,您可能是这样做的,因为您希望在编写的项周围添加[] (从代码中看到writer.write("["); )。另一次,可以通过使用FlatFileHeaderCallbackFlatFileFooterCallback API以声明的方式进行此操作。例如:

代码语言:javascript
复制
@Bean
public FlatFileItemWriter<String> flatFileItemWriter() {
    return new FlatFileItemWriterBuilder<String>()
            .name("flatFileItemWriter")
            .resource(new FileSystemResource("data.txt"))
            .lineAggregator(new PassThroughLineAggregator<>())
            .headerCallback(writer -> writer.write('['))
            .footerCallback(writer -> writer.write(']'))
            .build();
}

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53089841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档