Skip to content

Commit 0d22a64

Browse files
egoebelbeckermaibin
authored andcommitted
BAEL-1420 - introduction to InfluxDB with Java (eugenp#3276)
* BAEL-1420 - introduction to InfluxDB with Java * BAEL-1420 - removed unused catches. Shorten batch timer. * BAEL-1420 - add notes to readme.
1 parent 4f0ae5d commit 0d22a64

6 files changed

Lines changed: 282 additions & 0 deletions

File tree

‎influxdb/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Influx SDK Tutorial Project
2+
3+
### Relevant Article:
4+
- [Introduction to using InfluxDB with Java](http://www.baeldung.com/using-influxdb-with-java/)
5+
6+
### Overview
7+
This Maven project contains the Java code for the article linked above.
8+
9+
### Package Organization
10+
Java classes for the intro tutorial are in the
11+
org.baeldung.influxdb package.
12+
13+
14+
### Running the tests
15+
The test class expects an InfluxDB server to be available on localhost, at the default port of 8086 and with the default "admin" credentials.
16+
17+
```

‎influxdb/pom.xml‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>influxdb</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>influxdb</name>
9+
<description>InfluxDB SDK Tutorial</description>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.influxdb</groupId>
20+
<artifactId>influxdb-java</artifactId>
21+
<version>${influxdb.sdk.version}</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.projectlombok</groupId>
26+
<artifactId>lombok</artifactId>
27+
<!-- Check for the most recent available version: https://projectlombok.org/changelog.html -->
28+
<version>${lombok.version}</version>
29+
<scope>provided</scope>
30+
</dependency>
31+
32+
33+
34+
</dependencies>
35+
36+
<properties>
37+
<java.version>1.8</java.version>
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
<influxdb.sdk.version>2.8</influxdb.sdk.version>
40+
<lombok.version>1.16.18</lombok.version>
41+
42+
</properties>
43+
44+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.influxdb;
2+
3+
import lombok.Data;
4+
import org.influxdb.annotation.Column;
5+
import org.influxdb.annotation.Measurement;
6+
7+
import java.time.Instant;
8+
9+
@Data
10+
@Measurement(name = "memory")
11+
public class MemoryPoint {
12+
13+
@Column(name = "time")
14+
private Instant time;
15+
16+
@Column(name = "name")
17+
private String name;
18+
19+
@Column(name = "free")
20+
private Long free;
21+
22+
@Column(name = "used")
23+
private Long used;
24+
25+
@Column(name = "buffer")
26+
private Long buffer;
27+
28+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package com.baeldung.influxdb;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.influxdb.InfluxDB;
5+
import org.influxdb.InfluxDBFactory;
6+
import org.influxdb.InfluxDBIOException;
7+
import org.influxdb.dto.*;
8+
import org.influxdb.impl.InfluxDBResultMapper;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
import java.util.List;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import static junit.framework.TestCase.assertEquals;
16+
import static org.junit.Assert.assertFalse;
17+
import static org.junit.Assert.assertTrue;
18+
19+
@Slf4j
20+
public class InfluxDBConnectionLiveTest {
21+
22+
@Test
23+
public void whenCorrectInfoDatabaseConnects() {
24+
25+
InfluxDB connection = connectDatabase();
26+
assertTrue(pingServer(connection));
27+
}
28+
29+
private InfluxDB connectDatabase() {
30+
31+
// Connect to database assumed on localhost with default credentials.
32+
return InfluxDBFactory.connect("http://127.0.0.1:8086", "admin", "admin");
33+
34+
}
35+
36+
private boolean pingServer(InfluxDB influxDB) {
37+
try {
38+
// Ping and check for version string
39+
Pong response = influxDB.ping();
40+
if (response.getVersion().equalsIgnoreCase("unknown")) {
41+
log.error("Error pinging server.");
42+
return false;
43+
} else {
44+
log.info("Database version: {}", response.getVersion());
45+
return true;
46+
}
47+
} catch (InfluxDBIOException idbo) {
48+
log.error("Exception while pinging database: ", idbo);
49+
return false;
50+
}
51+
}
52+
53+
@Test
54+
public void whenDatabaseCreatedDatabaseChecksOk() {
55+
56+
InfluxDB connection = connectDatabase();
57+
58+
// Create "baeldung and check for it
59+
connection.createDatabase("baeldung");
60+
assertTrue(connection.databaseExists("baeldung"));
61+
62+
// Verify that nonsense databases are not there
63+
assertFalse(connection.databaseExists("foobar"));
64+
65+
// Drop "baeldung" and check again
66+
connection.deleteDatabase("baeldung");
67+
assertFalse(connection.databaseExists("baeldung"));
68+
}
69+
70+
@Test
71+
public void whenPointsWrittenPointsExists() throws Exception {
72+
73+
InfluxDB connection = connectDatabase();
74+
75+
String dbName = "baeldung";
76+
connection.createDatabase(dbName);
77+
78+
// Need a retention policy before we can proceed
79+
connection.createRetentionPolicy("defaultPolicy", "baeldung", "30d", 1, true);
80+
81+
// Since we are doing a batch thread, we need to set this as a default
82+
connection.setRetentionPolicy("defaultPolicy");
83+
84+
// Enable batch mode
85+
connection.enableBatch(10, 10, TimeUnit.MILLISECONDS);
86+
87+
for (int i = 0; i < 10; i++) {
88+
Point point = Point.measurement("memory")
89+
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
90+
.addField("name", "server1")
91+
.addField("free", 4743656L)
92+
.addField("used", 1015096L)
93+
.addField("buffer", 1010467L)
94+
.build();
95+
96+
connection.write(dbName, "defaultPolicy", point);
97+
Thread.sleep(2);
98+
99+
}
100+
101+
// Unfortunately, the sleep inside the loop doesn't always add enough time to insure
102+
// that Influx's batch thread flushes all of the writes and this sometimes fails without
103+
// another brief pause.
104+
Thread.sleep(10);
105+
106+
List<MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung");
107+
108+
assertEquals(10, memoryPointList.size());
109+
110+
// Turn off batch and clean up
111+
connection.disableBatch();
112+
connection.deleteDatabase("baeldung");
113+
connection.close();
114+
115+
}
116+
117+
private List<MemoryPoint> getPoints(InfluxDB connection, String query, String databaseName) {
118+
119+
// Run the query
120+
Query queryObject = new Query(query, databaseName);
121+
QueryResult queryResult = connection.query(queryObject);
122+
123+
// Map it
124+
InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
125+
return resultMapper.toPOJO(queryResult, MemoryPoint.class);
126+
}
127+
128+
129+
130+
@Test
131+
public void whenBatchWrittenBatchExists() {
132+
133+
InfluxDB connection = connectDatabase();
134+
135+
String dbName = "baeldung";
136+
connection.createDatabase(dbName);
137+
138+
// Need a retention policy before we can proceed
139+
// Since we are doing batches, we need not set it
140+
connection.createRetentionPolicy("defaultPolicy", "baeldung", "30d", 1, true);
141+
142+
143+
BatchPoints batchPoints = BatchPoints
144+
.database(dbName)
145+
.retentionPolicy("defaultPolicy")
146+
.build();
147+
Point point1 = Point.measurement("memory")
148+
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
149+
.addField("free", 4743656L)
150+
.addField("used", 1015096L)
151+
.addField("buffer", 1010467L)
152+
.build();
153+
Point point2 = Point.measurement("memory")
154+
.time(System.currentTimeMillis() - 100, TimeUnit.MILLISECONDS)
155+
.addField("free", 4743696L)
156+
.addField("used", 1016096L)
157+
.addField("buffer", 1008467L)
158+
.build();
159+
batchPoints.point(point1);
160+
batchPoints.point(point2);
161+
connection.write(batchPoints);
162+
163+
List<MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung");
164+
165+
assertEquals(2, memoryPointList.size());
166+
assertTrue(4743696L == memoryPointList.get(0).getFree());
167+
168+
169+
memoryPointList = getPoints(connection, "Select * from memory order by time desc", "baeldung");
170+
171+
assertEquals(2, memoryPointList.size());
172+
assertTrue(4743656L == memoryPointList.get(0).getFree());
173+
174+
// Clean up database
175+
connection.deleteDatabase("baeldung");
176+
connection.close();
177+
}
178+
179+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="INFO">
11+
<appender-ref ref="STDOUT" />
12+
</root>
13+
</configuration>

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080

8181
<module>image-processing</module>
8282
<module>immutables</module>
83+
<module>influxdb</module>
8384

8485
<module>jackson</module>
8586
<!-- <module>persistence-modules/java-cassandra</module> -->

0 commit comments

Comments
 (0)