-
Notifications
You must be signed in to change notification settings - Fork 128
/
WriteDataEverySecond.java
140 lines (122 loc) · 5.03 KB
/
WriteDataEverySecond.java
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
/*
* 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 example;
import java.time.Instant;
import java.util.Timer;
import java.util.TimerTask;
import com.influxdb.LogLevel;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApi;
import com.influxdb.client.WriteOptions;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
/**
* The example how to write data every seconds to the InfluxDB.
*
* Usage:
* <pre>
* java WriteDataEverySecond --url http://localhost:9999 --token my-token --bucket my-bucket --org my-org
* </pre>
*
* @author Jakub Bednar (06/10/2020 10:00)
*/
public class WriteDataEverySecond {
public static void main(final String[] args) throws ParseException {
//
// Parse configuration
//
CommandLine line = parseArgs(args);
String url = line.getOptionValue("u", "http://localhost:9999");
String token = line.getOptionValue("t", "my-token");
String org = line.getOptionValue("o", "my-org");
String bucket = line.getOptionValue("b", "my-bucket");
//
// Initialize client
//
InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray(), org, bucket)
.setLogLevel(LogLevel.BASIC);
//
// Initialize write API with flush interval to 5sec
//
WriteApi writeApi = client.makeWriteApi(WriteOptions.builder().flushInterval(5_000).build());
//
// Produce DataPoint every seconds and pass it to WriteApi
//
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// Create DataPoint
Point point = Point
.measurement("temperature")
.addField("value", Math.random() * 100)
.time(Instant.now(), WritePrecision.NS);
System.out.println("Produced DataPoint: " + point.toLineProtocol());
// Add Point to bath
writeApi.writePoint(point);
}
}, 0, 1000);
//
// Close client and producer at exit
//
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("Close Client and Producer");
timer.cancel();
client.close();
}));
}
/**
* Parse command line arguments.
*
* @param args the list of atomic option and value tokens
* @return parsed args
* @throws ParseException if there are any problems while parsing args
*/
private static CommandLine parseArgs(final String[] args) throws ParseException {
Options options = new Options()
.addOption("h", "help", false, "Print this help")
.addOption("u", "url", true,
"The url to connect to the InfluxDB. Default 'http://localhost:9999'.")
.addOption("t", "token", true,
"The token to use for the authorization. Default 'my-token'.")
.addOption("o", "org", true,
"The name of an organization. Default 'my-org'.")
.addOption("b", "bucket", true,
"The name of a bucket to write. Default 'my-bucket'.");
CommandLineParser parser = new DefaultParser();
CommandLine line = parser.parse(options, args);
if (line.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
formatter.setWidth(2000);
formatter.printHelp("java WriteDataEverySecond", options, true);
System.exit(0);
}
return line;
}
}