Skip to content

Commit a95ec06

Browse files
authored
feat: add InfluxQL tags support (#584)
1 parent 1640d60 commit a95ec06

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 6.10.0 [unreleased]
22

3+
### Bug Fixes
4+
1. [#584](https://github.com/influxdata/influxdb-client-java/pull/584): InfluxQL tags support
5+
36
### Dependencies
47

58
Update dependencies:

client-core/src/main/java/com/influxdb/query/InfluxQLQueryResult.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.influxdb.query;
2323

2424
import java.util.ArrayList;
25+
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.Map;
2728
import javax.annotation.Nonnull;
@@ -86,6 +87,9 @@ public List<Series> getSeries() {
8687
* Represents one series within the {@link Result} of an InfluxQL query.
8788
*/
8889
public static final class Series {
90+
@Nonnull
91+
private final Map<String, String> tags;
92+
8993
@Nonnull
9094
private final Map<String, Integer> columns;
9195

@@ -95,10 +99,18 @@ public static final class Series {
9599
private final List<Record> values;
96100

97101
public Series(final @Nonnull String name, final @Nonnull Map<String, Integer> columns) {
102+
this(name, new HashMap<>(), columns);
103+
}
104+
105+
public Series(final @Nonnull String name,
106+
final @Nonnull Map<String, String> tags,
107+
final @Nonnull Map<String, Integer> columns) {
98108
Arguments.checkNotNull(name, "name");
109+
Arguments.checkNotNull(tags, "tags");
99110
Arguments.checkNotNull(columns, "columns");
100111

101112
this.name = name;
113+
this.tags = tags;
102114
this.columns = columns;
103115
this.values = new ArrayList<>();
104116
}
@@ -111,6 +123,14 @@ public String getName() {
111123
return this.name;
112124
}
113125

126+
/**
127+
* @return the tags
128+
*/
129+
@Nonnull
130+
public Map<String, String> getTags() {
131+
return this.tags;
132+
}
133+
114134
/**
115135
* @return the columns
116136
*/

client/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,20 @@ public class InfluxQLExample {
479479
}
480480
```
481481

482+
When the data are grouped by tag(s) using `GROUP BY` clause, series tags are accessible
483+
via `InfluxQLQueryResult.Series.getTags()` method, eg.
484+
```java
485+
...
486+
for (InfluxQLQueryResult.Result resultResult : result.getResults()) {
487+
for (InfluxQLQueryResult.Series series : resultResult.getSeries()) {
488+
for (Map.Entry<String, String> tag : series.getTags().entrySet()) {
489+
System.out.println(tag.getKey() + "=" + tag.getValue());
490+
}
491+
}
492+
}
493+
...
494+
```
495+
482496
## Writes
483497

484498
The client offers two types of API to ingesting data:

client/src/main/java/com/influxdb/client/internal/InfluxQLQueryApiImpl.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.Reader;
2727
import java.nio.charset.StandardCharsets;
2828
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.HashMap;
2931
import java.util.LinkedHashMap;
3032
import java.util.List;
3133
import java.util.Map;
@@ -110,13 +112,13 @@ static InfluxQLQueryResult readInfluxQLResult(
110112
@Nullable final InfluxQLQueryResult.Series.ValueExtractor valueExtractor
111113
) throws IOException {
112114
List<InfluxQLQueryResult.Result> results = new ArrayList<>();
113-
114-
Map<String, InfluxQLQueryResult.Series> series = null;
115+
Map<List<Object>, InfluxQLQueryResult.Series> series = null;
115116
Map<String, Integer> headerCols = null;
116-
int nameCol = 0;
117-
// The first 3 columns are static (`name`, `tags` and `time`) and got skipped.
117+
final int nameCol = 0;
118+
final int tagsCol = 1;
119+
// The first 2 columns are static (`name`, `tags`) and got skipped.
118120
// All other columns are dynamically parsed
119-
int dynamicColumnsStartIndex = 2;
121+
final int dynamicColumnsStartIndex = 2;
120122

121123
try (CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.builder().setIgnoreEmptyLines(false).build())) {
122124
for (CSVRecord csvRecord : parser) {
@@ -148,10 +150,11 @@ static InfluxQLQueryResult readInfluxQLResult(
148150

149151
} else {
150152
String name = csvRecord.get(nameCol);
153+
Map<String, String> finalTags = parseTags(csvRecord.get(tagsCol));
151154
Map<String, Integer> finalHeaderCols = headerCols;
152155
InfluxQLQueryResult.Series serie = series.computeIfAbsent(
153-
name,
154-
n -> new InfluxQLQueryResult.Series(n, finalHeaderCols)
156+
Arrays.asList(name, finalTags),
157+
n -> new InfluxQLQueryResult.Series(name, finalTags, finalHeaderCols)
155158
);
156159
Object[] values = headerCols.entrySet().stream().map(entry -> {
157160
String value = csvRecord.get(entry.getValue() + dynamicColumnsStartIndex);
@@ -174,4 +177,16 @@ static InfluxQLQueryResult readInfluxQLResult(
174177
}
175178
return new InfluxQLQueryResult(results);
176179
}
180+
181+
private static Map<String, String> parseTags(@Nonnull final String value) {
182+
final Map<String, String> tags = new HashMap<>();
183+
if (value.length() > 0) {
184+
for (String entry : value.split(",")) {
185+
final String[] kv = entry.split("=");
186+
tags.put(kv[0], kv[1]);
187+
}
188+
}
189+
190+
return tags;
191+
}
177192
}

client/src/test/java/com/influxdb/client/internal/InfluxQLQueryApiImplTest.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ void readInfluxQLResult() throws IOException {
6565
"\n" +
6666
"name,tags,name\n" +
6767
"databases,,measurement-1\n" +
68-
"databases,,measurement-2");
68+
"databases,,measurement-2\n" +
69+
"\n" +
70+
"name,tags,time,usage_user,usage_system\n" +
71+
"cpu,\"region=us-east-1,host=server1\",1483225200,13.57,1.4\n" +
72+
"cpu,\"region=us-east-1,host=server1\",1483225201,14.06,1.7\n" +
73+
"cpu,\"region=us-east-1,host=server2\",1483225200,67.91,1.3\n");
6974

7075
InfluxQLQueryResult result = InfluxQLQueryApiImpl.readInfluxQLResult(reader, NO_CANCELLING, extractValues);
7176

7277
List<InfluxQLQueryResult.Result> results = result.getResults();
73-
Assertions.assertThat(results).hasSize(3);
78+
Assertions.assertThat(results).hasSize(4);
7479
Assertions.assertThat(results.get(0))
7580
.extracting(InfluxQLQueryResult.Result::getSeries)
7681
.satisfies(series -> {
@@ -127,5 +132,43 @@ void readInfluxQLResult() throws IOException {
127132
.isEqualTo("measurement-2");
128133
});
129134
});
135+
136+
Assertions.assertThat(results.get(3))
137+
.extracting(InfluxQLQueryResult.Result::getSeries)
138+
.satisfies(series -> {
139+
Assertions.assertThat(series).hasSize(2);
140+
Assertions.assertThat(series.get(0))
141+
.satisfies(series1 -> {
142+
Assertions.assertThat(series1.getName()).isEqualTo("cpu");
143+
Assertions.assertThat(series1.getTags()).containsOnlyKeys("region", "host");
144+
Assertions.assertThat(series1.getTags().get("region")).isEqualTo("us-east-1");
145+
Assertions.assertThat(series1.getTags().get("host")).isEqualTo("server1");
146+
Assertions.assertThat(series1.getColumns()).containsOnlyKeys("time","usage_user","usage_system");
147+
Assertions.assertThat(series1.getValues()).hasSize(2);
148+
149+
Assertions.assertThat( series1.getValues().get(0).getValueByKey("usage_user"))
150+
.isEqualTo("13.57");
151+
Assertions.assertThat( series1.getValues().get(0).getValueByKey("usage_system"))
152+
.isEqualTo("1.4");
153+
Assertions.assertThat( series1.getValues().get(1).getValueByKey("usage_user"))
154+
.isEqualTo("14.06");
155+
Assertions.assertThat( series1.getValues().get(1).getValueByKey("usage_system"))
156+
.isEqualTo("1.7");
157+
});
158+
Assertions.assertThat(series.get(1))
159+
.satisfies(series2 -> {
160+
Assertions.assertThat(series2.getName()).isEqualTo("cpu");
161+
Assertions.assertThat(series2.getTags()).containsOnlyKeys("region", "host");
162+
Assertions.assertThat(series2.getTags().get("region")).isEqualTo("us-east-1");
163+
Assertions.assertThat(series2.getTags().get("host")).isEqualTo("server2");
164+
Assertions.assertThat(series2.getColumns()).containsOnlyKeys("time","usage_user","usage_system");
165+
Assertions.assertThat(series2.getValues()).hasSize(1);
166+
167+
Assertions.assertThat( series2.getValues().get(0).getValueByKey("usage_user"))
168+
.isEqualTo("67.91");
169+
Assertions.assertThat( series2.getValues().get(0).getValueByKey("usage_system"))
170+
.isEqualTo("1.3");
171+
});
172+
});
130173
}
131174
}

0 commit comments

Comments
 (0)