Skip to content

Commit 125a9ca

Browse files
authored
Added version of query method (#687)
* Added version of query method InfluxDBResultMapper was recently updated with this version of .toPojo: public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, final String measurementName) throws InfluxDBMapperException { return toPOJO(queryResult, clazz, measurementName, TimeUnit.MILLISECONDS); } Adding InfluxDBMapper.query(final Query query, final Class<T> clazz, String measurementName) to take advantage of the new toPojo variation to map a db with variable measurement names to a POJO. Without this pull, mapping Query to Pojo requires both an InfluxDB instance to fetch the query result and an influxDBMapper instance to map to call toPojo directly.
1 parent 93c0093 commit 125a9ca

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
### Features
66
- Add an option in `BatchOption` to prevent `InfluxDB#write` from blocking when actions queue is exhausted. [Issue #668](https://github.com/influxdata/influxdb-java/issues/688)
7+
- Added new signature to InfluxDBMapper.query() with params final Query query, final Class<T> clazz, final String measurementName to leverage InfluxDBResultMapper.toPojo method with identical signature.
78

9+
### Improvements
10+
11+
- Test: Added test for new InfluxDBMapper.query() signature, as well as test for existing InfluxDBMapper.query(Class clazz) signature (previously only InfluxDBMapper.query(Query query, Class clazz) was tested).
812

913
## 2.19 [2020-05-18]
1014

src/main/java/org/influxdb/impl/InfluxDBMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public InfluxDBMapper(final InfluxDB influxDB) {
2121
this.influxDB = influxDB;
2222
}
2323

24+
public <T> List<T> query(final Query query, final Class<T> clazz, final String measurementName) {
25+
QueryResult queryResult = influxDB.query(query);
26+
return toPOJO(queryResult, clazz, measurementName);
27+
}
28+
2429
public <T> List<T> query(final Query query, final Class<T> clazz) {
2530
throwExceptionIfMissingAnnotation(clazz);
2631
QueryResult queryResult = influxDB.query(query);

src/test/java/org/influxdb/impl/InfluxDBMapperTest.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ public void testQuery() {
5555
Assert.assertTrue(persistedMeasures.size()>0);
5656
}
5757

58+
@Test
59+
public void testQueryWhenCalledWithClassOnly() {
60+
ServerMeasure serverMeasure = createMeasure();
61+
influxDBMapper.save(serverMeasure);
62+
63+
List<ServerMeasure> persistedMeasures = influxDBMapper.query(ServerMeasure.class);
64+
Assert.assertTrue(persistedMeasures.size()>0);
65+
}
66+
67+
@Test
68+
public void testQueryWhenCalledWithQuery_Class_MeasurementName() {
69+
ServerMeasure serverMeasure = createMeasure();
70+
influxDBMapper.save(serverMeasure);
71+
72+
List<NonAnnotatedServerMeasure> persistedMeasures = influxDBMapper.query(new Query("SELECT * FROM server_measure",UDP_DATABASE), NonAnnotatedServerMeasure.class, "server_measure");
73+
Assert.assertTrue(persistedMeasures.size()>0);
74+
}
75+
5876
@Test
5977
public void testIllegalField() {
6078
InvalidMeasure invalidMeasure = new InvalidMeasure();
@@ -185,6 +203,87 @@ public void setIp(String ip) {
185203
}
186204
}
187205

206+
static class NonAnnotatedServerMeasure {
207+
208+
/** Check the instant conversions */
209+
@Column(name = "time")
210+
private Instant time;
211+
212+
@Column(name = "name", tag = true)
213+
private String name;
214+
215+
@Column(name = "cpu")
216+
private double cpu;
217+
218+
@Column(name = "healthy")
219+
private boolean healthy;
220+
221+
@Column(name = "min")
222+
private long uptime;
223+
224+
@Column(name = "memory_utilization")
225+
private Double memoryUtilization;
226+
227+
@Column(name = "ip")
228+
private String ip;
229+
230+
public Instant getTime() {
231+
return time;
232+
}
233+
234+
public void setTime(Instant time) {
235+
this.time = time;
236+
}
237+
238+
public String getName() {
239+
return name;
240+
}
241+
242+
public void setName(String name) {
243+
this.name = name;
244+
}
245+
246+
public double getCpu() {
247+
return cpu;
248+
}
249+
250+
public void setCpu(double cpu) {
251+
this.cpu = cpu;
252+
}
253+
254+
public boolean isHealthy() {
255+
return healthy;
256+
}
257+
258+
public void setHealthy(boolean healthy) {
259+
this.healthy = healthy;
260+
}
261+
262+
public long getUptime() {
263+
return uptime;
264+
}
265+
266+
public void setUptime(long uptime) {
267+
this.uptime = uptime;
268+
}
269+
270+
public Double getMemoryUtilization() {
271+
return memoryUtilization;
272+
}
273+
274+
public void setMemoryUtilization(Double memoryUtilization) {
275+
this.memoryUtilization = memoryUtilization;
276+
}
277+
278+
public String getIp() {
279+
return ip;
280+
}
281+
282+
public void setIp(String ip) {
283+
this.ip = ip;
284+
}
285+
}
286+
188287
@Measurement(name = "invalid_measure", database = UDP_DATABASE)
189288
static class InvalidMeasure {
190289

0 commit comments

Comments
 (0)