org.influxdb.dto.QueryResult.Result Java Examples

The following examples show how to use org.influxdb.dto.QueryResult.Result. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InfluxDbSources.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
void fillBufferWithMeasurementMapping(SourceBuffer<T> sourceBuffer) {
    queue.drainTo(buffer, MAX_FILL_ELEMENTS);
    for (QueryResult result : buffer) {
        boolean done = throwExceptionIfResultWithErrorOrNull(result);
        if (done) {
            break;
        }
        for (Result internalResult : result.getResults()) {
            if (internalResult != null && internalResult.getSeries() != null) {
                for (Series s : internalResult.getSeries()) {
                    for (List<Object> objects : s.getValues()) {
                        sourceBuffer.add(
                                measurementProjection.apply(s.getName(), s.getTags(), s.getColumns(), objects));
                    }
                }
            }
        }
    }
    buffer.clear();
    if (finished && queue.isEmpty()) {
        sourceBuffer.close();
    }
}
 
Example #2
Source File: InfluxDbSources.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
private static boolean throwExceptionIfResultWithErrorOrNull(final QueryResult queryResult) {
    if (queryResult == null) {
        throw new RuntimeException("InfluxDB returned null query result");
    }
    if (queryResult.getResults() == null && "DONE".equals(queryResult.getError())) {
        return true;
    }
    if (queryResult.getError() != null) {
        throw new RuntimeException("InfluxDB returned an error: " + queryResult.getError());
    }
    if (queryResult.getResults() == null) {
        throw new RuntimeException("InfluxDB returned null query results");
    }
    for (Result seriesResult : queryResult.getResults()) {
        if (seriesResult.getError() != null) {
            throw new RuntimeException("InfluxDB returned an error with Series: " + seriesResult.getError());
        }
    }
    return false;
}
 
Example #3
Source File: InfluxDB.java    From iotdb-benchmark with Apache License 2.0 6 votes vote down vote up
private Status executeQueryAndGetStatus(String sql) {
  LOGGER.debug("{} query SQL: {}", Thread.currentThread().getName(), sql);

  QueryResult results = influxDbInstance.query(new Query(sql, influxDbName));
  int cnt = 0;
  for (Result result : results.getResults()) {
    List<Series> series = result.getSeries();
    if (series == null) {
      continue;
    }
    if (result.getError() != null) {
      return new Status(false, cnt, new Exception(result.getError()), sql);
    }
    for (Series serie : series) {
      List<List<Object>> values = serie.getValues();
      cnt += values.size() * (serie.getColumns().size() - 1);
    }
  }

  LOGGER.debug("{} 查到数据点数: {}", Thread.currentThread().getName(), cnt);
  return new Status(true, cnt);
}
 
Example #4
Source File: InfluxDbSinkTest.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
@Test
public void test_influxDbSink() {
    IList<Integer> measurements = jet.getList("mem_usage");
    for (int i = 0; i < VALUE_COUNT; i++) {
        measurements.add(i);
    }

    InfluxDB db = influxdbContainer.getNewInfluxDB();
    db.query(new Query("DROP SERIES FROM mem_usage"));

    Pipeline p = Pipeline.create();

    int startTime = 0;
    p.readFrom(Sources.list(measurements))
     .map(index -> Point.measurement("mem_usage")
                        .time(startTime + index, TimeUnit.MILLISECONDS)
                        .addField("value", index)
                        .build())
     .writeTo(InfluxDbSinks.influxDb(influxdbContainer.getUrl(), DATABASE_NAME, USERNAME, PASSWORD));

    jet.newJob(p).join();

    List<Result> results = db.query(new Query("SELECT * FROM mem_usage")).getResults();
    assertEquals(1, results.size());
    List<Series> seriesList = results.get(0).getSeries();
    assertEquals(1, seriesList.size());
    Series series = seriesList.get(0);
    assertEquals(SERIES, series.getName());
    assertEquals(VALUE_COUNT, series.getValues().size());
}
 
Example #5
Source File: InfluxDataMigrator.java    From RuuviCollector with MIT License 4 votes vote down vote up
@Override
public void accept(QueryResult r) {
    String error = r.getError();
    if (StringUtils.isNotBlank(error)) {
        if (error.equals("DONE")) {
            lock.lock();
            try {
                done.set(true);
            } finally {
                lock.unlock();
            }
        } else {
            LOG.error("Influx returned error: " + error);
        }
        return;
    }
    r.getResults().stream().map(Result::getSeries).filter(Objects::nonNull).flatMap(List::stream).forEach(s -> {
        List<String> columns = s.getColumns();
        int timeIndex = -1;
        int macIndex = -1;
        int sourceIndex = -1;
        int dataFormatIndex = -1;
        int valueIndex = -1;
        int axisIndex = -1;
        for (int i = 0; i < columns.size(); i++) {
            switch (columns.get(i)) {
                case "time":
                    timeIndex = i;
                    break;
                case "mac":
                    macIndex = i;
                    break;
                case "source":
                    sourceIndex = i;
                    break;
                case "dataFormatIndex":
                    dataFormatIndex = i;
                    break;
                case "value":
                    valueIndex = i;
                    break;
                case "axis":
                    axisIndex = i;
                    break;
            }
        }
        for (List v : s.getValues()) {
            LegacyMeasurement m = new LegacyMeasurement();
            String timeString = safeGet(v, timeIndex);
            if (timeString != null) {
                m.time = Instant.from(FORMATTER.parse(timeString)).toEpochMilli();
            }
            m.mac = safeGet(v, macIndex);
            if (m.mac == null) {
                m.mac = safeGet(v, sourceIndex);
            }
            m.dataFormat = safeGet(v, dataFormatIndex);
            m.value = Double.valueOf(safeGet(v, valueIndex));
            try {
                q.put(m);
            } catch (InterruptedException ex) {
                LOG.error("Interrupted", ex);
            }
        }
    });
}