Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About the failure of the query method-----query(@Nonnull String var1, @Nonnull Class<M> var2) #598

Open
ZY945 opened this issue Jul 4, 2023 · 7 comments

Comments

@ZY945
Copy link

ZY945 commented Jul 4, 2023

The issue:
There is a problem with the data when using this method-----client.getQueryApi().query(statFlux.toString(), Stat.class);
However, when using this method, the data is obtained normally-----client.getQueryApi().query(statFlux.toString());
This indicates that the database is working correctly, but there may be problems converting the data.
The following is the relevant information.
The dependency:

        <spring-boot.version>3.1.0</spring-boot.version>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <dependency>
            <groupId>com.influxdb</groupId>
            <artifactId>influxdb-client-java</artifactId>
            <version>6.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.influxdb</groupId>
            <artifactId>flux-dsl</artifactId>
            <version>6.9.0</version>
        </dependency>

The model:

import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Measurement(name = "Stat")//influxDB注解
public class Stat {
    @Column
    private String intr;
    @Column
    private long ctxt;
    @Column
    private long btime;
    @Column
    private long processes;
    @Column
    private long procs_running;
    @Column
    private String procs_blocked;
    @Column
    private long softirq;
    @Column(timestamp = true)
    Instant time;
}

The code:

import com.cabin.empty.influxDB.Stat;
import com.cabin.influxDB.util.InfluxDBTemplate;
import com.cabin.utils.dateUtil.DateUtil;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.query.FluxTable;
import com.influxdb.query.dsl.Flux;
import com.influxdb.query.dsl.functions.restriction.Restrictions;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;
import java.time.Instant;
import java.util.List;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class InfluxDBTest {
    @Resource(name = "influxByToken")
    private InfluxDBClient client;
    @Resource(name = "InfluxDBTemplate")
    private InfluxDBTemplate template;

    @Test
    void testAPI() {
        String bucket = "bucket";
        String measurement = "Stat";
        Instant stop = DateUtil.getNowInstant();
        Instant start = stop.minus(Duration.ofSeconds(1));

        Flux statFlux = Flux.from(bucket)
                .range(start, stop)
                .filter(Restrictions.and(
                        Restrictions.measurement().equal(measurement)));

        List<Stat> queryList = client.getQueryApi().query(statFlux.toString(), Stat.class);
        queryList.forEach(l -> {
            System.out.println(l.toString());
        });

        List<FluxTable> query = client.getQueryApi().query(statFlux.toString());
        query.forEach(l -> {
            l.getRecords()
                    .forEach(r -> System.out.println(r.getTime() + ": " + r.getValueByKey("_value")));
        });
    }

}

The result:

Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
Stat(intr=null, ctxt=0, btime=0, processes=0, procs_running=0, procs_blocked=null, softirq=0, time=2023-07-04T06:36:23.005Z)
Stat(intr=null, ctxt=0, btime=0, processes=0, procs_running=0, procs_blocked=null, softirq=0, time=2023-07-04T06:36:23.005Z)
Stat(intr=null, ctxt=0, btime=0, processes=0, procs_running=0, procs_blocked=null, softirq=0, time=2023-07-04T06:36:23.005Z)
Stat(intr=null, ctxt=0, btime=0, processes=0, procs_running=0, procs_blocked=null, softirq=0, time=2023-07-04T06:36:23.005Z)
Stat(intr=null, ctxt=0, btime=0, processes=0, procs_running=0, procs_blocked=null, softirq=0, time=2023-07-04T06:36:23.005Z)
Stat(intr=null, ctxt=0, btime=0, processes=0, procs_running=0, procs_blocked=null, softirq=0, time=2023-07-04T06:36:23.005Z)
2023-07-04T06:36:23.005Z: 1685014131
2023-07-04T06:36:23.005Z: 15002073429
2023-07-04T06:36:23.005Z: 6140525
2023-07-04T06:36:23.005Z: 0
2023-07-04T06:36:23.005Z: 7
2023-07-04T06:36:23.005Z: 0

The information:
image

@ZY945
Copy link
Author

ZY945 commented Jul 4, 2023

I thought that the databases were all of type String, so I modified the accepted type, but it still didn't seem to be available

package com.cabin.empty.influxDB;

import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Measurement(name = "Stat")
public class Stat {
    @Column
    private String intr;
    @Column
    private String ctxt;
    @Column
    private String btime;
    @Column
    private String processes;
    @Column
    private String procs_running;
    @Column
    private String procs_blocked;
    @Column
    private String softirq;
    @Column(timestamp = true)
    Instant time;

}

image

@ZY945
Copy link
Author

ZY945 commented Jul 4, 2023

This the version of influxDB.
image

@ZY945
Copy link
Author

ZY945 commented Jul 4, 2023

The data
image

@ZY945
Copy link
Author

ZY945 commented Jul 4, 2023

These data are consistent

image
image

@ZY945
Copy link
Author

ZY945 commented Jul 4, 2023

I think it should be the _filed values of the values to compare
maybe should recordValues.get("_field").containsKey(columnName)
image

@ZY945
Copy link
Author

ZY945 commented Jul 4, 2023

I modified the corresponding judgment according to the returned record, and I was able to get the data, but the result is as follows
image
The change code
image

                    if (recordValues.containsValue(columnName)) {
                        col = columnName;
                    } else if (recordValues.containsValue("_" + columnName)) {
                        col = "_" + columnName;
                    } else if (anno != null && anno.measurement()) {
                        col = "_measurement";
                    } else {
                        String columnNameInSnakeCase = camelCaseToSnakeCase(columnName);
                        if (recordValues.containsValue(columnNameInSnakeCase)) {
                            col = columnNameInSnakeCase;
                        }
                    }

                    if (col != null) {
                        Object value = record.getValues().get("_value");

                        setFieldValue(pojo, field, value);
                    }

@ZY945
Copy link
Author

ZY945 commented Jul 5, 2023

I can only read multiple times, and then copy each time,

 public List<CPUStatVo> getOneSecondCpu(String bucket,String[] measurement) {
        Instant stop = DateUtil.getNowInstant();
        Instant start = stop.minus(Duration.ofSeconds(1));
        List<CPUStatVo> cpuStatVos = new ArrayList<>();
        for (int i = 0; i < measurement.length; i++) {
            InfluxBO bo = new InfluxBO();
            Flux statFlux = Flux.from(bucket)
                    .range(start, stop)
                    .filter(Restrictions.and(
                            Restrictions.measurement().equal(measurement[i])));
            bo.setFlux(statFlux);
            List<FluxTable> query = influxDBTemplate.query(QueryType.Flux,bo);
            CPUStatVo cpuStatVo = new CPUStatVo();
            for (FluxTable fluxTable : query) {
                List<FluxRecord> records = fluxTable.getRecords();
                for (FluxRecord fluxRecord : records) {
                    Object key = fluxRecord.getValueByKey("_field");
                    Object value = fluxRecord.getValueByKey("_value");
                    if(key==null){
                        continue;
                    }
                    cpuStatVo.setProperty((String) key, value);
                    System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));

                }
            }
            cpuStatVos.add(cpuStatVo);
        }

        return cpuStatVos;
    }

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant