Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions examples/spring/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<!--

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.influxdb.v3.spring</groupId>
<artifactId>spring-example</artifactId>
<version>1.0-SNAPSHOT</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add same version as is in the client

<name>spring-example</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
<spring.version>6.2.11</spring.version>
<exec.main>org.influxdb.v3.Application</exec.main>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb3-java</artifactId>
<version>1.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.20</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${exec.main}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<failsOnError>true</failsOnError>
<configLocation>../../checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
<linkXRef>false</linkXRef>
<sourceDirectories>
<sourceDirectory>src/main/java</sourceDirectory>
</sourceDirectories>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>checkstyle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
109 changes: 109 additions & 0 deletions examples/spring/src/main/java/org/influxdb/v3/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.influxdb.v3;

import java.time.Duration;
import java.util.stream.Stream;
import javax.annotation.Nonnull;

import org.influxdb.v3.config.AppConfig;
import org.influxdb.v3.reading.EnvReading;
import org.influxdb.v3.sensor.SensorCollection;
import org.influxdb.v3.service.PersistService;
import org.influxdb.v3.service.ReadingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import com.influxdb.v3.client.PointValues;

@Component
public final class Application {

private Application() { }

static Logger logger = LoggerFactory.getLogger(Application.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lombok annotation for loggers:

@log


public static void main(final String[] args) {
try (var ctx = new AnnotationConfigApplicationContext(AppConfig.class)) {
ctx.registerShutdownHook();

SensorCollection sensors = ctx.getBean(SensorCollection.class);
PersistService persistService = ctx.getBean(PersistService.class);
persistService.persistDataRandom(sensors, 1, Duration.ofMinutes(5));

ReadingsService readingsService = ctx.getBean(ReadingsService.class);
logger.info("==== [ Get as Point Values ] ====");
/*
Be sure to use streams in try-with-resources blocks, failure to do so
may not close the underlying Arrow FlightStream properly, and
a memory leak can result.
*/
try (Stream<PointValues> stream = readingsService.getAllReadingsAsPV()) {
logPVStream(stream);
}
logger.info("==== [ Get as Mapped EnvReadings ] ====");
try (Stream<EnvReading> stream = readingsService.getAllReadings()) {
logEnvReadingStream(stream);
}
logger.info("==== [ Get as Object Array ] ====");
try (Stream<Object[]> stream = readingsService.getAllReadingsAsObj()) {
logObjArrayStream(stream);
}
}
}

public static void logPVStream(final @Nonnull Stream<PointValues> pvs) {
StringBuilder pvResults = new StringBuilder();
pvs.forEach(pv -> {
pvResults.append(String.format("%s, ", pv.getTimestamp()));
pvResults.append(String.format("name: %s, ", pv.getTag(("name"))));
pvResults.append(String.format("model: %s, ", pv.getTag(("model"))));
pvResults.append(String.format("id: %s, ", pv.getTag(("id"))));
pvResults.append(String.format("temp: %3.2f ", pv.getFloatField(("temp"))));
pvResults.append(String.format("press: %3.2f ", pv.getFloatField(("press"))));
pvResults.append(String.format("humid: %3.2f ", pv.getFloatField(("humid"))));
pvResults.append("\n");
});
logger.info("PointValueResults:\n{}\n", pvResults);
}

public static void logObjArrayStream(final @Nonnull Stream<Object[]> oas) {
StringBuilder results = new StringBuilder();
oas.forEach(o -> {
for (int i = 0; i < o.length; i++) {
results.append(String.format("%s, ", o[i]));
}
results.append("\n");
});
logger.info("ObjectArrayStream results:\n{}\n", results);
}

public static void logEnvReadingStream(final @Nonnull Stream<EnvReading> evs) {
StringBuilder result = new StringBuilder();
evs.forEach(point -> {
result.append(String.format("%s\n", point.toString()));
});
logger.info(result.toString());
}
}
101 changes: 101 additions & 0 deletions examples/spring/src/main/java/org/influxdb/v3/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.influxdb.v3.config;

import java.net.ConnectException;
import java.util.List;

import org.apache.arrow.flight.FlightRuntimeException;
import org.influxdb.v3.sensor.Sensor;
import org.influxdb.v3.sensor.SensorCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.support.RetryTemplate;

import com.influxdb.v3.client.InfluxDBApiException;
import com.influxdb.v3.client.InfluxDBClient;

@Configuration
@EnableRetry
@ComponentScan(basePackages = "org.influxdb.v3")
@PropertySource("classpath:application.properties")
public class AppConfig {

Logger logger = LoggerFactory.getLogger(AppConfig.class);

static List<Sensor> sensors = List.of(new Sensor("Able", "R2D2", "123"),
new Sensor("Baker", "C3PO", "456"),
new Sensor("Charlie", "Robbie", "789"),
new Sensor("Delta", "R2D2", "abc"),
new Sensor("Easy", "C3PO", "def")
);

@Value("${influxdb.url}")
private String influxDBUrl;

@Value("${influxdb.token}")
private String influxDBToken;

@Value("${influxdb.database}")
private String influxDBDatabase;

@Bean(name = {"internalSensors"})
public SensorCollection sensorCollectionInit() {
logger.debug("sensorCollection");
return new SensorCollection(sensors);
}

@Bean
public InfluxDBClient influxDBClient() {
logger.debug("influxDBClientBaseInit with " + influxDBUrl);
return InfluxDBClient.getInstance(influxDBUrl, influxDBToken.toCharArray(), influxDBDatabase);
}

@Bean
@Qualifier("writesTemplate")
public RetryTemplate retryTemplateWrites() {
return RetryTemplate.builder()
.maxAttempts(5)
.exponentialBackoff(100, 2, 10000)
.retryOn(List.of(InfluxDBApiException.class, ConnectException.class))
.traversingCauses()
.build();
}

@Bean
@Qualifier("readsTemplate")
public RetryTemplate retryTemplateReads() {
return RetryTemplate.builder()
.maxAttempts(3)
.exponentialBackoff(100, 2, 10000)
.retryOn(List.of(InfluxDBApiException.class, FlightRuntimeException.class, ConnectException.class))
.traversingCauses()
.build();
}
}
Loading
Loading