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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Prototype for a Java Database Application With REST and Security
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Handling Embedded Data in NoSQL With Java

Trending

  • How to Submit a Post to DZone
  • Implementing Secure API Gateways for Microservices Architecture
  • 7 Technology Waves I’ve Seen in 30 Years of Software — Will AI Be the Next Real Transformation?
  • 5 Common Security Pitfalls in Serverless Architectures
  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.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Mar. 25, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
8.5K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Prototype for a Java Database Application With REST and Security
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Handling Embedded Data in NoSQL With Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook