A Guide to the InfluxDBMapper and QueryBuilder for Java: Part 1
This tutorial gives a guide for the InfluxDBMapper and QueryBuilder for Java.
Join the DZone community and get the full member experience.
Join For FreeWith the release of the latest InfluxDB-Java driver version came along the InfluxDBMapper.
To get started, we need to spin up an InfluxDB instance, and Docker is the easiest way to do so. We just follow the steps as described here.
Now we have a database with some data, and we are ready to execute our queries.
We have the measure h2o_feet:
> SELECT * FROM "h2o_feet"
name: h2o_feet
--------------
time level description location water_level
2015-08-18T00:00:00Z below 3 feet santa_monica 2.064
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
[...]
2015-09-18T21:36:00Z between 3 and 6 feet santa_monica 5.066
2015-09-18T21:42:00Z between 3 and 6 feet santa_monica 4.938
Let's create a model for that.
package com.gkatzioura.mapper.showcase;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
@Measurement(name = "h2o_feet", database = "NOAA_water_database", timeUnit = TimeUnit.SECONDS)
public class H2OFeetMeasurement {
@Column(name = "time")
private Instant time;
@Column(name = "level description")
private String levelDescription;
@Column(name = "location")
private String location;
@Column(name = "water_level")
private Double waterLevel;
public Instant getTime() {
return time;
}
public void setTime(Instant time) {
this.time = time;
}
public String getLevelDescription() {
return levelDescription;
}
public void setLevelDescription(String levelDescription) {
this.levelDescription = levelDescription;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Double getWaterLevel() {
return waterLevel;
}
public void setWaterLevel(Double waterLevel) {
this.waterLevel = waterLevel;
}
}
And then we will fetch all the entries of the h2o_feet measurement.
package com.gkatzioura.mapper.showcase;
import java.util.List;
import java.util.logging.Logger;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.impl.InfluxDBImpl;
import org.influxdb.impl.InfluxDBMapper;
public class InfluxDBMapperShowcase {
private static final Logger LOGGER = Logger.getLogger(InfluxDBMapperShowcase.class.getName());
public static void main(String[] args) {
InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:8086", "root", "root");
InfluxDBMapper influxDBMapper = new InfluxDBMapper(influxDB);
List h2OFeetMeasurements = influxDBMapper.query(H2OFeetMeasurement.class);
}
}
After being successful in fetching the data, we will continue with persisting the data.
H2OFeetMeasurement h2OFeetMeasurement = new H2OFeetMeasurement();
h2OFeetMeasurement.setTime(Instant.now());
h2OFeetMeasurement.setLevelDescription("Just a test");
h2OFeetMeasurement.setLocation("London");
h2OFeetMeasurement.setWaterLevel(1.4d);
influxDBMapper.save(h2OFeetMeasurement);
List measurements = influxDBMapper.query(H2OFeetMeasurement.class);
H2OFeetMeasurement h2OFeetMeasurement1 = measurements.get(measurements.size()-1);
assert h2OFeetMeasurement1.getLevelDescription().equals("Just a test");
Apparently, fetching all the measurements to get the last entry is not the most efficient thing to do. In the upcoming tutorials, we are going to see how we use the InfluxDBMapper with advanced InfluxDB queries.
Let us know your thoughts in the comments.
Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Tech Hiring: Trends, Predictions, and Strategies for Success
-
Mastering Time Series Analysis: Techniques, Models, and Strategies
-
Adding Mermaid Diagrams to Markdown Documents
-
Event-Driven Architecture Using Serverless Technologies
Comments