influxdb-client-java
influxdb-client-java copied to clipboard
About the failure of the query method-----query(@Nonnull String var1, @Nonnull Class<M> var2)
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: