Skip to content

Java API

cnoelle edited this page Apr 8, 2018 · 6 revisions

Content

Setup

Add a dependency to the FendoDB API to your project. For Maven users:

<dependency>
	<groupId>org.smartrplace.logging</groupId>
	<artifactId>fendodb-api</artifactId>
	<version>0.0.2</version>
</dependency>

If you are not using OSGi but want to use FendoDB in a standalone Java application, see the Usage guide instead.

Create a database

The starting point for an application using the database is the FendoDbFactory interface. In an OSGi-environment retrieve an instance as a service, e.g. using the @Reference declarative service annotation:

@Reference
private FendoDbFactory factory;

and create a database instance:

CloseableDataRecorder db = factory.getInstance("test");

The getInstance method either returns an existing database, or creates a new one with default settings. In order to control the configuration settings for the database, a FendoDbConfiguration object can be passed to the method. Create it using a builder:

final FendoDbConfiguration config = FendoDbConfigurationBuilder.getInstance()
    .setReadOnlyMode(true)
    .build();
try (final CloseableDataRecorder db = factory.getInstance(Paths.get("test"), config)) {
    final List<FendoTimeSeries> timeSeries = db.getAllTimeSeries();
    System.out.println("All time series: " + timeSeries);
}

Add data points

Use the insertValue or insertValues method to add data points to a time series:

try (final CloseableDataRecorder db = factory.getInstance(Paths.get("test"))) {
    final RecordedDataConfiguration cfg = new RecordedDataConfiguration();
    cfg.setStorageType(StorageType.ON_VALUE_UPDATE);
    final FendoTimeSeries timeSeries = slots.createRecordedDataStorage("timeSeries0", cfg);
    final long start = System.currentTimeMillis();
    final List<SampledValue> values = Arrays.asList(
        new SampledValue(new DoubleValue(0.1), start, Quality.GOOD),
        new SampledValue(new DoubleValue(0.3), start + 60000, Quality.GOOD),
        new SampledValue(new DoubleValue(0.6), start + 120000, Quality.GOOD)
	);
    timeSeries.insertValues(values);
    timeSeries.insertValue(new SampledValue(new DoubleValue(1), start + 180000, Quality.GOOD));
}

Read data

The preferred way to read a potentially large set of data points is to use an iterator, or a stream:

try (final CloseableDataRecorder db = factory.getInstance(Paths.get("test"))) {
    final FendoTimeSeries timeSeries = slots.getRecordedDataStorage("timeSeries0");
    final Iterator<SampledValue> iterator = timeSeries.iterator(); // optionally specify start and end time
    while (iterator.hasNext()) {
        final SampledValue next = iterator.next();
        System.out.println("Next value: " + next);
    }
    // alternatively, as a stream:
    timeSeries.getValuesAsStream().forEach(value -> System.out.println("Next value2: " + value));
}

It is also possible to retrieve all data points in a given interval in a bulk read operation:

    final long now = System.currentTimeMillis();
    final long oneHour = 60 * 60 * 1000;
    final List<SampledValue> values = timeSeries.getValues(now - oneHour, now);
}

By means of the getNextValue and getPReviousValue methods the first and last data points in a time series can be queried:

    final SampledValue first = timeSeries.getNextValue(Long.MIN_VALUE);
    final SampledValue last = timeSeries.getPreviousValue(Long.MAX_VALUE);

and the size-methods allow to query the total number of data points, or the number within some interval:

    final int size = timeSeries.size();

Edit properties/tags

See Properties, tags. Example:

try (final CloseableDataRecorder slots = factory.getInstance(Paths.get("test"))) {
    final RecordedDataConfiguration cfg = new RecordedDataConfiguration();
    cfg.setStorageType(StorageType.ON_VALUE_UPDATE);
    final FendoTimeSeries ts = slots.createRecordedDataStorage("timeseries0", cfg);
    ts.setProperty("applicationDomain", "heating");
    ts.addProperty("applicationDomain", "electricity");
    ts.addProperty("data", "measurement");
    System.out.println(" All properties: " + ts.getProperties());
    ts.removeProperty("applicationDomain", "heating");
    ts.removeProperty("data"); // remove all values for the key
    System.out.println(" All properties 2: " + ts.getProperties());
}

Next

Clone this wiki locally