How to fetch and write influxdb data using java

Thursday, June 20, 2019 By Ashish Doneriya

Influx time series database is a platform for storing, collecting, visualizing and managing time-series data.

There is already official java api in maven and github. But I have created an api for influxdb in Github. From that we could fetch and write to database very easily. To use that api download jar. You can also add dependency in your project using JitPack.

In Influxdb we call tables as measurements and columns as fields. So let’s start.

Steps for writing data.

  1. First create configuration.
    Configuration configuration = new Configuration("localhost", "8086", "root", "root", "mydb");

    The parameters above are for host, port, user and password respectively.

  2. Now to write data create Object of DataWriter class.
    DataWriter writer = new DataWriter(configuration);

    We have to pass configuration.

  3. Now we set measurement name ( or you can say table name).
    writer.setMeasurement("sampleMeasurement1");
  4. Set time unit ie what time format do you want to use.
    writer.setTimeUnit(TimeUnit.SECONDS);

    Supported formats in influxdb are HOURS, MINUTES, SECONDS, MILLISECONDS, MICROSECONDS and NANOSECONDS.

  5. Now set time
    writer.setTime(System.currentTimeMillis() / 1000);

    Note : In here we defined time in seconds because in step we set the time in seconds.

  6. Add fields (or you can say columns).
    writer.addField("field1", 12212);		// Integer value
    writer.addField("field2", 22.44);		// Double value
    writer.addField("field3", "thisIsString");	// String value
    writer.addField("field4", false);		// boolean value
    

    In influxdb we don’t need to define table (measurements) or columns (fields) and its datatype. Influxdb will create that automatically as we insert data. We just need to define database.

  7. Add Tags
    writer.addTag("hostname", "server001");
    writer.addTag("disk_type", "SSD");
    
  8. To write the above data into database use
    writer.writeData();

    It will then insert the data into influxdb.

  9. We can use the same writer to add more data into fields.
    writer.addField("field1", 112);
    writer.addField("field1", 21.44);
    writer.addField("field1", "thisIsString1");
    writer.addField("field1", true);
    // Influxdb saves one point at one time. Therefore we have to add another point at another time.
    writer.setTime(System.currentTimeMillis() / 1000 + 1);
    writer.addTag("disk_type", "HDD");
    writer.writeData();

    Note : 1. Make sure put the same type of data into particular column.ie. in step 6. datatype of column1 and column2 are integer and decimal type. Next time whenever we put data in these columns datatype should also be integer and decimal type afterthat.
    2. Influxdb saves one point at one time. To add another point at same time we can use tags otherwise it will override the previous point.

Steps to read data from Influxdb.

    1. Create configuration.
      Configuration configuration = new Configuration("localhost", "8086", "root", "root", "mydb");
    2. Create object of Query class.
      Query query = new Query();
    3. To set the measuremen name.
      query.setMeasurement("sampleMeasurement1");

If we want to set multiple measurements then.

List list = new ArrayList();
list.add("sampleMeasurement1");
list.add("sampleMeasurement2");
query.setMeasurements(list);
  1. To set fields whose values we want to fetch.
    query.addField("field1");
    query.addField("field2");

    It will fetch all fields by default, if not specified as above. (like select * from).

  2. If you want to set time limit like fetch data of last 1 hour.
    query.setDuration("1h");

    or fetch data for last 120 seconds.

    query.setDuration("120s");

    Supported format are d, h, m, s.

    • d = days
    • h = hours
    • m = minutes
    • s = seconds

    If you want to put the data range then use.

    query.setRange(new Date(2012, 12, 31), new Date())
  3. If you want to set aggregate function then
    query.setAggregateFunction(AggregateFunction.MEAN);

    Supported functions in influxdb are COUNT, MIN, MAX, MEAN, MODE, MEDIAN, DISTINCT, SUM, STDDEV, FIRST, LAST, DIFFERENCE and NOFUNCTION.

  4. To set group by time like for 1 minute.
     query.setGroupByTime("1m");
  5. To limit the number of rows.
    query.setLimit(1000);
  6. When we use “group by” in query, then influxdb will return NULL for a time if their is no value found in that range (ie. 1m as defined above). So if you want to replace that NULL value with something like number then use
    query.fillNullValues("0");

    It will replace all null values with 0.

  7. Now create object of DataReader class and fetch the result.
    DataReader dataReader = new DataReader(query, configuration);
    ResultSet resultSet = dataReader.getResult();
    System.out.println(resultSet);

    The resultSet contains the result.