Analysis of reasons for failure to add new data in influxdb

I installed an influxdb time series database locally a few days ago, but adding new data through java has been failing. The strange thing is that the measurement and tag can be added successfully, but the field has always been worthless.

At first I thought it was user permissions, but it turned out it wasn't.

Final reason:influxdb只能往默认的保留策略里面存数据

First check the default retention policy of your local influxdb database

show retention policies on 数据库名

Insert image description here
This is thirty_two_day. Therefore, data must be stored here to be successful, so we need to set the retention policy in java.
Insert image description here
If the retention policy is not set, the influxdbImpl class will default to autogen, so if we are not using autogena retention policy, we need to configure it ourselves.

Insert image description here

Your problem should be solved here! The following demonstrates the process of using influxdb1.x in java.

1. Import maven

First, ensure that influxdb version 1.x is installed locally, and users, databases, and retention policies are created.

<!-- influxdb ( influxdb-java 适用于 1.x版本 )-->
        <dependency>
            <groupId>org.influxdb</groupId>
            <artifactId>influxdb-java</artifactId>
        </dependency>
2. Configuration class
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Filename: InfluxDBConfig InfluxDB配置类
 * @Author: sheng.wanping
 * <li>Date: 2023/7/6 17:13
 * <li>Version: 1.0
 * <li>Content: create
 */
@Configuration
public class InfluxDBConfig {
    
    

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

    @Value("${influxdb.username}")
    private String influxDbUsername;

    @Value("${influxdb.password}")
    private String influxDbPassword;

    @Bean
    public InfluxDB influxDB() {
    
    
        // 需提前建数据库和保留策略
        // create database big_screen;create retention policy thirty_two_day on big_screen duration 32d replication 1 default;
        InfluxDB influxDB = InfluxDBFactory.connect(influxDbUrl, influxDbUsername, influxDbPassword);
        influxDB.setDatabase("big_screen");
        // 不设置默认使用 autogen
        influxDB.setRetentionPolicy("thirty_two_day");
        return influxDB;
    }
}
3. Test
	@Autowired
    private InfluxDB influxDB;

	Point point = Point.measurement("big_screen_device")
	       .tag("device_id", deviceId)
	       .addField("device_status", deviceStatus)
	       .build();
	influxDB.write(point);

Supplement:
1. If the default saving policy is deleted in influxdb, an error will be reported when querying data. After deletion, the system will not select another retention policy as the default. You need to add a default retention policy yourself, and the original data will be deleted.
2. After influxdb modifies 默认the retention policy, all data in the database will be converted to the current default retention policy.

Guess you like

Origin blog.csdn.net/weixin_44183847/article/details/131770865