Skip to content
Jesse White edited this page Apr 13, 2016 · 6 revisions

Java API

Example:

import org.opennms.newts.api.*;
...

// Create a collection of Sample objects
List<Sample> samples = new ArrayList<Sample>();

samples.add(
    new Sample(
        Timestamp.now(),
        new Resource("/devices/sw1/interfaces"),
        "ifInOctets",
        MetricType.COUNTER,
        new Counter(123456789000L)
    )
);

...

// Create new Cassandra session instance
CassandraSession session = new CassandraSession("newts", "localhost", 9042, "NONE", "cassandra", "cassandra", false);
// Number of seconds to keep samples for
int ttl = 86400 * 365;
// Codahale metric registry (http://metrics.dropwizard.io).xs
MetricRegistry registry = new MetricRegistry();
// Create an empty sample processor service
SampleProcessorService processors = new SampleProcessorService(32);

// Sample repositories are used for reading/writing
SampleRepository repo = new CassandraSampleRepository(session, ttl, registry, processors, new ContextConfigurations());

// Insert!
repo.insert(samples);

Example:

Resource resource = new Resource("/devices/sw1/interfaces");
Timestamp start, end;
end = Timestamp.now();
start = end.minus(Duration.minutes(120));

// Select raw samples
Results<Sample> samples = repo.select(Context.DEFAULT_CONTEXT, resource, Optional.of(start), Optional.of(end));

Example:

Resource resource = new Resource("/devices/sw1/interfaces");
Timestamp start, end;
end = Timestamp.now();
start = end.minus(Duration.minutes(120));

// Create a result descriptor with a 5 minute sample interval (300 seconds).
// Generate averages for the metrics named "ifInOctets" and "ifOutOctets",
// with a heartbeat of 600 seconds; Name the aggregates "in" and "out"
// respectively, and export them both.
ResultDescriptor descriptor = new ResultDescriptor(300)
    .datasource("in",  "ifInOctets",  Duration.seconds(600), StandardAggregationFunctions.AVERAGE)
    .datasource("out", "ifOutOctets", Duration.seconds(600), StandardAggregationFunctions.AVERAGE)
    .export("in", "out");

Duration duration = Duration.minutes(15);

// Select aggregated measurements
Results<Measurement> measurements = repo.select(Context.DEFAULT_CONTEXT, resource, Optional.of(start), Optional.of(end), descriptor, Optional.of(duration));
Clone this wiki locally