Print Properties
CSV Files

CSV Data Source

The description below shows how to configure Inforama project to use CSV Files as a data source.

CSV Data Source configuration

In order to make the CSV data source available in the project the following entry should be added to the datasources.cfg.xml file.
<datasources>
	...
	<datasource name="csv_datasource" type="ds.csv"/>
	...
</datasources>

The data source definition doesn't take any configuration properties, it just indicates that the CSV data source should be available under the name specified by the name attribute. (csv_datasource in the example above).

CSV Data Set configuration

In order to configure a CSV data set the following properties needs to be specified:
  • filepath - points to a CSV file that should be used.
  • firstrowcols - indicates whether the first row of the CSV file should act as column names. The value of this property should be specified as either true or false.
  • separator - the value of this property specified a separator character (e.g. "," or "\t") used in the CSV file.
  • query - this property specifies a SQL-like query used to retrieve the data from a CSV file.

A sample data set configuration is shown below.

<datasets>
...
<dataset datasource="csv_datasource" name="csv">
	<property name="filepath" value="$P{csvFileName}.csv" valueClass="java.lang.String"/>
	<property name="firstrowcols" value="true" valueClass="java.lang.String"/>
	<property name="query" value="select *" valueClass="java.lang.String"/>
	<property name="separator" value="," valueClass="java.lang.String"/>
</dataset>
....
</datasets>

The example above specifies the data set named "csv" created from the "csv_datasource" data source. The path to a CSV file should be provided in a "csvFileName" parameter. The first row from a CSV file should act as column names. The specified query "select *" means that the entire content of the file should be used as data set and, finally, values in a CSV file should be separeted by a "," character.

The "query" property

The "query" property from a data set definition specifies whith part of the CSV file should be used to build the data set. The query should be in the form: select column_names where conditions

  • column_names - a list of comma separated column names or '*' character meaning "all columns". The columns can be referenced using indexes enclosed within square brackets (e.g. [2] - means second column) or column names (works only when the first row of the associated CSV file acts as column names). So, for example, query: select [1],[2] will built a data set only from the first two columns from a CSV file.
  • conditions - list of conditions filtering a data set that will be built. For example query: select first_name, last_name where city='Dublin' will be build a data set only from rows whose "city" columns' value equals "Dublin".

Accessing a CSV data set within a document

There are two ways of referring to a CSV data set within a document.
  • The 'standard' way, which is: $F{[data_set_name].[column_name]}, for example: $F{csv.first_name} - will get the value from the "first_name" column.
  • Using column indexes: For example: $F{csv.[3]} will retrieve the value from the third column.

The first (standard) way will work only when the first row of the associated CSV file acts as column names (i.e. "firstrowcols" property is set to "true").

Sample Inforama Project using a CSV data source (CsvProject.zip) and a sample WebService client application using the project are in the attachemt.

You can download the following sample files which demonstrate how to use this datasource...

1030 Views, 1 Attachments 1 Attachments

  • Comments
Copyright © 2008 - 2010