Connecting an InfluxDB Database to a Spring Boot Application
Step 1: Setting Up InfluxDB with Docker
To run InfluxDB in a Docker container, you can use the following Dockerfile:
FROM influxdb:latest
EXPOSE 8086
This Dockerfile uses the official InfluxDB image and exposes the default InfluxDB port (8086) to allow connection to the database.
To build and run the Docker container, use the following commands:
\# Build the Docker image
docker build -t influxdb .
\# Run the Docker container
docker run -p 8086:8086 - name influxdb influxdb
These commands will build the Docker image and run a container named “influxdb” with the InfluxDB service running on port 8086.
Step 2: Adding Dependencies to the Spring Boot Project
To include the necessary libraries for connecting to InfluxDB in your Spring Boot project, add the following dependency to your pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-influx</artifactId>
</dependency>
Step 3: Configuring the Connection to InfluxDB
In your Spring Boot application, you will need to configure the connection to your InfluxDB instance. You can do this by adding the following configuration to your application.properties file:
spring.influx.url=http://localhost:8086
spring.influx.username=<YOUR_USERNAME>
spring.influx.password=<YOUR_PASSWORD>
spring.influx.database=<YOUR_DATABASE_NAME>
Step 4: Using the InfluxDBTemplate Class
Now, you can use the InfluxDBTemplate class to connect to your InfluxDB instance and perform various operations, such as writing data to the database or querying for data. Here is an example of how you can use the InfluxDBTemplate class to write data to your database:
@Autowired
private InfluxDBTemplate<Point> influxDBTemplate;
// ...
Point point \= Point.measurement("temperature")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.addField("value", 22.0)
.build();
influxDBTemplate.write(point);
This code creates a new point measurement with the name “temperature” and a field called “value” with a value of 22.0. It then writes this point to the database using the write method of the InfluxDBTemplate class.
You can also use the InfluxDBTemplate class to execute queries and retrieve data from your database.
Step 5: Querying Data from InfluxDB
To query data from the InfluxDB database, you can use the query method of the InfluxDBTemplate class. Here is an example of how you can use the query method to retrieve data from the database:
String query \= "SELECT \* FROM temperature WHERE time > now() - 1h";
QueryResult queryResult \= influxDBTemplate.query(query, TimeUnit.MILLISECONDS);
List<Result> results = queryResult.getResults();
for (Result result : results) {
List<Series> series = result.getSeries();
for (Series s : series) {
List<List<Object>> values = s.getValues();
for (List<Object> value : values) {
System.out.println(value);
}
}
}
This code executes a SELECT query on the “temperature” measurement, retrieving all data from the past hour. The queryResult object contains the results of the query, which can be accessed and processed as shown in the example.
Conclusion
In this tutorial, you learned how to connect an InfluxDB database to a Spring Boot application using the InfluxDBTemplate class. You saw how to write data to the database and how to execute queries to retrieve data. I hope this tutorial was helpful and that you are now able to connect your own Spring Boot application to an InfluxDB database.