Skip to content

Commit f00cff6

Browse files
committed
docs: start spring example
1 parent 240309c commit f00cff6

File tree

11 files changed

+679
-0
lines changed

11 files changed

+679
-0
lines changed

examples/spring/pom.xml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
6+
<groupId>com.influxdb.v3</groupId>
7+
<artifactId>example-spring</artifactId>
8+
<version>0.1-SNAPSHOT</version>
9+
10+
<name>example-spring</name>
11+
<!-- FIXME change it to the project's website -->
12+
<url>http://www.example.com</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.release>17</maven.compiler.release>
17+
<spring.version>6.2.11</spring.version>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.junit</groupId>
24+
<artifactId>junit-bom</artifactId>
25+
<version>5.11.0</version>
26+
<type>pom</type>
27+
<scope>import</scope>
28+
</dependency>
29+
</dependencies>
30+
</dependencyManagement>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework</groupId>
35+
<artifactId>spring-context</artifactId>
36+
<version>${spring.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-core</artifactId>
41+
<version>${spring.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework</groupId>
45+
<artifactId>spring-aspects</artifactId>
46+
<version>${spring.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.retry</groupId>
50+
<artifactId>spring-retry</artifactId>
51+
<version>2.0.12</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.influxdb</groupId>
55+
<artifactId>influxdb3-java</artifactId>
56+
<version>1.6.0-SNAPSHOT</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.junit.jupiter</groupId>
60+
<artifactId>junit-jupiter-api</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>ch.qos.logback</groupId>
65+
<artifactId>logback-classic</artifactId>
66+
<version>1.5.18</version>
67+
</dependency>
68+
<!-- Optionally: parameterized tests support -->
69+
<dependency>
70+
<groupId>org.junit.jupiter</groupId>
71+
<artifactId>junit-jupiter-params</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.jetbrains</groupId>
76+
<artifactId>annotations</artifactId>
77+
<version>25.0.0</version>
78+
<scope>compile</scope>
79+
</dependency>
80+
</dependencies>
81+
82+
<build>
83+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
84+
<plugins>
85+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
86+
<plugin>
87+
<artifactId>maven-clean-plugin</artifactId>
88+
<version>3.4.0</version>
89+
</plugin>
90+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
91+
<plugin>
92+
<artifactId>maven-resources-plugin</artifactId>
93+
<version>3.3.1</version>
94+
</plugin>
95+
<plugin>
96+
<artifactId>maven-compiler-plugin</artifactId>
97+
<version>3.13.0</version>
98+
</plugin>
99+
<plugin>
100+
<artifactId>maven-surefire-plugin</artifactId>
101+
<version>3.3.0</version>
102+
</plugin>
103+
<plugin>
104+
<artifactId>maven-jar-plugin</artifactId>
105+
<version>3.4.2</version>
106+
</plugin>
107+
<plugin>
108+
<artifactId>maven-install-plugin</artifactId>
109+
<version>3.1.2</version>
110+
</plugin>
111+
<plugin>
112+
<artifactId>maven-deploy-plugin</artifactId>
113+
<version>3.1.2</version>
114+
</plugin>
115+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
116+
<plugin>
117+
<artifactId>maven-site-plugin</artifactId>
118+
<version>3.12.1</version>
119+
</plugin>
120+
<plugin>
121+
<artifactId>maven-project-info-reports-plugin</artifactId>
122+
<version>3.6.1</version>
123+
</plugin>
124+
</plugins>
125+
</pluginManagement>
126+
</build>
127+
</project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.influxdb.v3;
2+
3+
import com.influxdb.v3.client.PointValues;
4+
import com.influxdb.v3.config.AppConfig;
5+
import com.influxdb.v3.reading.EnvReading;
6+
import com.influxdb.v3.sensor.SensorCollection;
7+
import com.influxdb.v3.service.PersistService;
8+
import com.influxdb.v3.service.ReadingsService;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
12+
import org.springframework.stereotype.Component;
13+
14+
import java.io.IOException;
15+
import java.time.Duration;
16+
import java.util.stream.Stream;
17+
18+
@Component
19+
public class Application {
20+
21+
static Logger logger = LoggerFactory.getLogger(Application.class);
22+
23+
public static void main(String[] args) throws InterruptedException, IOException {
24+
try(var ctx = new AnnotationConfigApplicationContext(AppConfig.class)) {
25+
ctx.registerShutdownHook();
26+
SensorCollection sensors = ctx.getBean(SensorCollection.class);
27+
PersistService persistService = ctx.getBean(PersistService.class);
28+
persistService.persistDataRandom(sensors, 1, Duration.ofMinutes(5));
29+
30+
ReadingsService readingsService = ctx.getBean(ReadingsService.class);
31+
logger.info("==== [ Get as Point Values ] ====");
32+
logPVStream(readingsService.getAllReadingsAsPV());
33+
logger.info("==== [ Get as Mapped EnvReadings ] ====");
34+
logEnvReadingStream(readingsService.getAllReadings());
35+
logger.info("==== [ Get as Object Array ] ====");
36+
logObjArrayStream(readingsService.getAllReadingsAsObj());
37+
}
38+
}
39+
40+
public static void logPVStream(Stream<PointValues> pvs) {
41+
StringBuilder pvResults = new StringBuilder();
42+
pvs.forEach(pv -> {
43+
pvResults.append(String.format("%s, ", pv.getTimestamp()));
44+
pvResults.append(String.format("name: %s, ", pv.getTag(("name"))));
45+
pvResults.append(String.format("model: %s, ", pv.getTag(("model"))));
46+
pvResults.append(String.format("id: %s, ", pv.getTag(("id"))));
47+
pvResults.append(String.format("temp: %3.2f ", pv.getFloatField(("temp"))));
48+
pvResults.append(String.format("press: %3.2f ", pv.getFloatField(("press"))));
49+
pvResults.append(String.format("humid: %3.2f ", pv.getFloatField(("humid"))));
50+
pvResults.append("\n");
51+
});
52+
logger.info("PointValueResults: \n {}\n", pvResults);
53+
}
54+
55+
public static void logObjArrayStream(Stream<Object[]> oas) {
56+
StringBuilder results = new StringBuilder();
57+
oas.forEach(o -> {
58+
for(int i = 0; i < o.length;i++) {
59+
results.append(String.format("%s, ", o[i]));
60+
}
61+
results.append("\n");
62+
});
63+
logger.info("ObjectArrayStream results: \n {}\n", results);
64+
}
65+
66+
public static void logEnvReadingStream(Stream<EnvReading> evs) {
67+
StringBuilder result = new StringBuilder();
68+
evs.forEach(point -> {
69+
result.append(String.format("%s\n", point.toString()));
70+
});
71+
logger.info(result.toString());
72+
}
73+
74+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.influxdb.v3.config;
2+
3+
import com.influxdb.v3.sensor.Sensor;
4+
import com.influxdb.v3.sensor.SensorCollection;
5+
import com.influxdb.v3.client.InfluxDBClient;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ComponentScan;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.context.annotation.PropertySource;
14+
import org.springframework.retry.annotation.EnableRetry;
15+
import org.springframework.retry.backoff.FixedBackOffPolicy;
16+
import org.springframework.retry.policy.SimpleRetryPolicy;
17+
import org.springframework.retry.support.RetryTemplate;
18+
19+
import java.util.List;
20+
21+
@Configuration
22+
@EnableRetry
23+
@ComponentScan(basePackages = "com.influxdb.v3")
24+
@PropertySource("classpath:application.properties")
25+
public class AppConfig {
26+
27+
Logger logger = LoggerFactory.getLogger(AppConfig.class);
28+
29+
static List<Sensor> sensors = List.of(new Sensor("Able", "R2D2", "123"),
30+
new Sensor("Baker", "C3PO", "456"),
31+
new Sensor("Charle", "Robbie", "789"),
32+
new Sensor("Delta", "R2D2", "abc"),
33+
new Sensor( "Easy", "C3PO", "def"
34+
)
35+
);
36+
37+
@Value("${influxdb.url}")
38+
private String influxDBUrl;
39+
40+
@Value("${influxdb.token}")
41+
private String influxDBToken;
42+
43+
@Value("${influxdb.database}")
44+
private String influxDBDatabase;
45+
46+
@Bean(name={"internalSensors"})
47+
public SensorCollection sensorCollectionInit(){
48+
logger.debug("sensorCollection");
49+
return new SensorCollection(sensors);
50+
}
51+
52+
/* @Bean
53+
@Qualifier("customClient")
54+
public CustomInfluxClient customInfluxClientInit(){
55+
logger.debug("customInfluxClient with " + influxDBUrl);
56+
return new CustomInfluxClient(
57+
InfluxDBClient.getInstance(influxDBUrl, influxDBToken.toCharArray(), influxDBDatabase)
58+
);
59+
} */
60+
61+
@Bean
62+
@Qualifier("baseInfluxClient")
63+
public InfluxDBClient influxDBClientBaseInit(){
64+
logger.debug("influxDBClientBaseInit with " + influxDBUrl);
65+
return InfluxDBClient.getInstance(influxDBUrl, influxDBToken.toCharArray(), influxDBDatabase);
66+
}
67+
68+
@Bean
69+
public RetryTemplate retryTemplate(){
70+
RetryTemplate retryTemplate = new RetryTemplate();
71+
72+
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
73+
fixedBackOffPolicy.setBackOffPeriod(2000l);
74+
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
75+
76+
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
77+
retryPolicy.setMaxAttempts(2);
78+
retryTemplate.setRetryPolicy(retryPolicy);
79+
80+
return retryTemplate;
81+
}
82+
83+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.influxdb.v3.reading;
2+
3+
import com.influxdb.v3.sensor.Sensor;
4+
import com.influxdb.v3.client.Point;
5+
6+
import java.time.Instant;
7+
8+
public class EnvReading {
9+
10+
Sensor sensor;
11+
double temperature;
12+
double humidity;
13+
double pressure;
14+
Instant timestamp;
15+
16+
public EnvReading(Sensor sensor, double temperature, double humidity, double pressure) {
17+
this.sensor = sensor;
18+
this.temperature = temperature;
19+
this.humidity = humidity;
20+
this.pressure = pressure;
21+
}
22+
23+
public EnvReading(Sensor sensor, double temperature, double humidity, double pressure, Instant timestamp) {
24+
this.sensor = sensor;
25+
this.temperature = temperature;
26+
this.humidity = humidity;
27+
this.pressure = pressure;
28+
this.timestamp = timestamp;
29+
}
30+
31+
32+
public Sensor getSensor() {
33+
return this.sensor;
34+
}
35+
36+
public void setSensor(Sensor sensor) {
37+
this.sensor = sensor;
38+
}
39+
40+
public double getTemperature() {
41+
return this.temperature;
42+
}
43+
44+
public void setTemperature(double temperature) {
45+
this.temperature = temperature;
46+
}
47+
48+
public double getHumidity() {
49+
return this.humidity;
50+
}
51+
52+
public void setHumidity(double humidity) {
53+
this.humidity = humidity;
54+
}
55+
public double getPressure() {
56+
return this.pressure;
57+
}
58+
public void setPressure(double pressure) {
59+
this.pressure = pressure;
60+
}
61+
62+
public Point toPoint(String measurement, Instant timestamp) {
63+
if (this.timestamp == null) {
64+
this.timestamp = timestamp;
65+
}
66+
return new Point(measurement)
67+
.setTags(this.sensor.getTags())
68+
.setFloatField("temp", this.temperature)
69+
.setFloatField("humid", this.humidity)
70+
.setFloatField("press", this.pressure)
71+
.setTimestamp(timestamp);
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return String.format("sensor[%s] temp: %f3.3, humid: %f3.3, press: %f3.3, time: %s",
77+
sensor, temperature, humidity, pressure, timestamp);
78+
}
79+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.influxdb.v3.reading;
2+
3+
import com.influxdb.v3.sensor.Sensor;
4+
5+
public class RandomEnvReading {
6+
7+
public static EnvReading genReading(Sensor sensor){
8+
return new EnvReading(sensor,
9+
(Math.random() * 40.0) + (Math.random() * 40.0) - 20.0,
10+
(Math.random() * 60) + (Math.random() * 40),
11+
Math.random() * 8.0 + 26.0
12+
);
13+
}
14+
}

0 commit comments

Comments
 (0)