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
| public void insert(String table, List<Map<String, Object>> rows) { if (CollectionUtils.isEmpty(rows)) { return; } List<Point> points = rows.stream() .filter(e -> !CollectionUtils.isEmpty(e)) .map(e -> { return to(table, e);
}) .collect(Collectors.toList()); if (CollectionUtils.isEmpty(points)) { return; } BatchPoints batchPoints = BatchPoints.builder() .points(points) .build(); influxDB.write(batchPoints); influxDB.flush(); }
private Point to(String table, Map<String, Object> row) { Point.Builder builder = Point.measurement(table).time(getTime(row), TimeUnit.MILLISECONDS); row.remove("time"); row.forEach((k, v) -> { if (v == null) { return; } if (StringUtils.startsWith(k, "_")) { String key = StringUtils.removeStart(k, "_"); if (v.getClass().getName().equals(boolean.class.getName())) { builder.addField(key, (boolean) v); } else if (v.getClass().getName().equals(short.class.getName())) { builder.addField(key, (short) v); } else if (v.getClass().getName().equals(int.class.getName())) { builder.addField(key, (int) v); } else if (v.getClass().getName().equals(long.class.getName())) { builder.addField(key, (long) v); } else if (v.getClass().getName().equals(float.class.getName())) { builder.addField(key, (float) v); } else if (v.getClass().getName().equals(double.class.getName())) { builder.addField(key, (double) v); } else if (v instanceof Boolean) { builder.addField(key, (Boolean) v); } else if (v instanceof Number) { builder.addField(key, (Number) v); } else if (v instanceof String) { builder.addField(key, (String) v); } else { builder.addField(key, v.toString()); } } else { builder.tag(k, v.toString()); } }); return builder.build(); } public Point build() { Preconditions.checkNonEmptyString(this.measurement, "measurement"); Preconditions.checkPositiveNumber(this.fields.size(), "fields size"); Point point = new Point(); point.setFields(this.fields); point.setMeasurement(this.measurement); if (this.time != null) { point.setTime(this.time); point.setPrecision(this.precision); }
point.setTags(this.tags); return point; }
|