Skip to content

Commit 2e74d60

Browse files
committed
feat: add an example how to define consistency with enterprise version
1 parent 21d3c99 commit 2e74d60

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.influxdb.client.domain.OnboardingRequest;
2828
import com.influxdb.client.domain.OnboardingResponse;
29+
import com.influxdb.client.domain.WriteConsistency;
2930
import com.influxdb.client.internal.InfluxDBClientImpl;
3031
import com.influxdb.utils.Arguments;
3132

@@ -160,7 +161,7 @@ public static InfluxDBClient create(@Nonnull final String url,
160161
}
161162

162163
/**
163-
* Create a instance of the InfluxDB 2.x client to connect into InfluxDB 1.8.
164+
* Create an instance of the InfluxDB 2.x client to connect into InfluxDB 1.8.
164165
*
165166
* @param url the url to connect to the InfluxDB 1.8
166167
* @param username authorization username
@@ -175,6 +176,31 @@ public static InfluxDBClient createV1(@Nonnull final String url,
175176
final char[] password,
176177
@Nonnull final String database,
177178
@Nullable final String retentionPolicy) {
179+
return createV1(url, username, password, database, retentionPolicy, null);
180+
}
181+
182+
/**
183+
* Create an instance of the InfluxDB 2.x client to connect into InfluxDB 1.8.
184+
*
185+
* @param url the url to connect to the InfluxDB 1.8
186+
* @param username authorization username
187+
* @param password authorization password
188+
* @param database database name
189+
* @param retentionPolicy retention policy
190+
* @param consistency Specify the write consistency for the point.
191+
* InfluxDB assumes that the write consistency is {@link WriteConsistency#ONE} if you
192+
* do not specify consistency. See the <a href="https://bit.ly/enterprise-consistency">
193+
* InfluxDB Enterprise documentation</a> for detailed descriptions of each consistency
194+
* option. <b>Available with InfluxDB Enterprise clusters only!</b>
195+
* @return client
196+
*/
197+
@Nonnull
198+
public static InfluxDBClient createV1(@Nonnull final String url,
199+
@Nullable final String username,
200+
final char[] password,
201+
@Nonnull final String database,
202+
@Nullable final String retentionPolicy,
203+
@Nullable final WriteConsistency consistency) {
178204

179205
Arguments.checkNonEmpty(database, "database");
180206

@@ -185,6 +211,7 @@ public static InfluxDBClient createV1(@Nonnull final String url,
185211
username == null ? "" : username,
186212
password == null ? "" : String.valueOf(password)).toCharArray())
187213
.bucket(String.format("%s/%s", database, retentionPolicy == null ? "" : retentionPolicy))
214+
.consistency(consistency)
188215
.build();
189216

190217
return create(options);

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ This directory contains Java, Kotlin and Scala examples.
77
- [ParameterizedQuery.java](src/main/java/example/ParameterizedQuery.java) - How to use Parameterized Queries
88

99
### Others
10-
- [InvocableScripts.py](src/main/java/example/InvocableScripts.java) - How to use Invocable scripts Cloud API to create custom endpoints that query data
10+
- [InvocableScripts.java](src/main/java/example/InvocableScripts.java) - How to use Invocable scripts Cloud API to create custom endpoints that query data
11+
- [InfluxDBEnterpriseExample.java](src/main/java/example/InfluxDBEnterpriseExample.java) - How to use `consistency` parameter for InfluxDB Enterprise
1112

1213
## Kotlin
1314

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 com.influxdb.client.InfluxDBClient;
25+
import com.influxdb.client.InfluxDBClientFactory;
26+
import com.influxdb.client.WriteApiBlocking;
27+
import com.influxdb.client.domain.WriteConsistency;
28+
import com.influxdb.client.domain.WritePrecision;
29+
import com.influxdb.client.write.WriteParameters;
30+
31+
/**
32+
* How to use `consistency` parameter for InfluxDB Enterprise.
33+
*
34+
* @author Jakub Bednar (12/04/2022 11:00)
35+
*/
36+
public class InfluxDBEnterpriseExample {
37+
38+
private static final String URL = "http://ec2-13-57-181-120.us-west-1.compute.amazonaws.com:8086";
39+
/**
40+
* Credentials
41+
*/
42+
private static final String USERNAME = "username";
43+
private static final String PASSWORD = "password";
44+
/**
45+
* Database name
46+
*/
47+
private static final String DB = "benchmark_db";
48+
/**
49+
* Retention policy name
50+
*/
51+
private static final String RP = "autogen";
52+
53+
public static void main(final String[] args) {
54+
55+
try (InfluxDBClient client = InfluxDBClientFactory.createV1(URL, USERNAME, PASSWORD.toCharArray(), DB, RP)) {
56+
57+
System.out.println("--- Write Record ---");
58+
59+
// Initialize Blocking API
60+
WriteApiBlocking writeApi = client.getWriteApiBlocking();
61+
62+
// Configure precision and consistency
63+
WriteParameters parameters = new WriteParameters(WritePrecision.NS, WriteConsistency.ALL);
64+
65+
// Write record
66+
writeApi.writeRecord("cpu_load_short,host=server02 value=0.67", parameters);
67+
68+
System.out.println("--- Written data with consistency set to: ALL ---");
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)