-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathInfluxDBClient.java
More file actions
594 lines (557 loc) · 23.7 KB
/
InfluxDBClient.java
File metadata and controls
594 lines (557 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.v3.client;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.arrow.vector.VectorSchemaRoot;
import com.influxdb.v3.client.config.ClientConfig;
import com.influxdb.v3.client.internal.InfluxDBClientImpl;
import com.influxdb.v3.client.query.QueryOptions;
import com.influxdb.v3.client.write.WriteOptions;
/**
* The InfluxDBClient interface provides a client for interact with InfluxDB 3.
* This client supports both writing data to InfluxDB and querying data using the FlightSQL client,
* which allows you to execute SQL queries against InfluxDB IOx.
*/
public interface InfluxDBClient extends AutoCloseable {
/**
* Write a record specified in the InfluxDB Line Protocol to the InfluxDB server.
*
* @param record the record specified in the InfluxDB Line Protocol, can be null
*/
void writeRecord(@Nullable final String record);
/**
* Write a record specified in the InfluxDB Line Protocol to the InfluxDB server.
*
* @param record the record specified in the InfluxDB Line Protocol, can be null
* @param options the options for writing data to InfluxDB
*/
void writeRecord(@Nullable final String record, @Nonnull final WriteOptions options);
/**
* Write records specified in the InfluxDB Line Protocol to the InfluxDB server.
*
* @param records the records specified in the InfluxDB Line Protocol, cannot be null
*/
void writeRecords(@Nonnull final List<String> records);
/**
* Write records specified in the InfluxDB Line Protocol to the InfluxDB server.
*
* @param records the records specified in the InfluxDB Line Protocol, cannot be null
* @param options the options for writing data to InfluxDB
*/
void writeRecords(@Nonnull final List<String> records, @Nonnull final WriteOptions options);
/**
* Write a {@link Point} to the InfluxDB server.
*
* @param point the {@link Point} to write, can be null
* <p>
* Note: the timestamp passed will be converted to nanoseconds since the Unix epoch
* by NanosecondConverter helper class
*/
void writePoint(@Nullable final Point point);
/**
* Write a {@link Point} to the InfluxDB server.
*
* @param point the {@link Point} to write, can be null
* @param options the options for writing data to InfluxDB
* <p>
* Note: the timestamp passed will be converted to nanoseconds since the Unix epoch
* by NanosecondConverter helper class
*/
void writePoint(@Nullable final Point point, @Nonnull final WriteOptions options);
/**
* Write a list of {@link Point} to the InfluxDB server.
*
* @param points the list of {@link Point} to write, cannot be null
* <p>
* Note: the timestamp passed will be converted to nanoseconds since the Unix epoch
* by NanosecondConverter helper class
*/
void writePoints(@Nonnull final List<Point> points);
/**
* Write a list of {@link Point} to the InfluxDB server.
*
* @param points the list of {@link Point} to write, cannot be null
* @param options the options for writing data to InfluxDB
* <p>
* Note: the timestamp passed will be converted to nanoseconds since the Unix epoch
* by NanosecondConverter helper class
*/
void writePoints(@Nonnull final List<Point> points, @Nonnull final WriteOptions options);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<Object[]> rows = client.query("select * from cpu")) {
* rows.forEach(row -> {
* // process row
* }
* });
* </pre>
*
* @param query the SQL query string to execute, cannot be null
* @return Batches of rows returned by the query
* <p>
* Note: the timestamp will be returned as a number of nanoseconds since the Unix epoch
*/
@Nonnull
Stream<Object[]> query(@Nonnull final String query);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<Object[]> rows = client.query("select * from cpu where host=$host",
* Map.of("host", "server-a")) {
* rows.forEach(row -> {
* // process row
* }
* });
* </pre>
*
* @param query the SQL query string to execute, cannot be null
* @param parameters query named parameters
* @return Batches of rows returned by the query
* <p>
* Note: the timestamp will be returned as a number of nanoseconds since the Unix epoch
*/
@Nonnull
Stream<Object[]> query(@Nonnull final String query, @Nonnull final Map<String, Object> parameters);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<Object[]> rows = client.query("select * from cpu", options)) {
* rows.forEach(row -> {
* // process row
* }
* });
* </pre>
*
* @param query the query string to execute, cannot be null
* @param options the options for querying data from InfluxDB
* @return Batches of rows returned by the query
* <p>
* Note: the timestamp will be returned as a number of nanoseconds since the Unix epoch
*/
@Nonnull
Stream<Object[]> query(@Nonnull final String query, @Nonnull final QueryOptions options);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<Object[]> rows = client.query("select * from cpu where host=$host",
* Map.of("host", "server-a"), options)) {
* rows.forEach(row -> {
* // process row
* }
* });
* </pre>
*
* @param query the query string to execute, cannot be null
* @param parameters query named parameters
* @param options the options for querying data from InfluxDB
* @return Batches of rows returned by the query
* <p>
* Note: the timestamp will be returned as a number of nanoseconds since the Unix epoch
*/
@Nonnull
Stream<Object[]> query(@Nonnull final String query,
@Nonnull final Map<String, Object> parameters,
@Nonnull final QueryOptions options);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<Map<String, Object>> rows = client.queryRows("select * from cpu where host=intel")) {
* rows.forEach(row -> {
* // process row
* });
* };
* </pre>
*
* @param query the query string to execute, cannot be null
* @return Batches of rows returned by the query
*/
@Nonnull
Stream<Map<String, Object>> queryRows(@Nonnull final String query);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<Map<String, Object>> rows = client.queryRows("select * from cpu where host=$host",
* Map.of("host", "server-a"))) {
* rows.forEach(row -> {
* // process row
* })
* };
* </pre>
*
* @param query the query string to execute, cannot be null
* @param parameters query named parameters
* @return Batches of rows returned by the query
*/
@Nonnull
Stream<Map<String, Object>> queryRows(@Nonnull final String query, @Nonnull final Map<String, Object> parameters);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<Map<String, Object>> rows = client.queryRows("select * from cpu where host=intel",
* options)) {
* rows.forEach(row -> {
* // process row
* })
* };
* </pre>
*
* @param query the query string to execute, cannot be null
* @param options the options for querying data from InfluxDB
* @return Batches of rows returned by the query
*/
@Nonnull
Stream<Map<String, Object>> queryRows(@Nonnull final String query, @Nonnull final QueryOptions options);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<Map<String, Object>> rows = client.queryRows("select * from cpu where host=$host",
* Map.of("host", "server-a"), options)) {
* rows.forEach(row -> {
* // process row
* })
* };
* </pre>
*
* @param query the query string to execute, cannot be null
* @param parameters query named parameters
* @param options the options for querying data from InfluxDB
* @return Batches of rows returned by the query
*/
@Nonnull
Stream<Map<String, Object>> queryRows(@Nonnull final String query,
@Nonnull final Map<String, Object> parameters,
@Nonnull final QueryOptions options);
/**
* Query data from InfluxDB IOx into Point structure using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<PointValues> rows = client.queryPoints("select * from cpu", options)) {
* rows.forEach(row -> {
* // process row
* }
* });
* </pre>
*
* @param query the SQL query string to execute, cannot be null
* @return Batches of PointValues returned by the query
* <p>
* Note: the timestamp will be returned as a number of nanoseconds since the Unix epoch
*/
@Nonnull
Stream<PointValues> queryPoints(@Nonnull final String query);
/**
* Query data from InfluxDB IOx into Point structure using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<PointValues> rows = client.queryPoints("select * from cpu where host=$host",
* Map.of("host", "server-a"))) {
* rows.forEach(row -> {
* // process row
* }
* });
* </pre>
*
* @param query the SQL query string to execute, cannot be null
* @param parameters query named parameters
* @return Batches of PointValues returned by the query
* <p>
* Note: the timestamp will be returned as a number of nanoseconds since the Unix epoch
*/
@Nonnull
Stream<PointValues> queryPoints(@Nonnull final String query, @Nonnull final Map<String, Object> parameters);
/**
* Query data from InfluxDB IOx into Point structure using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<PointValues> rows = client.queryPoints("select * from cpu", options)) {
* rows.forEach(row -> {
* // process row
* }
* });
* </pre>
*
* @param query the query string to execute, cannot be null
* @param options the options for querying data from InfluxDB
* @return Batches of PointValues returned by the query
* <p>
* Note: the timestamp will be returned as a number of nanoseconds since the Unix epoch
*/
@Nonnull
Stream<PointValues> queryPoints(@Nonnull final String query, @Nonnull final QueryOptions options);
/**
* Query data from InfluxDB IOx into Point structure using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<PointValues> rows = client.queryPoints("select * from cpu where host=$host",
* Map.of("host", "server-a"),
* options)) {
* rows.forEach(row -> {
* // process row
* }
* });
* </pre>
*
* @param query the query string to execute, cannot be null
* @param parameters query named parameters
* @param options the options for querying data from InfluxDB
* @return Batches of PointValues returned by the query
* <p>
* Note: the timestamp will be returned as a number of nanoseconds since the Unix epoch
*/
@Nonnull
Stream<PointValues> queryPoints(@Nonnull final String query,
@Nonnull final Map<String, Object> parameters,
@Nonnull final QueryOptions options);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<VectorSchemaRoot> batches = client.queryBatches("select * from cpu")) {
* batches.forEach(batch -> {
* // process batch
* }
* });
* </pre>
*
* @param query the SQL query string to execute, cannot be null
* @return Batches of rows returned by the query
*/
@Nonnull
Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <p>
* The result stream should be closed after use, you can use try-resource pattern to close it automatically:
* <pre>
* try (Stream<VectorSchemaRoot> batches = client.queryBatches("select * from cpu where host=$host",
* Map.of("host", "server-a"))) {
* batches.forEach(batch -> {
* // process batch
* }
* });
* </pre>
*
* @param query the SQL query string to execute, cannot be null
* @param parameters query named parameters
* @return Batches of rows returned by the query
*/
@Nonnull
Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query, @Nonnull final Map<String, Object> parameters);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <pre>
* try (Stream<VectorSchemaRoot> batches = client.queryBatches("select * from cpu", options)) {
* batches.forEach(batch -> {
* // process batch
* }
* });
* </pre>
*
* @param query the query string to execute, cannot be null
* @param options the options for querying data from InfluxDB
* @return Batches of rows returned by the query
*/
@Nonnull
Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query, @Nonnull final QueryOptions options);
/**
* Query data from InfluxDB IOx using FlightSQL.
* <pre>
* try (Stream<VectorSchemaRoot> batches = client.queryBatches("select * from cpu where host=$host",
* Map.of("host", "server-a"),
* options)) {
* batches.forEach(batch -> {
* // process batch
* }
* });
* </pre>
*
* @param query the query string to execute, cannot be null
* @param parameters query named parameters
* @param options the options for querying data from InfluxDB
* @return Batches of rows returned by the query
*/
@Nonnull
Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query,
@Nonnull final Map<String, Object> parameters,
@Nonnull final QueryOptions options);
/**
* Get InfluxDB server version.
*
* @return a string representing the server version.
* Returns <code>null</code> if the server version can't be determined.
*/
@Nullable
String getServerVersion();
/**
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
* common operations such as writing, querying.
*
* @param host the URL of the InfluxDB server
* @param token the authentication token for accessing the InfluxDB server, can be null
* @param database the database to be used for InfluxDB operations, can be null
* @return new instance of the {@link InfluxDBClient}
*/
@Nonnull
static InfluxDBClient getInstance(@Nonnull final String host,
@Nullable final char[] token,
@Nullable final String database) {
ClientConfig config = new ClientConfig.Builder()
.host(host)
.token(token)
.database(database)
.build();
return getInstance(config);
}
/**
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
* common operations such as writing, querying.
*
* @param host the URL of the InfluxDB server
* @param token the authentication token for accessing the InfluxDB server, can be null
* @param database the database to be used for InfluxDB operations, can be null
* @param defaultTags tags to be added by default to writes of points
* @return new instance of the {@link InfluxDBClient}
*/
@Nonnull
static InfluxDBClient getInstance(@Nonnull final String host,
@Nullable final char[] token,
@Nullable final String database,
@Nullable Map<String, String> defaultTags) {
ClientConfig config = new ClientConfig.Builder()
.host(host)
.token(token)
.database(database)
.defaultTags(defaultTags)
.build();
return getInstance(config);
}
/**
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
* common operations such as writing, querying.
* For possible configuration options see {@link ClientConfig}.
*
* @param config the configuration for the InfluxDB client
* @return new instance of the {@link InfluxDBClient}
*/
@Nonnull
static InfluxDBClient getInstance(@Nonnull final ClientConfig config) {
return new InfluxDBClientImpl(config);
}
/**
* Creates a new instance of the {@link InfluxDBClient} from the connection string in URL format.
* <p>
* Example:
* <pre>
* client = InfluxDBClient.getInstance("https://us-east-1-1.aws.cloud2.influxdata.com/"
* + "?token=my-token&database=my-database");
* </pre>
* <p>
* Supported parameters:
* <ul>
* <li>token - authentication token <i>(required)</i></li>
* <li>org - organization name</li>
* <li>database - database (bucket) name</li>
* <li>precision - timestamp precision when writing data</li>
* <li>gzipThreshold - payload size size for gzipping data</li>
* <li>writeNoSync - skip waiting for WAL persistence on write</li>
* <li>writeAcceptPartial - accept partial writes</li>
* </ul>
*
* @param connectionString connection string
* @return instance of {@link InfluxDBClient}
*/
@Nonnull
static InfluxDBClient getInstance(@Nonnull final String connectionString) {
try {
return getInstance(new ClientConfig.Builder().build(connectionString));
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e); // same exception as ClientConfig.validate()
}
}
/**
* Creates a new instance of the {@link InfluxDBClient} from environment variables and/or system properties.
* Environment variables take precedence over system properties.
* <p>
* Example:
* <pre>
* client = InfluxDBClient.getInstance();
* </pre>
* <p>
* Supported environment variables:
* <ul>
* <li>INFLUX_HOST - cloud/server URL <i>required</i></li>
* <li>INFLUX_TOKEN - authentication token <i>required</i></li>
* <li>INFLUX_AUTH_SCHEME - authentication scheme</li>
* <li>INFLUX_ORG - organization name</li>
* <li>INFLUX_DATABASE - database (bucket) name</li>
* <li>INFLUX_PRECISION - timestamp precision when writing data</li>
* <li>INFLUX_GZIP_THRESHOLD - payload size size for gzipping data</li>
* <li>INFLUX_WRITE_NO_SYNC - skip waiting for WAL persistence on write</li>
* <li>INFLUX_WRITE_ACCEPT_PARTIAL - accept partial writes</li>
* </ul>
* Supported system properties:
* <ul>
* <li>influx.host - cloud/server URL <i>required</i></li>
* <li>influx.token - authentication token <i>required</i></li>
* <li>influx.authScheme - authentication scheme</li>
* <li>influx.org - organization name</li>
* <li>influx.database - database (bucket) name</li>
* <li>influx.precision - timestamp precision when writing data</li>
* <li>influx.gzipThreshold - payload size size for gzipping data</li>
* <li>influx.writeNoSync - skip waiting for WAL persistence on write</li>
* <li>influx.writeAcceptPartial - accept partial writes</li>
* </ul>
*
* @return instance of {@link InfluxDBClient}
*/
@Nonnull
static InfluxDBClient getInstance() {
return getInstance(new ClientConfig.Builder().build(System.getenv(), System.getProperties()));
}
}