使用Spring Batch,boot和Spring Jpa将数据从Oracle数据库加载到CSV文件中

时间:2017-02-05 05:41:16

标签: spring-boot spring-data-jpa spring-batch

您好我是第一次使用春季批次。我的用例是我必须使用Spring引导和Spring Data将数据从oracle db加载到csv文件。 我在ItemReader中配置oracle db时遇到了困难。有人可以帮助我。

这是我的代码

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public PersonAcctRepository personAcctRepository;



    @Bean
    public ItemWriter<CustomerInsr> writer(){

        FlatFileItemWriter<CustomerInsr> writer = new FlatFileItemWriter<CustomerInsr>();

        String exportFileHeader = "FIRST_NAM, MIDDLE_NAM, LAST_NAM";
        StringHeaderWriter headerWriter =  new StringHeaderWriter(exportFileHeader);
        writer.setHeaderCallback(headerWriter);
        writer.setResource(new ClassPathResource("sample-data.csv"));

        LineAggregator<CustomerInsr> lineAgg = createCustomerInsrAgg();
        writer.setLineAggregator(lineAgg);



        return writer;

    }

    @Bean
    public LineAggregator<CustomerInsr> createCustomerInsrAgg(){

        DelimitedLineAggregator<CaresCustomerInsr> deliAgg = new DelimitedLineAggregator<CustomerInsr>();
        deliAgg.setDelimiter(",");

        FieldExtractor<CustomerInsr> fieldExtractor = createCustomerInsrExtractor();
        deliAgg.setFieldExtractor(fieldExtractor);
        return deliAgg;

    }

    @Bean
    public FieldExtractor<CustomerInsr> createCustomerInsrExtractor(){

        BeanWrapperFieldExtractor<CustomerInsr> fieldExtractor = new BeanWrapperFieldExtractor<CustomerInsr>();
        fieldExtractor.setNames(new String []{"firstName", "middleName", "lastName"});
        return fieldExtractor;  
    }

    @Bean
    public ItemReader<CustomerInsr> reader(){

        RepositoryItemReader<CustomerInsr> reader  = new RepositoryItemReader<CustomerInsr> ();
        reader.setRepository(personAcctRepository);
        reader.setMethodName("findAll");
        return reader;  
    }


    @Bean
    public Job job(){

        return jobBuilderFactory.get("job")
               .incrementer(new RunIdIncrementer())
               .flow(step())
               .end()
               .build();

    }


    @Bean
    public Step  step(){

        return stepBuilderFactory.get("step")
               .<CustomerInsr, CustomerInsr> chunk(10)
               .reader(reader())
               .writer(writer())
               .build();
    }

}

1 个答案:

答案 0 :(得分:0)

您的用例是我经常遇到的用例。我经常写一个小应用程序(spring boot,spring batch)来为你的用例提供一个意见的实现。

您可以克隆the repo并手动添加oracle驱动程序。添加正确的驱动程序后,您可以将应用程序重新打包到新的jar中(如果您使用maven使用包构建阶段)。

在application.yml中提供数据库凭据和卸载数据的设置(这是一个外部配置文件,类似于database.properties)。

我冒昧地为您提供了一个示例,该示例使用CustomerInsr作为表,firstName,middleName和lastName作为您要卸载的列:

spring:
  datasource:
    url: jdbc:oracle:thin:@yoururl:yourport:yoursid
    username: 
    password:         

output-dir: your/directory/target/
commit-interval: 10
page-size: 10
delimiter: ","
quote: "\""
quote-escape: "\\"
table-definitions:
  - file-name: CustomerInsr.csv    
    select-query: "SELECT firstName, middleName, lastName"
    from-query: "FROM CustomerInsr"
    sort-column: firstName

从命令行运行jar,例如:

java -jar jdbc-unload-1.0.0.jar --spring.config.location=c:\yourlocation\application.yml  

如果您有任何疑问,请随时问我。