|
| 1 | +package com.baeldung.influxdb; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.influxdb.InfluxDB; |
| 5 | +import org.influxdb.InfluxDBFactory; |
| 6 | +import org.influxdb.InfluxDBIOException; |
| 7 | +import org.influxdb.dto.*; |
| 8 | +import org.influxdb.impl.InfluxDBResultMapper; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +import static junit.framework.TestCase.assertEquals; |
| 16 | +import static org.junit.Assert.assertFalse; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +public class InfluxDBConnectionLiveTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void whenCorrectInfoDatabaseConnects() { |
| 24 | + |
| 25 | + InfluxDB connection = connectDatabase(); |
| 26 | + assertTrue(pingServer(connection)); |
| 27 | + } |
| 28 | + |
| 29 | + private InfluxDB connectDatabase() { |
| 30 | + |
| 31 | + // Connect to database assumed on localhost with default credentials. |
| 32 | + return InfluxDBFactory.connect("http://127.0.0.1:8086", "admin", "admin"); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + private boolean pingServer(InfluxDB influxDB) { |
| 37 | + try { |
| 38 | + // Ping and check for version string |
| 39 | + Pong response = influxDB.ping(); |
| 40 | + if (response.getVersion().equalsIgnoreCase("unknown")) { |
| 41 | + log.error("Error pinging server."); |
| 42 | + return false; |
| 43 | + } else { |
| 44 | + log.info("Database version: {}", response.getVersion()); |
| 45 | + return true; |
| 46 | + } |
| 47 | + } catch (InfluxDBIOException idbo) { |
| 48 | + log.error("Exception while pinging database: ", idbo); |
| 49 | + return false; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenDatabaseCreatedDatabaseChecksOk() { |
| 55 | + |
| 56 | + InfluxDB connection = connectDatabase(); |
| 57 | + |
| 58 | + // Create "baeldung and check for it |
| 59 | + connection.createDatabase("baeldung"); |
| 60 | + assertTrue(connection.databaseExists("baeldung")); |
| 61 | + |
| 62 | + // Verify that nonsense databases are not there |
| 63 | + assertFalse(connection.databaseExists("foobar")); |
| 64 | + |
| 65 | + // Drop "baeldung" and check again |
| 66 | + connection.deleteDatabase("baeldung"); |
| 67 | + assertFalse(connection.databaseExists("baeldung")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenPointsWrittenPointsExists() throws Exception { |
| 72 | + |
| 73 | + InfluxDB connection = connectDatabase(); |
| 74 | + |
| 75 | + String dbName = "baeldung"; |
| 76 | + connection.createDatabase(dbName); |
| 77 | + |
| 78 | + // Need a retention policy before we can proceed |
| 79 | + connection.createRetentionPolicy("defaultPolicy", "baeldung", "30d", 1, true); |
| 80 | + |
| 81 | + // Since we are doing a batch thread, we need to set this as a default |
| 82 | + connection.setRetentionPolicy("defaultPolicy"); |
| 83 | + |
| 84 | + // Enable batch mode |
| 85 | + connection.enableBatch(10, 10, TimeUnit.MILLISECONDS); |
| 86 | + |
| 87 | + for (int i = 0; i < 10; i++) { |
| 88 | + Point point = Point.measurement("memory") |
| 89 | + .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) |
| 90 | + .addField("name", "server1") |
| 91 | + .addField("free", 4743656L) |
| 92 | + .addField("used", 1015096L) |
| 93 | + .addField("buffer", 1010467L) |
| 94 | + .build(); |
| 95 | + |
| 96 | + connection.write(dbName, "defaultPolicy", point); |
| 97 | + Thread.sleep(2); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + // Unfortunately, the sleep inside the loop doesn't always add enough time to insure |
| 102 | + // that Influx's batch thread flushes all of the writes and this sometimes fails without |
| 103 | + // another brief pause. |
| 104 | + Thread.sleep(10); |
| 105 | + |
| 106 | + List<MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung"); |
| 107 | + |
| 108 | + assertEquals(10, memoryPointList.size()); |
| 109 | + |
| 110 | + // Turn off batch and clean up |
| 111 | + connection.disableBatch(); |
| 112 | + connection.deleteDatabase("baeldung"); |
| 113 | + connection.close(); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + private List<MemoryPoint> getPoints(InfluxDB connection, String query, String databaseName) { |
| 118 | + |
| 119 | + // Run the query |
| 120 | + Query queryObject = new Query(query, databaseName); |
| 121 | + QueryResult queryResult = connection.query(queryObject); |
| 122 | + |
| 123 | + // Map it |
| 124 | + InfluxDBResultMapper resultMapper = new InfluxDBResultMapper(); |
| 125 | + return resultMapper.toPOJO(queryResult, MemoryPoint.class); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + @Test |
| 131 | + public void whenBatchWrittenBatchExists() { |
| 132 | + |
| 133 | + InfluxDB connection = connectDatabase(); |
| 134 | + |
| 135 | + String dbName = "baeldung"; |
| 136 | + connection.createDatabase(dbName); |
| 137 | + |
| 138 | + // Need a retention policy before we can proceed |
| 139 | + // Since we are doing batches, we need not set it |
| 140 | + connection.createRetentionPolicy("defaultPolicy", "baeldung", "30d", 1, true); |
| 141 | + |
| 142 | + |
| 143 | + BatchPoints batchPoints = BatchPoints |
| 144 | + .database(dbName) |
| 145 | + .retentionPolicy("defaultPolicy") |
| 146 | + .build(); |
| 147 | + Point point1 = Point.measurement("memory") |
| 148 | + .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) |
| 149 | + .addField("free", 4743656L) |
| 150 | + .addField("used", 1015096L) |
| 151 | + .addField("buffer", 1010467L) |
| 152 | + .build(); |
| 153 | + Point point2 = Point.measurement("memory") |
| 154 | + .time(System.currentTimeMillis() - 100, TimeUnit.MILLISECONDS) |
| 155 | + .addField("free", 4743696L) |
| 156 | + .addField("used", 1016096L) |
| 157 | + .addField("buffer", 1008467L) |
| 158 | + .build(); |
| 159 | + batchPoints.point(point1); |
| 160 | + batchPoints.point(point2); |
| 161 | + connection.write(batchPoints); |
| 162 | + |
| 163 | + List<MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung"); |
| 164 | + |
| 165 | + assertEquals(2, memoryPointList.size()); |
| 166 | + assertTrue(4743696L == memoryPointList.get(0).getFree()); |
| 167 | + |
| 168 | + |
| 169 | + memoryPointList = getPoints(connection, "Select * from memory order by time desc", "baeldung"); |
| 170 | + |
| 171 | + assertEquals(2, memoryPointList.size()); |
| 172 | + assertTrue(4743656L == memoryPointList.get(0).getFree()); |
| 173 | + |
| 174 | + // Clean up database |
| 175 | + connection.deleteDatabase("baeldung"); |
| 176 | + connection.close(); |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments