0

I started using Spring Batch (version 4.1.2) with SpringBoot(version 2.1.7). Now I generate reports in csv format and save them to the file system. My next step is to save the file to the database in binary format. The tutorials and examples show how to save entities FROM strings in csv files, but I can't figure out how to save the whole file in binary.

I will be grateful for any advice! Here are my steps for now:

<bean id="jdbcItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="job">
        <property name="dataSource" ref="dataSource"/>
        <property name="rowMapper">
            <bean class="org.springframework.jdbc.core.ColumnMapRowMapper"/>
        </property>
        <property name="sql" value="#{jobParameters['sql']}"/>
        <property name="preparedStatementSetter">
            <bean class="ru.bpc.otter.batch.PreparedStatementSetter">
                <constructor-arg value="#{jobParameters}"/>
            </bean>
        </property>
    </bean>
    <bean id="csvItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="job">
        <property name="resource" value="file://#{jobParameters['file.name']}"/>
        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="delimiter" value=";"/>
                <property name="fieldExtractor">
                    <bean class="org.springframework.batch.item.file.transform.PassThroughFieldExtractor"/>
                </property>
            </bean>
        </property>
    </bean>

    <bean class="org.springframework.batch.core.scope.JobScope"/>

    <batch:job id="generateReportJob" restartable="false">
        <batch:step id="generateReportStep">
            <batch:tasklet>
                <batch:chunk reader="jdbcItemReader" writer="csvItemWriter" commit-interval="${commit.interval:10000}"/>
            </batch:tasklet>
        </batch:step>
    </batch:job>
  • `save the file to the database in binary format`: how would you do it without Spring Batch? Now the code to do this can be put is a tasklet and added as a step after the step that generates the file. – Mahmoud Ben Hassine Nov 22 '19 at 15:09
  • You haven't mentioned the RDBMS name but generally , concept is to read whole file into a byte array and save that byte array into a BLOB type column of table. Refer [Answer](https://stackoverflow.com/a/2579397/3850730). You DB must provide BLOB type - Oracle & DB2 do , not sure about others. – Sabir Khan Nov 28 '19 at 08:22

0 Answers0