Announcement Announcement Module
No announcement yet.
How to use FlatFileItemWriter? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use FlatFileItemWriter?

    I am trying to read from database and write to a file.
    Here is the config i took from one of the samples:

    <bean class="org.springframework.batch.item.file.FlatFileItemWriter"
          <property name="resource" ref="simpleFileLocator" />
          <property name="fieldSetCreator">
             <bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />
    Now I have the items from DB, but all i see in the file the reference address of the Domain Object.

    I implemented toString method of the domain object, but still no luck. The documentation is also not clear as to how to write to a file.

    BTW, i wanted to create a Fixed length file.

  • #2
    Hi seshu

    i am also doing the same thing as you, but i am reading from db and writing to fixed length file

    this is what i did to write the the property of my domain object to fixed length file

    <bean class="org.springframework.batch.item.file.FlatFil eItemWriter" id="fileItemWriter">
    <property name="resource" ref="simpleFileLocator" />
    <property name="fieldSetCreator">
    <bean class="test.CustomFieldSetMapper" />
    <property name="lineAggregator" ref="fixedLengthLineAggregator"/>

    <bean id="fixedLengthLineAggregator" class="org.springframework.batch.item.file.transfo rm.FixedLengthLineAggregator">
    <property name="columns" value="1-500,501-550,551-600"/>
    My CustomFieldSetMapper override the PassThroughFieldSetMapper mapItem method to return DefaultFieldsetMapper which contains string arrays of the values of the property of my domain object

    then i use the provided FixedLengthLineAggregator to write it to a file. works fine

    Dont forget to configure the customEditorConfigurer so that it recognizes the array.

    My question now is that if i want to do some data trimming ( because i notice if my data is longer than the specified column length, spring will throw error), what is the best practice to do this ? Is it from the sql query when i retrieve the data of when i map the Fieldset into my own domain object and trim it there ?

    Also i will need to do padding like providing 0 prefix for number ( example 10000 become 00010000 for 8 digit column ). where is the best place to do this ?

    any help is appreciated




    • #3
      Thank you Ballistic. Actually, the issue was with the location of the fixed length file. It was writing to a different directory.

      To your question of trimming and padding, I can see 2 quick solutions:
      1. As you said, change the sql query to pad and trim. I was planning to do this too.
      2. Or, we can implement trimming and padding logic in "RowMapper", where we map each row to a domain object.

      Or yet another way is to implement that logic in the CustomFieldSetMapper.

      Let me know which one works out best for you.



      • #4
        Hi Seshu,

        I think, your simpleFileLocator needs to implement the interface.

        The reason is that the "resource" property in the FlatFileItemWriter requires an object which implement the Resource interface.




        • #5
          Hi Vito,
          I was already using FileSystemResource for fileLocator. Again, as i mentioned in my previous post, the issue was with the location of the file.


          • #6
            Hi Seshu,

            Do you mean that the FlatFileItemWriter is writing to a different folder from the one specified in the simpleFileLocator?

            For me, I use
            <bean id="outputFile" class="org.springframework.batch.core.resource.Ste pExecutionResourceProxy" >
            <property name="filePattern" value="" />
            This "outputFile" bean is used as a reference to the "resource" property of FlatFileItemWriter. The value of "" is set in the jobParameters.

            jobParametersBuilder.addString("", stringFileName);
            The tricky part is the file naming format. I need to do several trials to get the correct format.

            In Linux environtment: Assuming "project" is a folder in the root.

            String stringFileName="file://project/test/FlatFiles/";
            In Windows environtment:

            String stringFileName="file://D:/project/test/FlatFiles/";
            I hope that it helps.




            • #7
              Thanks Vito. The problem was with the path. I have a relative path, so it created the file in the root directory of the eclipse project that i was running this code from.

              Anyway, the issue was solved by giving an absolute path as mentioned in your post.