5 Replies Latest reply on Aug 26, 2016 9:25 AM by richardmoore

    Can the csvitemwriter write a file without a header?

    richardmoore

      I need to be able to write out a file without a header. I have left the property out and assigned "header" a value of "", in both instances I get -

      JBERET060000: Invalid reader or writer property value null for key header

        • 1. Re: Can the csvitemwriter write a file without a header?
          cfang

          From my recollection, it's required to specify a header property.  This is needed by the underlying csv lib (supercsv).  I think it's a good practice to keep the header in the generated output.

           

          There is another csv item writer in jberet-support: org.jberet.support.io.JacksonCsvItemWriter, where you can skip the header with useHeader property.

           

          pls see https://github.com/jberet/jsr352/blob/master/jberet-support/src/test/resources/META-INF/batch-jobs/org.jberet.support.io.JacksonCsvReaderTest.xml

           

          jsr352/JacksonCsvItemReaderWriterTest.java at master · jberet/jsr352 · GitHub

           

          In above test class, testListType and testStringArrayType skip headers.

          • 2. Re: Can the csvitemwriter write a file without a header?
            richardmoore

            I setup a test but must be missing something. I have this step setup as follows -

             

            <chunk>
            <reader ref="jdbcItemReader">
            <properties>
            <property name="dataSourceLookup" value="db2"/>
            <property name="sql" value="SELECT fclty_cd, fclty_ltr2_cd, fclty_nm, dw_fclty_ltr2_cd, current timestamp as current_ts FROM db2pdba.aim_fclty" />
            <property name="beanType" value="java.util.Map" />
            </properties>
            </reader>
            <writer ref="jacksonCsvItemWriter">
            <properties>
            <property name="resource" value="#{systemProperties['APP_DATA']}/MyBatchletTest_test.out" />
            <property name="writeMode" value="overwrite" />
            <property name="columnSeparator" value="|" />
            <property name="beanType" value="java.util.Map" />
            <property name="useHeader" value="false" />
            <property name="csvGeneratorFeatures" value="ALWAYS_QUOTE_STRINGS=false"/>
            <property name="columns" value="FCLTY_CD STRING, FCLTY_LTR2_CD STRING, FCLTY_NM STRING, DW_FCLTY_LTR2_CD STRING, CURRENT_TS STRING" />
            </properties>
            </writer>
            </chunk>

             

            The output is as follows -

            "00"|"KC"|"KANSAS CITY         "|"KC"|1472158934721

            "01"|"SP"|"SPRINGFIELD         "|"SP"|1472158934721

            "02"|"GM"|"FORT SCOTT          "|"FS"|1472158934721

            "03"|"OK"|"OKLA CITY           "|"OK"|1472158934721

            "04"|"SO"|"MEMPHIS             "|"SO"|1472158934721

            "05"|"GO"|"NASHVILLE           "|"GO"|1472158934721

            "06"|"GC"|"GULF COAST          "|"GC"|1472158934721

            "07"|"GM"|"MEMPHIS-VM          "|"ME"|1472158934721

            "08"|"FW"|"FORT WORTH          "|"FW"|1472158934721

            "09"|"M1"|"FUTURE NAM          "|"M1"|1472158934721

            "10"|"R2"|"FUTURE NAME         "|"R2"|1472158934721

            "11"|"NO"|"NEBRASKA            "|"NO"|1472158934721

            "12"|"KE"|"GREAT LAKES         "|"KE"|1472158934721

             

            1. I have tried to remove the quotes with the csvGeneratorFeatures but they still show up. Is there a way to stop quoting?
            2. Is there a way to trim trailing spaces from a column (for example, column 3 in the above output)?
            • 3. Re: Can the csvitemwriter write a file without a header?
              cfang

              having quote around string values is a good practice, and is always safe to keep.  Most csv tools will be able to process your data.

               

              If we blindly disable quoting string values, what if some values contains the delimiters?  You just cannot foresee what kind of data you have.

               

              If you want to disable it:

               

              <property name="csvGeneratorFeatures" value="ALWAYS_QUOTE_STRINGS=false, STRICT_CHECK_FOR_QUOTING=true"/>


              with that, the writer will add quote char only where necessary.

              • 4. Re: Can the csvitemwriter write a file without a header?
                cfang

                I think whitespace trimming can be best handled inside your data class (e.g., Employee class, Person class), which knows exactly what string fields can be safely trimmed when reading from the item reader.

                • 5. Re: Can the csvitemwriter write a file without a header?
                  richardmoore

                  I agree with having the quotes, but I am converting an existing process over to the framework and that is how it was originally implemented.