Announcement Announcement Module
Collapse
No announcement yet.
FormatterLineAggregator vs FixedLengthLineAggregator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • FormatterLineAggregator vs FixedLengthLineAggregator

    I am little bit confused on the usage of FormatterLineAggregator.

    In Spring Batch 1.x I used to use FixedLengthLineAggregator to format and output my header/content/footer, that worked just fine.

    I do not see that class in 2.x and after much research found that it is replaced by FormatterLineAggregator. Well I am confused on the usage of this.

    I did
    Code:
    <beans:bean id="reportHeaderLine1" class="org.springframework.batch.item.file.transform.FormatterLineAggregator"
    				p:format="1s%-6s" />
    It is printing out

    1s[1, REPORT]

    While I would expect the output to be:
    1 REPORT

    with no 1s and square brackets and the commas.

    I am sure there is a trick to do this, can someone help? Also how do I provide ranges using this?? like: 1, 4-6, 10-34

    I used to have this in FixedLengthLineAggregator and I am not sure how to do that in 2.x Any help is greatly appreciated.

    Thanks!

  • #2
    I have nailed it down to have it display:
    [1, REPORT]

    Now how do I get rid of the [] and ,

    Thanks!

    Comment


    • #3
      Did you provide a FieldExtractor? You can look at the fixed length example from the ioSample job to see usage.

      Comment


      • #4
        Note that your format is also wrong. Assuming that the types in your array are [int, String], it should probably be:

        Code:
        p:format="%1d %6s"
        Is the item you are aggregating already an array? If so, you don't need to specify a fieldExtractor because the PassThroughFieldExtractor is the default.

        Comment


        • #5
          I am looking for a features similar to FixedLengthLineAggregator. So that I can specify the location of values[1, 3-7, 12-50] and then return those values as["1", "REPORT", "REPORTNAME"]

          I looked through FieldExtractor, I am not sure whether it will let me do that, will give that a try. In the meanwhile, wanted to check how can I do this 2.x which I could easily do in 1.x

          Thanks!

          Comment


          • #6
            Here is the post that we used for 1.x, do we have a substitute for 2.x?

            I am not sure why writing a fixed length output be so complicated... any help is greatly appreciated:
            http://forum.springsource.org/showth...t=fixed+output

            Lucas, in the jira issue mentioned in the above post you have mentioned that the document is fixed to have some details, can you direct me to that? Thanks!
            Last edited by hailspring; May 5th, 2009, 09:55 AM. Reason: add details

            Comment


            • #7
              Take a look at the fixedLength sample: https://src.springframework.org/svn/...ixedLength.xml

              It uses the FormatterLineAggregator to aggregate a CustomerCredit object. The aggregator is defined as follows:

              Code:
              <bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
                  <property name="fieldExtractor">
                      <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                          <property name="names" value="name,credit" />
                      </bean>
                  </property>
                  <property name="format" value="%-9s%-2.0f" />
              </bean>
              The format, "%-9s%-2.0f", will apply to an array containing two items. See http://java.sun.com/j2se/1.5.0/docs/...Formatter.html for more information on Java's Formatter.

              The BeanWrapperFieldExtractor is pretty convenient if the object you are aggregating has getters for the properties you need to write, but you can write your own custom FieldExtractor too:

              Code:
              public class CustomerCreditFieldExtractor implements FieldExtractor<CustomerCredit> {
                  public Object[] extract(CustomerCredit item) {
                      return new Object[]{item.getName(), item.getCredit()};
                  }
              }
              As mentioned before, the PassThroughFieldExtractor is useful when then object being aggregated is already an array or a Collection.
              Last edited by DHGarrette; May 5th, 2009, 11:03 AM.

              Comment


              • #8
                Ok.... that makes it clear...! Is there any reason why FixedLengthLineAggregator was removed? :-) just curious.

                Comment


                • #9
                  The LineAggregator interface was all wrong in 1.x because it was the wrong level of abstraction (FieldSet is not a useful concept on the output side). The simple case is still pretty simple, but customisation is much easier, and there are things you can do with the FieldExtractor that you simply couldn't do before.

                  Comment

                  Working...
                  X