|
| 1 | +package scala.bench; |
| 2 | + |
| 3 | +import org.influxdb.InfluxDB; |
| 4 | +import org.influxdb.dto.BatchPoints; |
| 5 | +import org.influxdb.dto.Point; |
| 6 | +import org.influxdb.dto.Query; |
| 7 | +import org.influxdb.dto.QueryResult; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.*; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import static scala.bench.Database.connectDb; |
| 14 | + |
| 15 | +public class DataMigrator { |
| 16 | + |
| 17 | + public static final String JAVA_VERSION_TAG_NAME = "javaVersion"; |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + |
| 21 | + InfluxDB influxDB = connectDb(); |
| 22 | + try { |
| 23 | + BatchPoints batchPoints = BatchPoints |
| 24 | + .database("scala_benchmark") |
| 25 | + .retentionPolicy("autogen") |
| 26 | + .consistency(InfluxDB.ConsistencyLevel.ALL) |
| 27 | + .build(); |
| 28 | + influxDB.enableBatch(100, 10, TimeUnit.MILLISECONDS); |
| 29 | + // > SELECT * INTO result_backup_20210519 FROM result GROUP BY * |
| 30 | + //name: result |
| 31 | + //time written |
| 32 | + //---- ------- |
| 33 | + //0 14076 |
| 34 | + String oldMeasure = "result_backup_20210519"; |
| 35 | + String newMeasure = "result"; |
| 36 | + |
| 37 | + QueryResult queryResult = influxDB.query(new Query("select * from " + oldMeasure + " group by *", "scala_benchmark")); |
| 38 | + for (QueryResult.Result result : queryResult.getResults()) { |
| 39 | + for (QueryResult.Series series : result.getSeries()) { |
| 40 | + List<String> newFieldNames = new ArrayList<>(series.getColumns()); |
| 41 | + int javaVersionIndex = newFieldNames.indexOf(JAVA_VERSION_TAG_NAME); |
| 42 | + newFieldNames.remove(javaVersionIndex); |
| 43 | + Point.Builder builder = Point.measurement(newMeasure); |
| 44 | + Map<String, String> newTags = new HashMap<>(series.getTags()); |
| 45 | + List<Object> newValues = new ArrayList<>(series.getValues().get(0)); |
| 46 | + Object removed = newValues.remove(javaVersionIndex); |
| 47 | + newTags.put(JAVA_VERSION_TAG_NAME, (String) removed); |
| 48 | + newTags.entrySet().removeIf(x -> x.getValue() == null || x.getValue().equals("")); |
| 49 | + builder.tag(newTags); |
| 50 | + LinkedHashMap<String, Object> newFieldsMap = new LinkedHashMap<>(); |
| 51 | + assert (newFieldNames.size() == newValues.size()); |
| 52 | + for (int i = 0; i < newFieldNames.size(); i++) { |
| 53 | + newFieldsMap.put(newFieldNames.get(i), newValues.get(i)); |
| 54 | + } |
| 55 | + builder.fields(newFieldsMap); |
| 56 | + Point point = builder.build(); |
| 57 | + batchPoints.point(point); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + influxDB.write(batchPoints); |
| 62 | + } finally { |
| 63 | + influxDB.close(); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
0 commit comments