|
| 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 com.influxdb.v3; |
| 23 | + |
| 24 | +import java.util.UUID; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import com.influxdb.v3.client.InfluxDBClient; |
| 28 | +import com.influxdb.v3.client.Point; |
| 29 | +import com.influxdb.v3.client.PointValues; |
| 30 | +import com.influxdb.v3.client.config.ClientConfig; |
| 31 | + |
| 32 | +public final class ProxyExample { |
| 33 | + |
| 34 | + private ProxyExample() { |
| 35 | + } |
| 36 | + |
| 37 | + public static void main(final String[] args) throws Exception { |
| 38 | + // Run docker-compose.yml file to start Envoy proxy |
| 39 | + |
| 40 | + String proxyUrl = "http://localhost:10000"; |
| 41 | + String sslRootsFilePath = "src/test/java/com/influxdb/v3/client/testdata/influxdb-certificate.pem"; |
| 42 | + ClientConfig clientConfig = new ClientConfig.Builder() |
| 43 | + .host(System.getenv("INFLUXDB_URL")) |
| 44 | + .token(System.getenv("INFLUXDB_TOKEN").toCharArray()) |
| 45 | + .database(System.getenv("INFLUXDB_DATABASE")) |
| 46 | + .proxyUrl(proxyUrl) |
| 47 | + .sslRootsFilePath(sslRootsFilePath) |
| 48 | + .build(); |
| 49 | + |
| 50 | + InfluxDBClient influxDBClient = InfluxDBClient.getInstance(clientConfig); |
| 51 | + String testId = UUID.randomUUID().toString(); |
| 52 | + Point point = Point.measurement("My_Home") |
| 53 | + .setTag("room", "Kitchen") |
| 54 | + .setField("temp", 12.7) |
| 55 | + .setField("hum", 37) |
| 56 | + .setField("testId", testId); |
| 57 | + influxDBClient.writePoint(point); |
| 58 | + |
| 59 | + String query = String.format("SELECT * FROM \"My_Home\" WHERE \"testId\" = '%s'", testId); |
| 60 | + try (Stream<PointValues> stream = influxDBClient.queryPoints(query)) { |
| 61 | + stream.findFirst().ifPresent(values -> { |
| 62 | + assert values.getTimestamp() != null; |
| 63 | + System.out.printf("room[%s]: %s, temp: %3.2f, hum: %d", |
| 64 | + new java.util.Date(values.getTimestamp().longValue() / 1000000), |
| 65 | + values.getTag("room"), |
| 66 | + (Double) values.getField("temp"), |
| 67 | + (Long) values.getField("hum")); |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
0 commit comments