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.
- First create configuration.
Configuration configuration = new Configuration("localhost", "8086", "root", "root", "mydb");
The parameters above are for host, port, user and password respectively.
- Now to write data create Object of
DataWriter
class.
DataWriter writer = new DataWriter(configuration);
We have to pass configuration.
- Now we set measurement name ( or you can say table name).
writer.setMeasurement("sampleMeasurement1");
- 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
.
- 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.
- 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.
- Add Tags
writer.addTag("hostname", "server001");
writer.addTag("disk_type", "SSD");
- To write the above data into database use
writer.writeData();
It will then insert the data into influxdb.
- 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.
- Create configuration.
Configuration configuration = new Configuration("localhost", "8086", "root", "root", "mydb");
- Create object of
Query
class.
Query query = new Query();
- To set the measuremen name.
query.setMeasurement("sampleMeasurement1");
If we want to set multiple measurements then.
List<String> list = new ArrayList<String>();
list.add("sampleMeasurement1");
list.add("sampleMeasurement2");
query.setMeasurements(list);
- 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).
- 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())
- 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
.
- To set group by time like for 1 minute.
query.setGroupByTime("1m");
- To limit the number of rows.
query.setLimit(1000);
- 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.
- 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.