/spring-data-influxdb

Spring Data InfluxDB

Primary LanguageJavaApache License 2.0Apache-2.0

Spring Data InfluxDB

The primary goal of the Spring Data project is to make it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services.

This modules provides integration with the InfluxDB database and wraps the capabilities of the official influxdb-java library.

Artifacts

Maven

<dependency>
  <groupId>com.github.miwurster</groupId>
  <artifactId>spring-data-influxdb</artifactId>
  <version>1.5</version>
</dependency>
<dependency>
  <groupId>org.influxdb</groupId>
  <artifactId>influxdb-java</artifactId>
  <version>2.5</version>
</dependency>

Usage (Spring Boot)

  • Following properties can be used in your application.yml:

    spring:
      influxdb:
        url: http://localhost:8086
        username: user
        password: ~
        database: test
        retention-policy: autogen
        create-database: false
  • Create InfluxDBConnectionFactory and InfluxDBTemplate beans:

    @Configuration
    @EnableConfigurationProperties(InfluxDBProperties.class)
    public class InfluxDBConfiguration
    {
      @Bean
      public InfluxDBConnectionFactory connectionFactory(final InfluxDBProperties properties)
      {
        return new InfluxDBConnectionFactory(properties);
      }
    
      @Bean
      public InfluxDBTemplate<Point> influxDBTemplate(final InfluxDBConnectionFactory connectionFactory)
      {
        /*
         * You can use your own 'PointCollectionConverter' implementation, e.g. in case
         * you want to use your own custom measurement object.
         */
        return new InfluxDBTemplate<>(connectionFactory, new PointConverter());
      }
      
      @Bean
      public DefaultInfluxDBTemplate defaultTemplate(final InfluxDBConnectionFactory connectionFactory)
      {
        /*
         * If you are just dealing with Point objects from 'influxdb-java' you could
         * also use an instance of class DefaultInfluxDBTemplate.
         */
        return new DefaultInfluxDBTemplate(connectionFactory);
      }
    }
  • Use InfluxDBTemplate to interact with the InfluxDB database:

    @Autowired
    private InfluxDBTemplate<Point> influxDBTemplate;
    
    influxDBTemplate.createDatabase();
    final Point p = Point.measurement("disk")
      .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
      .tag("tenant", "default")
      .addField("used", 80L)
      .addField("free", 1L)
      .build();
    influxDBTemplate.write(p);

Building

Spring Data InfluxDB uses Maven as its build system.

mvn clean install

Additional Hints

Since InfluxDB 1.0.0 the create-database flag must be set to false due to the removeing of the "IF NOT EXISTS" syntax in queries. See jmxtrans/jmxtrans#489 for example.