InfluxDB is a time series database designed to handle high write and query loads. It is an essential time series toolkit — dashboards, queries, tasks and agents all in one place. It comes with it’s own set of plugins to create dashoards.
InfluxDB empowers developers to build IoT, analytics and monitoring software. It is purpose-built to handle the massive volumes and countless sources of time-stamped data produced by sensors, applications and infrastructure.
1. Set up InfluxDB with Docker
The easiest way to get started with InfluxDB is with docker. If you’re working on Windows, you can install Docker Desktop. If you’re on Linux, you can install the Docker Engine.
Run the below command to pull the latest Docker image of InfluxDB.
docker pull influxdb
Once you have pulled the image, run the below command to start a container.
docker run -p 8086:8086 -v myInfluxVolume:/var/lib/influxdb2 influxdb:latest
You can then run the below command to check if the process is running.
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7106352d956d influxdb:latest "/entrypoint.sh infl…" 45 minutes ago Up 45 minutes 0.0.0.0:8086->8086/tcp eloquent_shaw
Open http://localhost:8086 on your browser and you should be able see the InfluxDB portal.
2. Create a bucket & generate API token
Once the process is up and running, you can create a bucket where you can display the data.
Once bucket is created, you will need to generate an API token which you can use to connect to InfluxDB. You have the option of creating an administrative token or a custom API token. An administrative token gives you all the privileges whereas a custom token only gives you specific privileges.
3. Java code to write data into InfluxDB
Before you write the code, make sure to have the below library added to your build. If you’re using Maven, you can use the below dependency.
<dependency> <groupId>com.influxdb</groupId> <artifactId>influxdb-client-java</artifactId> <version>6.6.0</version> </dependency>
Below is the code you can use to insert sample date.
import java.time.Instant; import com.influxdb.client.InfluxDBClient; import com.influxdb.client.InfluxDBClientFactory; import com.influxdb.client.WriteApiBlocking; import com.influxdb.client.domain.WritePrecision; import com.influxdb.client.write.Point; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class App { @SuppressWarnings("unused") private static final Logger LOGGER = LoggerFactory.getLogger(App.class); public static void main(String[] args) { String token = "<<INFLUX_TOKEN>>"; String bucket = "Test"; String org = "Test"; InfluxDBClient client = InfluxDBClientFactory.create("http://localhost:8086", token.toCharArray()); try { for (int i = 0; i < 10; i++) { Point point = Point.measurement("mem") .addTag("host", "host" + i) .addField("used_percent", Math.random()).time(Instant.now(), WritePrecision.NS); WriteApiBlocking writeApi = client.getWriteApiBlocking(); writeApi.writePoint(bucket, org, point); } } catch (Exception e) { LOGGER.debug("Error occurred -----> " + e.getMessage()); } finally { client.close(); } } }
4. View the data
Once you have run the code, you can open the bucket you have created and filter the data.
Once you have verified the data, you can tweak the code as per your requirement. You can also check out some of the other languages it supports using which you can insert data.