databene

...because software quality matters

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

flat file generation tutorial

E-mail Print PDF

flat file generation tutorial

for generating files from a configuration file, Benerator can be run from a setup file. See the following example:

  <?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE setup SYSTEM "http://databene.org/benerator-0.3.dtd">
<setup>
<create-entities name="product" count="3">
<attribute name="id" type="long" min="1" max="1000000000" distribution="step"/>
<attribute name="ean_code" generator="org.databene.domain.product.EANGenerator"/>
<attribute name="name" type="string" minLength="5"/>
<attribute name="price" type="big_decimal" min="0.49" max="99.99" precision="0.10"/>
<consumer class="org.databene.platform.flat.FlatFileEntityExporter">
<property name="uri" value="products.flat"/>
<property name="properties" value="id[8r0],ean_code[13],name[20],price[8r0]"/>
</consumer>
</create-entities>
</setup>

The file has a central create-entities element which tells benerator to create three entities of type 'product'. An entity is the word, that benerator uses for business objects or, more general, composite data objects. Entities are composed of attributes .

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

  • id is generated as continuous long values from 1 to 1000000000
  • 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 of lenght 5
  • 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 tell benerator to instantiate the JavaBean class org.databene.platform.flat.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 file_format.html )

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

 bin/benerator demo/file/create_flat.ben.xml

or, on Windows, via

 bin\benerator demo\file\create_flat.ben.xml

benerator will create a flat file 'transactions.flat' like this:

 00000001800035300638600009.850006
00000002800035000334000002.490018
00000003800035300638600009.850010
00000004807680000008500000.890022
00000005807680000008500000.890024
00000006807680000008500000.890024
...

(When running offline, you will need to remove the DOCTYPE declaration from the file demo/file/create_flat.ben.xml)