Skip to content

Commit 99f1b5f

Browse files
authored
docs: Clarify how to use a client with InfluxDB 1.8 (#103)
1 parent fbf5d57 commit 99f1b5f

File tree

5 files changed

+147
-8
lines changed

5 files changed

+147
-8
lines changed

CHANGELOG.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
1. [#93](https://github.com/influxdata/influxdb-client-java/issues/93): Add addTags and addFields helper functions to Point
55
1. [#97](https://github.com/influxdata/influxdb-client-java/pull/97): Add the ability to specify the org and the bucket when creating the client
66

7-
### Bugs
7+
### Documentation
8+
1. [#103](https://github.com/influxdata/influxdb-client-java/pull/103): Clarify how to use a client with InfluxDB 1.8
9+
10+
### Bug Fixes
811
1. [#98](https://github.com/influxdata/influxdb-client-java/issues/98): @Column supports super class inheritance for write measurements
912

1013
## 1.6.0 [2020-03-13]
@@ -14,7 +17,7 @@
1417
1. [#83](https://github.com/influxdata/influxdb-client-java/issues/83): Add reduce operator to FluxDSL
1518
1. [#91](https://github.com/influxdata/influxdb-client-java/pull/91): Set User-Agent to influxdb-client-java/VERSION for all requests
1619

17-
### Bugs
20+
### Bug Fixes
1821
1. [#90](https://github.com/influxdata/influxdb-client-java/pull/90): Correctly parse CSV where multiple results include multiple tables
1922
1. [#89](https://github.com/influxdata/influxdb-client-java/issues/89): @Column supports super class inheritance
2023

@@ -25,7 +28,7 @@
2528
1. [#33](https://github.com/influxdata/influxdb-client-java/issues/33): InfluxDBClient.close also dispose a created writeApi
2629
1. [#80](https://github.com/influxdata/influxdb-client-java/issues/80): FluxRecord, FluxColumn, FluxTable are serializable
2730

28-
### Bugs
31+
### Bug Fixes
2932
1. [#82](https://github.com/influxdata/influxdb-client-java/pull/82): Apply backpressure strategy when a buffer overflow
3033

3134
## 1.4.0 [2020-01-17]
@@ -41,7 +44,7 @@
4144
### API
4245
1. [#68](https://github.com/influxdata/influxdb-client-java/pull/68): Updated swagger to latest version
4346

44-
### Bugs
47+
### Bug Fixes
4548
1. [#69](https://github.com/influxdata/influxdb-client-java/issues/69): Fixed android compatibility
4649

4750
## 1.2.0 [2019-11-08]
@@ -64,15 +67,15 @@
6467
### API
6568
1. [#58](https://github.com/influxdata/influxdb-client-java/pull/58): Updated swagger to latest version
6669

67-
### Bugs
70+
### Bug Fixes
6871
1. [#57](https://github.com/influxdata/influxdb-client-java/pull/57): LabelsApi: orgID parameter has to be pass as second argument
6972

7073
## 1.0.0 [2019-08-30]
7174

7275
### Features
7376
1. [#50](https://github.com/influxdata/influxdb-client-java/issues/50): Added support for gzip compression of query response
7477

75-
### Bugs
78+
### Bug Fixes
7679
1. [#48](https://github.com/influxdata/influxdb-client-java/issues/48): The org parameter takes either the ID or Name interchangeably
7780
1. [#53](https://github.com/influxdata/influxdb-client-java/issues/53): Drop NaN and infinity values from fields when writing to InfluxDB
7881

@@ -92,7 +95,7 @@
9295
1. [#35](https://github.com/influxdata/influxdb-client-java/issues/35): Possibility to specify default tags
9396
1. [#41](https://github.com/influxdata/influxdb-client-java/issues/41): Synchronous blocking API to Write time-series data into InfluxDB 2.0
9497

95-
### Bugs
98+
### Bug Fixes
9699
1. [#43](https://github.com/influxdata/influxdb-client-java/issues/43): The data point without field should be ignored
97100

98101
### CI

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
This repository contains the reference JVM clients for the InfluxDB 2.0. Currently, Java, Reactive, Kotlin and Scala clients are implemented.
1313

14-
#### Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+. For connecting to InfluxDB 1.7 or earlier instances, use the [influxdb-java](https://github.com/influxdata/influxdb-java) client library.
14+
#### Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ ([see details](#influxdb-18-api-compatibility)). For connecting to InfluxDB 1.7 or earlier instances, use the [influxdb-java](https://github.com/influxdata/influxdb-java) client library.
1515

1616
- [Features](#features)
1717
- [Documentation](#documentation)
@@ -281,6 +281,20 @@ public class InfluxDB2ManagementExample {
281281
}
282282
```
283283

284+
### InfluxDB 1.8 API compatibility
285+
286+
[InfluxDB 1.8.0 introduced forward compatibility APIs](https://docs.influxdata.com/influxdb/latest/tools/api/#influxdb-2-0-api-compatibility-endpoints) for InfluxDB 2.0. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.0 Cloud or open source.
287+
288+
The following forward compatible APIs are available:
289+
290+
| API | Endpoint | Description |
291+
|:----------|:----------|:----------|
292+
| [QueryApi.java](client/src/main/java/com/influxdb/client/QueryApi.java) | [/api/v2/query](https://docs.influxdata.com/influxdb/latest/tools/api/#api-v2-query-http-endpoint) | Query data in InfluxDB 1.8.0+ using the InfluxDB 2.0 API and [Flux](https://docs.influxdata.com/flux/latest/) _(endpoint should be enabled by [`flux-enabled` option](https://docs.influxdata.com/influxdb/latest/administration/config/#flux-enabled-false))_ |
293+
| [WriteApi.java](client/src/main/java/com/influxdb/client/WriteApi.java) | [/api/v2/write](https://docs.influxdata.com/influxdb/latest/tools/api/#api-v2-write-http-endpoint) | Write data to InfluxDB 1.8.0+ using the InfluxDB 2.0 API |
294+
| [health()](client/src/main/java/com/influxdb/client/InfluxDBClient.java#L236) | [/health](https://docs.influxdata.com/influxdb/latest/tools/api/#health-http-endpoint) | Check the health of your InfluxDB instance |
295+
296+
For detail info see [InfluxDB 1.8 example](examples/src/main/java/example/InfluxDB18Example.java).
297+
284298
### Flux queries in InfluxDB 1.7+
285299

286300
The following example demonstrates querying using the Flux language.

client/src/main/java/com/influxdb/client/InfluxDBClientFactory.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,37 @@ public static InfluxDBClient create(@Nonnull final String url,
152152
return create(options);
153153
}
154154

155+
/**
156+
* Create a instance of the InfluxDB 2.0 client to connect into InfluxDB 1.8.
157+
*
158+
* @param url the url to connect to the InfluxDB 1.8
159+
* @param username authorization username
160+
* @param password authorization password
161+
* @param database database name
162+
* @param retentionPolicy retention policy
163+
* @return client
164+
*/
165+
@Nonnull
166+
public static InfluxDBClient createV1(@Nonnull final String url,
167+
@Nullable final String username,
168+
final char[] password,
169+
@Nonnull final String database,
170+
@Nullable final String retentionPolicy) {
171+
172+
Arguments.checkNonEmpty(database, "database");
173+
174+
InfluxDBClientOptions options = InfluxDBClientOptions.builder()
175+
.url(url)
176+
.org("-")
177+
.authenticateToken(String.format("%s:%s",
178+
username == null ? "" : username,
179+
password == null ? "" : String.valueOf(password)).toCharArray())
180+
.bucket(String.format("%s/%s", database, retentionPolicy == null ? "" : retentionPolicy))
181+
.build();
182+
183+
return create(options);
184+
}
185+
155186
/**
156187
* Create an instance of the InfluxDB 2.0 client.
157188
*

client/src/test/java/com/influxdb/client/InfluxDBClientFactoryTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,24 @@ void loadFromProperties() throws NoSuchFieldException, IllegalAccessException {
159159
.hasEntrySatisfying("version", value -> Assertions.assertThat(value).isEqualTo("${version}"))
160160
.hasEntrySatisfying("hostname", value -> Assertions.assertThat(value).isEqualTo("${env.hostname}"));
161161
}
162+
163+
@Test
164+
void v1Configuration() throws NoSuchFieldException, IllegalAccessException {
165+
InfluxDBClient client = InfluxDBClientFactory.createV1("http://localhost:8086", "my-username", "my-password".toCharArray(), "database", "week");
166+
167+
InfluxDBClientOptions options = getDeclaredField(client, "options", AbstractInfluxDBClient.class);
168+
169+
Assertions.assertThat(options.getUrl()).isEqualTo("http://localhost:8086");
170+
Assertions.assertThat(options.getOrg()).isEqualTo("-");
171+
Assertions.assertThat(options.getBucket()).isEqualTo("database/week");
172+
Assertions.assertThat(options.getToken()).isEqualTo("my-username:my-password".toCharArray());
173+
174+
client = InfluxDBClientFactory.createV1("http://localhost:8086", null, null, "database", null);
175+
176+
options = getDeclaredField(client, "options", AbstractInfluxDBClient.class);
177+
Assertions.assertThat(options.getUrl()).isEqualTo("http://localhost:8086");
178+
Assertions.assertThat(options.getOrg()).isEqualTo("-");
179+
Assertions.assertThat(options.getBucket()).isEqualTo("database/");
180+
Assertions.assertThat(options.getToken()).isEqualTo(":".toCharArray());
181+
}
162182
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package example;
23+
24+
import java.util.List;
25+
26+
import com.influxdb.client.InfluxDBClient;
27+
import com.influxdb.client.InfluxDBClientFactory;
28+
import com.influxdb.client.WriteApi;
29+
import com.influxdb.client.write.Point;
30+
import com.influxdb.query.FluxTable;
31+
32+
/**
33+
* @author Jakub Bednar (16/04/2020 11:00)
34+
*/
35+
public class InfluxDB18Example {
36+
37+
public static void main(final String[] args) {
38+
39+
String database = "telegraf";
40+
String retentionPolicy = "autogen";
41+
42+
InfluxDBClient client = InfluxDBClientFactory.createV1("http://localhost:8086",
43+
"username",
44+
"password".toCharArray(),
45+
database,
46+
retentionPolicy);
47+
48+
System.out.println("*** Write Points ***");
49+
50+
try (WriteApi writeApi = client.getWriteApi()) {
51+
52+
Point point = Point.measurement("mem")
53+
.addTag("host", "host1")
54+
.addField("used_percent", 29.43234543);
55+
56+
System.out.println(point.toLineProtocol());
57+
58+
writeApi.writePoint(point);
59+
}
60+
61+
System.out.println("*** Query Points ***");
62+
String query = String.format("from(bucket: \"%s/%s\") |> range(start: -1h)", database, retentionPolicy);
63+
64+
List<FluxTable> tables = client.getQueryApi().query(query);
65+
tables.get(0).getRecords()
66+
.forEach(record -> System.out.println(String.format("%s %s: %s %s",
67+
record.getTime(), record.getMeasurement(), record.getField(), record.getValue())));
68+
69+
client.close();
70+
}
71+
}

0 commit comments

Comments
 (0)