DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

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

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
  1. DZone
  2. Data Engineering
  3. Databases
  4. A Guide to the InfluxDBMapper and QueryBuilder for Java: Part 1

A Guide to the InfluxDBMapper and QueryBuilder for Java: Part 1

This tutorial gives a guide for the InfluxDBMapper and QueryBuilder for Java.

Emmanouil Gkatziouras user avatar by
Emmanouil Gkatziouras
CORE ·
Mar. 22, 19 · Tutorial
Like (2)
Save
Tweet
Share
8.77K Views

Join the DZone community and get the full member experience.

Join For Free

With 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.

Java (programming language) Data (computing) Database Measurement (journal) InfluxDB Release (agency) Docker (software) Measure (physics) Driver (software)

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: