Created
December 13, 2020 16:48
-
-
Save marcoberri/6e4a01a8a8fa63966e8307fe8eef045e to your computer and use it in GitHub Desktop.
Sample import data into influx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.marcoberri.meteo; | |
import com.influxdb.annotations.Column; | |
import com.influxdb.annotations.Measurement; | |
import com.influxdb.client.InfluxDBClient; | |
import com.influxdb.client.InfluxDBClientFactory; | |
import com.influxdb.client.WriteApi; | |
import com.influxdb.client.domain.WritePrecision; | |
import com.influxdb.client.write.Point; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.commons.lang3.StringUtils; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.time.Instant; | |
import java.util.List; | |
/** | |
* var result = { | |
* 'ts': new Date(tmpTs), | |
* 'tsMillis' : tmpTs, | |
* 'T0': parseFloat(splitted[1]), | |
* 'H0': parseFloat(splitted[2]), | |
* 'T1': parseFloat(splitted[3]), | |
* 'H1': parseFloat(splitted[4]), | |
* 'PRESS': parseFloat(splitted[13]), | |
* 'FC':parseFloat(splitted[15]), | |
* 'STORM': parseFloat(splitted[16]), | |
* 'WD': parseFloat(splitted[17]), | |
* 'WS': parseFloat(splitted[18]), | |
* 'WG': parseFloat(splitted[19]), | |
* 'WC': parseFloat(splitted[20]), | |
* 'RC': parseFloat(splitted[21]) | |
*/ | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
String database = "meteo"; | |
String retentionPolicy = "autogen"; | |
InfluxDBClient client = InfluxDBClientFactory.createV1("http://192.168.118.128:8086", | |
"username", | |
"password".toCharArray(), | |
database, | |
retentionPolicy); | |
System.out.println("client.isGzipEnabled(): " + client.isGzipEnabled()); | |
System.out.println("file in folder:" + Utils.getResourceFiles("/data/")); | |
try (WriteApi writeApi = client.getWriteApi()) { | |
for (String filename : Utils.getResourceFiles("/data/")) { | |
ClassLoader classloader = Thread.currentThread().getContextClassLoader(); | |
InputStream is = classloader.getResourceAsStream("data/" + filename); | |
List<String> fileLines = IOUtils.readLines(is); | |
for (String line : fileLines) { | |
if (StringUtils.isEmpty(line)) | |
continue; | |
try { | |
String[] splitted = line.split(":"); | |
Instant instant = Instant.ofEpochSecond(Integer.parseInt(splitted[0])); | |
Point point = Point.measurement("raw") | |
.time(instant, WritePrecision.MS) | |
.addField("T0", Float.parseFloat(splitted[1])) | |
.addField("H0", Float.parseFloat(splitted[2])) | |
.addField("T1", Float.parseFloat(splitted[3])) | |
.addField("H1", Float.parseFloat(splitted[4])) | |
.addField("FC", Float.parseFloat(splitted[15])) | |
.addField("STORM", Float.parseFloat(splitted[16])) | |
.addField("WD", Float.parseFloat(splitted[17])) | |
.addField("WS", Float.parseFloat(splitted[18])) | |
.addField("WG", Float.parseFloat(splitted[19])) | |
.addField("WC", Float.parseFloat(splitted[20])) | |
.addField("RC", Float.parseFloat(splitted[21])) | |
.addField("PRESS", Float.parseFloat(splitted[13])); | |
System.out.println(point.toLineProtocol()); | |
writeApi.writePoint(point); | |
} catch (Exception e) { | |
System.out.println("Error read line:" + e); | |
} | |
} | |
} | |
} | |
client.close(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment