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
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
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Data Engineering
  3. Databases
  4. A Guide to the InfluxDBMapper and QueryBuilder for Java: Part 2

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

Learn how to execute some queries against InfluxDB using the QueryBuilder combined with the InfluxDBMapper.

Emmanouil Gkatziouras user avatar by
Emmanouil Gkatziouras
CORE ·
Mar. 25, 19 · Tutorial
Like (5)
Save
Tweet
Share
6.48K Views

Join the DZone community and get the full member experience.

Join For Free

Previously, we set up an InfluxDB instance running through docker, and we also ran our first InfluxDBMapper code against an InfluxDB database.

The next step is to execute some queries against InfluxDB using the QueryBuilder combined with the InfluxDBMapper.Image title

Let’s get started and select everything from the table H2OFeetMeasurement.

private static final String DATABASE = "NOAA_water_database";

public static void main(String[] args) {
    InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:8086", "root", "root");

    InfluxDBMapper influxDBMapper = new InfluxDBMapper(influxDB);

    Query query = select().from(DATABASE,"h2o_feet");
    List h2OFeetMeasurements = influxDBMapper.query(query, H2OFeetMeasurement.class);
}

Let’s get more specific; we will select measurements with a water level higher than 8.

Query query = select().from(DATABASE,"h2o_feet").where(gt("water_level",8));
LOGGER.info("Executing query "+query.getCommand());
List higherThanMeasurements = influxDBMapper.query(query, H2OFeetMeasurement.class);

I bet you noticed the query.getCommand() detail. If you want to see the actual query that is being executed, you can call the getCommand() method from the query.

Apart from where statements, we can perform certain operations on fields such as calculations.

Query query = select().op(op(cop("water_level",MUL,2),"+",4)).from(DATABASE,"h2o_feet");
LOGGER.info("Executing query "+query.getCommand());
QueryResult queryResult = influxDB.query(query);

We just used the cop function to multiply the water level by 2. The cop function creates a clause that will execute an operation to a column. Then we are going to increment by 4 the product of the previous operation by using the op function. The op function creates a clause, which will execute an operation with regards to two arguments given.

The next case is to select using a specific string field key-value:

Query query = select().from(DATABASE,"h2o_feet").where(eq("location","santa_monica"));
LOGGER.info("Executing query "+query.getCommand());
List h2OFeetMeasurements = influxDBMapper.query(query, H2OFeetMeasurement.class);

Things can get even more specific and you can select data that have specific field key-values and tag key-values.

Query query = select().column("water_level").from(DATABASE,"h2o_feet")
                      .where(neq("location","santa_monica"))
                      .andNested()
                      .and(lt("water_level",-0.59))
                      .or(gt("water_level",9.95))
                      .close();
LOGGER.info("Executing query "+query.getCommand());
List h2OFeetMeasurements = influxDBMapper.query(query, H2OFeetMeasurement.class);

Since InfluxDB is a time series database, it is essential to issue queries with specific timestamps.

Query query = select().from(DATABASE,"h2o_feet")
                      .where(gt("time",subTime(7,DAY)));
LOGGER.info("Executing query "+query.getCommand());
List h2OFeetMeasurements = influxDBMapper.query(query, H2OFeetMeasurement.class);

Last but not least, we can make a query for specific fields. I will create a model just for the fields that we are going to retrieve.

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", timeUnit = TimeUnit.SECONDS)
public class LocationWithDescription {

    @Column(name = "time")
    private Instant time;

    @Column(name = "level description")
    private String levelDescription;

    @Column(name = "location")
    private String location;

    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;
    }
}

And now I shall query for them.

Query selectFields = select("level description","location").from(DATABASE,"h2o_feet");
List locationWithDescriptions = influxDBMapper.query(selectFields, LocationWithDescription.class);

As you can see, we can also map certain fields to a model. For now, mapping to models can be done only when data comes from certain measurements. Thus, we shall proceed to more query-builder-specific examples next time.

You can find the source code on GitHub.

Database Java (programming language)

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 7 Awesome Libraries for Java Unit and Integration Testing
  • Deploying Java Serverless Functions as AWS Lambda
  • What Is a Kubernetes CI/CD Pipeline?
  • Bye Bye, Regular Dev [Comic]

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
  • +1 (919) 678-0300

Let's be friends: