Spring批量中的本地分区

时间:2017-03-24 20:34:59

标签: spring spring-batch batch-processing partitioning

我实际上使用本地分区将具有100K行的客户表导出到多个XML文件(因为我无法将数据导出到一个文件,因为docDbClient.readCollection(docUrl, function (err, collection) { console.log(collection); }, function(err){ console.log(err); }); 不是线程 - 安全)但即使将StaxEventItemWriter<T>增加到10,我也不会用多个线程获得更好的结果。我认为StaxEventItemWriter中的问题是因为我有一些代码错误,如:

gridSize

阅读器

 java.lang.NullPointerException: null
    at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.flush(XMLStreamWriterImpl.java:397) ~[na:1.8.0_121]
    at com.sun.xml.internal.stream.writers.XMLEventWriterImpl.flush(XMLEventWriterImpl.java:212) ~[na:1.8.0_121]
    at org.springframework.batch.item.xml.StaxEventItemWriter.close(StaxEventItemWriter.java:674) ~[spring-batch-infrastructure-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:354) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:277) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.run(DisposableBeanAdapter.java:236) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.batch.core.scope.context.StepContext.close(StepContext.java:213) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.batch.core.scope.context.StepSynchronizationManager$1.close(StepSynchronizationManager.java:53) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.batch.core.scope.context.StepSynchronizationManager$1.close(StepSynchronizationManager.java:36) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.batch.core.scope.context.SynchronizationManagerSupport.release(SynchronizationManagerSupport.java:190) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.batch.core.scope.context.StepSynchronizationManager.release(StepSynchronizationManager.java:112) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.batch.core.step.AbstractStep.doExecutionRelease(AbstractStep.java:290) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:278) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:139) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:136) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]

作家

    @Bean
    @StepScope
    public JdbcPagingItemReader<Customer> pagingItemReader(
            @Value("#{stepExecutionContext['minValue']}")Long minValue,
            @Value("#{stepExecutionContext['maxValue']}")Long maxValue) {
        System.out.println("reading " + minValue + " to " + maxValue);
        JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();

        reader.setDataSource(this.dataSource);
        reader.setFetchSize(10000);
        reader.setRowMapper(new CustomerRowMapper());

        MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
        queryProvider.setSelectClause("id, firstName, lastName, birthdate");
        queryProvider.setFromClause("from customer");
        queryProvider.setWhereClause("where id >= " + minValue + " and id <= " + maxValue);

        Map<String, Order> sortKeys = new HashMap<>(1);

        sortKeys.put("id", Order.ASCENDING);

        queryProvider.setSortKeys(sortKeys);

        reader.setQueryProvider(queryProvider);

        return reader;
    }

分区程序

 @Bean
    @StepScope
    public StaxEventItemWriter<Customer> bookItemWriter(
        @Value("#{stepExecutionContext['ressource']}")Object ressource
        ) {

        XStreamMarshaller marshaller = new XStreamMarshaller();

        StaxEventItemWriter<Customer> itemWriter = new StaxEventItemWriter<>();

        itemWriter.setRootTagName("customers");
        itemWriter.setMarshaller(marshaller());




        File file = new File("C\\resources\\data\\"+ressource+".xml");

        String customerOutputPath=file.getAbsolutePath();

        itemWriter.setResource(new FileSystemResource(customerOutputPath));



        return itemWriter;
    }

感谢您的帮助。

0 个答案:

没有答案
相关问题