Database · Java

How to fetch and write influxdb data using java

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

19 thoughts on “How to fetch and write influxdb data using java

  1. writeData() is throwing an Exception:
    Exception in thread “main” java.lang.Exception: Unable to write data. HTTP/1.1 404 Not Found

    Could you please help? Thanks!

  2. 1. Check whether influxdb is installed in your system by typing command
    > influx

    2. Check if your configurations are correct or not (ie. host, port, username, password and database). If you haven’t created a user then create it by using command

    CREATE USER “username” WITH PASSWORD ‘password’ WITH ALL PRIVILEGES

    3. The database name that you are providing should also exist. If not, you can create the database using command

    CREATE DATABASE “database_name”

  3. setTables() contains error, as following code will add “table1, table2table2” into the query statement. While actually it should be “table1, table2”.
    List tablesList = new ArrayList();
    tablesList.add(“table1”);
    tablesList.add(“tables2”);
    query.setTables(tablesList);

  4. Hi,

    I’m trying to read from InfluxDB. There is one issue.

    setTableName is deprecated and therefore I can’t compile.

    Can you fix this? Otherwise which alternative do we have? maybe another library accomplishing the same? It is a pity though, this library seems very easy to understand.

    Looking forward to hearing from you,

    Jaume

  5. When I add a new measurement through code using the following snippet the new measurement is not getting created in the influxdb whereas the data gets added normally for old measurements. Can you suggest me a solution?
    writer.setMeasurement(“measurement_name”);
    writer.addField(“field_name”);
    writer.addTag(“tag_name”, “tag_value”);
    writer.setTime(currentTime);
    writer.writeData();

  6. Hi i am new to influx db can u please tell how to connect influx db via nodejs .
    how to give query to insert and select data in influx db via nodejs

  7. I tried with the examples
    but i got a error while fetching or quering a data from db
    Error: error parsing query: found BADSTRING, expected identifier at line 1, char 14 at Object.get

  8. Hi,

    is it possible to add the “order by” as it exists?
    Influxdb documentation : ORDER BY time DESC.

    Many thanks for the job.

  9. Sorry Nikomer, I didn’t provided option to set order of time, but I have fixed it in the new release (2.5.2) and added option to set order of time in Query class using method (setOrderOfTime). Thanks for suggestion.

Leave a Reply

Your email address will not be published. Required fields are marked *