-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIOxExample.java
More file actions
129 lines (108 loc) · 5.51 KB
/
IOxExample.java
File metadata and controls
129 lines (108 loc) · 5.51 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
/*
* 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;
import java.time.Instant;
import java.util.Map;
import java.util.stream.Stream;
import com.influxdb.v3.client.InfluxDBClient;
import com.influxdb.v3.client.Point;
import com.influxdb.v3.client.PointValues;
import com.influxdb.v3.client.query.QueryOptions;
/**
* The example depends on the "influxdb3-java" module and this module should be built first
* by running "mvn install" in the root directory.
*/
public final class IOxExample {
private IOxExample() {
}
public static void main(final String[] args) throws Exception {
String host = "https://us-east-1-1.aws.cloud2.influxdata.com";
String token = "my-token";
String database = "my-database";
try (InfluxDBClient client = InfluxDBClient.getInstance(host, token.toCharArray(), database)) {
//
// Write by Point
//
Point point = Point.measurement("temperature")
.setTag("location", "west")
.setField("value", 55.15)
.setTimestamp(Instant.now().minusSeconds(-10));
client.writePoint(point);
//
// Write by LineProtocol
//
String record = "temperature,location=north value=60.0";
client.writeRecord(record);
//
// Query by SQL
//
System.out.printf("--------------------------------------------------------%n");
System.out.printf("| %-8s | %-8s | %-30s |%n", "location", "value", "time");
System.out.printf("--------------------------------------------------------%n");
String sql = "select time,location,value from temperature order by time desc limit 10";
try (Stream<Object[]> stream = client.query(sql)) {
stream.forEach(row -> System.out.printf("| %-8s | %-8s | %-30s |%n", row[1], row[2], row[0]));
}
System.out.printf("--------------------------------------------------------%n%n");
//
// Query by parametrized SQL
//
System.out.printf("--------------------Parametrized SQL--------------------%n");
System.out.printf("| %-8s | %-8s | %-30s |%n", "location", "value", "time");
System.out.printf("--------------------------------------------------------%n");
String sqlWithParameters =
"select time,location,value from temperature where location=$location order by time desc limit 10";
try (Stream<Object[]> stream = client.query(sqlWithParameters, Map.of("location", "north"))) {
stream.forEach(row -> System.out.printf("| %-8s | %-8s | %-30s |%n", row[1], row[2], row[0]));
}
System.out.printf("--------------------------------------------------------%n%n");
//
// Query by InfluxQL
//
System.out.printf("-----------------------------------------%n");
System.out.printf("| %-16s | %-18s |%n", "time", "mean");
System.out.printf("-----------------------------------------%n");
String influxQL =
"select MEAN(value) from temperature group by time(1d) fill(none) order by time desc limit 10";
try (Stream<Object[]> stream = client.query(influxQL, QueryOptions.defaultInfluxQlQueryOptions())) {
stream.forEach(row -> System.out.printf("| %-16s | %-18s |%n", row[1], row[2]));
}
System.out.printf("-----------------------------------------%n%n");
System.out.printf("--------------------------------------------------------%n");
System.out.printf("| %-8s | %-8s | %-30s |%n", "location", "value", "time");
System.out.printf("--------------------------------------------------------%n");
//
// Query by SQL into Points
//
try (Stream<PointValues> stream = client.queryPoints(sql)) {
stream.forEach(
(PointValues p) -> {
var time = p.getTimestamp();
var location = p.getTag("location");
var value = p.getField("value", Double.class);
System.out.printf("| %-8s | %-8s | %-30s |%n", location, value, time);
});
}
System.out.printf("--------------------------------------------------------%n%n");
}
}
}