influxdata / influxdata/influxdb-client-java

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

Open
#598 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
477
Forks
129
PR merge metrics
No merged PRs in 30d

Description

__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:__
```xml
3.1.0
17
UTF-8

com.influxdb
influxdb-client-java
6.9.0


com.influxdb
flux-dsl
6.9.0

```
__The model:__
```java

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:__
```java
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 queryList = client.getQueryApi().query(statFlux.toString(), Stat.class);
queryList.forEach(l -> {
System.out.println(l.toString());
});

List 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](https://github.com/influxdata/influxdb-client-java/assets/74083801/774f1419-70a1-42ec-9476-26a631eac3d4)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at QueryApi.query(String, Class) and compare its behavior with query(String), using the provided Stat model and Flux query to reproduce the mapping result. Done means the class-based query returns the populated field values shown by the raw FluxTable records.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.