databene

 
  • Increase font size
  • Default font size
  • Decrease font size

flat file generation tutorial

for generating files from a configuration file, Benerator can be run from a setup file. You can copy the following example and save it as file 'benerator.xml':

<?xml version="1.0" encoding="iso-8859-1"?>
<setup>

     <import domains="product"
                  platforms="fixedwidth"/>

    <generate type="product" count="3">
        <id name="id" type="long" />
        <attribute name="ean_code" generator="EANGenerator"/>
        <attribute name="name" type="string" minLength="5" maxLength="20"/>
        <attribute name="price" type="big_decimal" min="0.49" max="99.99" precision="0.10"/>

        <consumer class="FixedWidthEntityExporter">
            <property name="uri" value="products.flat"/>
            <property name="columns" value="id[8r0],ean_code[13],name[20],price[8r0]"/>
        </consumer>

    </generate>

    <echo>Generated data:</echo>
    <if test="org.databene.commons.SystemInfo.isWindows()">
        <then>
            <execute type="shell">cmd /C type export.csv</execute>
        </then>
        <else>
            <execute type="shell">cat export.csv</execute>
        </else>
    </if>  

</setup>

The file has a central generate element which tells benerator to create three entities of type 'product'. An entity is a business object or, more general, a composite data object. Entities are composed of attributes. Special kinds of attributes are ids and references. More about them later.

The attributes are the data components of an entity. The attribute elements above describe, how the attributes should be generated:

  • id is generated as incremental long values starting with 1
  • ean_code is created by a distinct 'Generator' class. This is the first place where you can insert plugins. You will learn more about it later
  • name is an arbitrary string with a length of at least 5 and at most 20 characters
  • price is a big decimal value between 0.49 and 99.99 (both inclusive), with a precision of 0.10. This means the values 0.49, 0.59, 0.69, ..., 99.79, 99.89, 99.99

The consumer element tells benerator to instantiate the JavaBean class FlatFileEntityExporter and have it process each generated entity. The entities will be written to a file 'products.flat' using a flat file format, rendering the columns with fixed with, alignment and padding character:

id[8r0] means that the attribute 'id' is padded to eight characters, aligned to the right and padded with '0' characters.

ean_code[13] means padding to 13 characters using default alignment (left) and character (' ')

(For a more detailed explanation of the file format, see the file format introduction and the user manual)

So, finally run the example from the root directory of your benerator installation on a Unix system via

 bin/benerator.sh benerator.xml

or, on Windows, via

 bin\benerator.bat benerator.xml

If you stored the 'benerator.xml' file somewhere else you need to specify the relative or absolute path to it.

When executed, Benerator has created a flat file 'transactions.flat' like this:

00000001800035300638600009.850006
00000002800035000334000002.490018
00000003800035300638600009.850010
00000004807680000008500000.890022
00000005807680000008500000.890024
00000006807680000008500000.890024
...

And the last section (<if>) prints out the file content directly to the console, so that you do not need to locate the file in this tutorial.