Skip to content

Create a Data Source

Philipp Schürmann edited this page Oct 23, 2015 · 1 revision

Every visualization needs a data source to pull the data for visualization.

You can create a data source by creating a class that implements the interface DataSource. The class has to implement the following method:

List<DataObject> getData(DataRequest dataRequest);

As data sources are very complex due to the data processing (filters, operations) they have to do, the framework already provides a list of abstract base classes that you can extend depending on the type of data source you want to create.

StreamDataSource

The most basic type you can extend is the com.github.mrgoro.interactivedata.core.data.source.StreamDataSource. As the name already suggests, this class makes use of the Java 8 Stream API.

To create a custom data source all you have to do is create a class that extends the StreamDataSource class. Your class then has to implement this method:

protected abstract Stream<T> getDataStream();

Your implementation has to provide a stream of objects - your custom data. That's it!

File based DataSource

The FileDataSource is an advanced version of the StreamDataSource that relies on the behaviour of the StreamDataSource. FileDataSource is optimized for use with data files, such as CSV. For files the usage of this base class is easier as it provides additional help to convert your file into the Stream required for StreamDataSource.

To create a DataSource for your file, create a new class that extends FileDataSource. At least your class has to implement the following method to return the Path to your data file:

protected abstract Path getPath();

The default implementation uses a semicolon (;) to separate values in a line. You can specify your own by overriden getSeparator().

It is convenient to supply the name fields name inside the first line of a file. This is also the default behaviour for the mapping of FileDataSource. You can adopt the behaviour by overriding the method getMappings() and supply a array of Strings as the field names of your values. Note that the default implementation skips the first line as it usually contains the headers. You can set the number of lines to skip (e.g. 0) by overriding the method skipLines().

For an example on how to implement a custom data source for a CSV file see the UserCsvDataSource from the sample app.

JPA Database DataSource

JpaSpecificationDataSource is an abstraction of the StreamDataSource for use with databases. Currently it relies on the Spring integration module. It leverages the possibilities of a JpaSpecificationExecutor of the Spring Data JPA project for quering data.

To implement a DataSource for your JPA database you have to create a custom class that extends JpaSpecificationExecutor and override the abstract method getRepository().

protected abstract JpaSpecificationExecutor<T> getRepository();

The easiest way to provide a JpaSpecificationExecutor is by providing a Repository (e.g. CrudRepository, PagingAndSortingRepository) of Spring Data JPA. See the reference documentation of Spring Data JPA on detailed information on repositories.

For an example usage see the TemperatureDataSource of the sample app and its corresponding TemperatureRepository.