Using the Time Series Client Library
Using the Time Series Client Library
The Time Series service provides a REST-based API that you can access programatically using Java.
About This Task
In addition to a RESTful API that can be accessed using any language, the Time Series service provides a Java-based client library interface to the service. Use this library to help save time with configuring the WebSocket and HTTP connections, for example.
To use the Time Series client library, you should have a basic understanding of how to develop Java applications. You can use the Time Series client library to make calls to the Time Series API.
- Average
- Interpolation
- Scale
To use the client library, complete the tasks in tss-setup-configure.html#reference_7ed111be-2b84-42c8-b8de-5d2a4f9bb799, then complete the following steps:
Procedure
Including the Time Series Client in Your Project
The Time Series client must be included in your project.
Procedure
Configuring the Time Series Client Library
About This Task
predix-timeseries.properties
file. Procedure
Creating an Ingestion Request Using the Time Series Client Library
Learn how to create a time series data ingestion request using the example in this topic.
About This Task
The following shows an example data ingestion request using one good, one bad, and one uncertain data point. These values are derived from an active data source, such as a sensor.
Integer sensorValueAsInt = (int) Math.random();
Double sensorValueAsDouble = Math.random();
IngestionRequestBuilder ingestionBuilder = IngestionRequestBuilder.createIngestionRequest()
.withMessageId("<MessageID>")
.addIngestionTag(IngestionTag.Builder.createIngestionTag()
.withTagName("TagName")
.addDataPoints(
Arrays.asList(
new DataPoint(new Date().getTime(), sensorValueAsInt, Quality.GOOD),
new DataPoint(new Date().getTime(), sensorValueAsDouble, Quality.NOT_APPLICABLE),
new DataPoint(new Date().getTime(), "Bad Value", Quality.BAD),
new DataPoint(new Date().getTime(), null, Quality.UNCERTAIN)
)
)
.addAttribute("AttributeKey", "AttributeValue")
.addAttribute("AttributeKey2", "AttributeValue2")
.build());
String json = ingestionBuilder.build().get(0);
IngestionResponse response = ClientFactory.ingestionClientForTenant(ingestionTenant).ingest(json);
String responseStr = response.getMessageId() + response.getStatusCode();
Creating a Query Using the Time Series Client Library
The client library also offers a Query Builder that interfaces with the Time Series service and return a QueryResponse object.
About This Task
Procedure
QueryBuilder builder = QueryBuilder.createQuery()
.withStartAbs(1427463525000L)
.withEndAbs(1427483525000L)
.addTags(
QueryTag.Builder.createQueryTag()
.withTagNames(Arrays.asList("ALT_SENSOR", "TEMP_SENSOR"))
.withLimit(1000)
.addAggregation(Aggregation.Builder.averageWithInterval(1, TimeUnit.HOURS))
.addFilters(FilterBuilder.getInstance()
.addAttributeFilter("host", Arrays.asList("<host>")).build())
.addFilters(FilterBuilder.getInstance()
.addAttributeFilter("type", Arrays.asList("<type>")).build())
.addFilters(FilterBuilder.getInstance()
.addMeasurementFilter(FilterBuilder.Condition.GREATER_THAN_OR_EQUALS, Arrays.asList("23.1")).build())
.addFilters(FilterBuilder.getInstance()
.withQualitiesFilter(Arrays.asList(Quality.BAD, Quality.GOOD)).build())
.build());
QueryResponse response = ClientFactory.queryClientForTenant(tenant).queryAll(builder.build());