org.influxdb.dto.QueryResult.Series Java Examples

The following examples show how to use org.influxdb.dto.QueryResult.Series. 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: 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 #3
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidScheduleQueryWithNoIncoming() {
    String message = "water,country=US,city=newark rain=1,humidity=0.6 1501002274856668652";
    influxDB.write(dbName, DEFAULT_RETENTION_POLICY, InfluxDB.ConsistencyLevel.ONE, message);

    String query = "select * from water";
    runner.setProperty(ExecuteInfluxDBQuery.INFLUX_DB_QUERY, query);

    runner.setIncomingConnection(false);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertNull("Value should be null", flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query, flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));

    QueryResult queryResult = gson.fromJson(new StringReader(new String(flowFiles.get(0).toByteArray())), QueryResult.class);
    Series series = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series.getName(), series.getColumns(), series.getValues().get(0),"newark",1.0);
}
 
Example #4
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidSinglePoint() {
    String message = "water,country=US,city=newark rain=1,humidity=0.6 1501002274856668652";
    influxDB.write(dbName, DEFAULT_RETENTION_POLICY, InfluxDB.ConsistencyLevel.ONE, message);
    String query = "select * from water";
    byte [] bytes = query.getBytes();
    runner.enqueue(bytes);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertNull("Value should be null", flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query, flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));

    QueryResult queryResult = gson.fromJson(new StringReader(new String(flowFiles.get(0).toByteArray())), QueryResult.class);
    Series series = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series.getName(), series.getColumns(), series.getValues().get(0),"newark",1.0);
}
 
Example #5
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testShowDatabases() {
    String query = "show databases";
    byte [] bytes = query.getBytes();
    runner.enqueue(bytes);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertNull("Value should be null", flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query, flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));

    String result = new String(flowFiles.get(0).toByteArray());
    QueryResult queryResult = gson.fromJson(new StringReader(result), QueryResult.class);
    Series series = queryResult.getResults().get(0).getSeries().get(0);
    assertEquals("series name should be same", "databases", series.getName());
    assertEquals("series column should be same", "name", series.getColumns().get(0));
    boolean internal = series.getValues().get(0).stream().anyMatch(o -> o.equals("_internal"));
    Assert.assertTrue("content should contain _internal " + queryResult, internal);
    boolean test = series.getValues().stream().flatMap(i -> ((List<Object>)i).stream()).anyMatch(o -> o.equals("test"));
    Assert.assertTrue("content should contain test " + queryResult, test);
}
 
Example #6
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyFlowFileQueryWithScheduledQuery() {
    String message = "water,country=US,city=newark rain=1,humidity=0.6 1501002274856668652";
    influxDB.write(dbName, DEFAULT_RETENTION_POLICY, InfluxDB.ConsistencyLevel.ONE, message);

    String query = "select * from water";
    runner.setProperty(ExecuteInfluxDBQuery.INFLUX_DB_QUERY, query);

    byte [] bytes = new byte [] {};
    runner.enqueue(bytes);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);

    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertEquals("Value should be equal",null, flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query, flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));

    QueryResult queryResult = gson.fromJson(new StringReader(new String(flowFiles.get(0).toByteArray())), QueryResult.class);
    assertNotNull("QueryResult should not be null", queryResult.getResults());
    assertEquals("results array should be same size", 1, queryResult.getResults().size());
    Series series = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series.getName(), series.getColumns(), series.getValues().get(0),"newark",1.0);
}
 
Example #7
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyFlowFileQueryWithScheduledQueryEL() {
    String message = "water,country=US,city=newark rain=1,humidity=0.6 1501002274856668652";
    influxDB.write(dbName, DEFAULT_RETENTION_POLICY, InfluxDB.ConsistencyLevel.ONE, message);

    String query = "select * from ${measurement}";
    runner.setProperty(ExecuteInfluxDBQuery.INFLUX_DB_QUERY, query);

    byte [] bytes = new byte [] {};
    Map<String,String> properties = new HashMap<>();
    properties.put("measurement","water");
    runner.enqueue(bytes, properties);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);

    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertNull("Value should be null",flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query.replace("${measurement}", "water"), flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));

    QueryResult queryResult = gson.fromJson(new StringReader(new String(flowFiles.get(0).toByteArray())), QueryResult.class);
    assertNotNull("QueryResult should not be null", queryResult.getResults());
    assertEquals("results array should be same size", 1, queryResult.getResults().size());
    Series series = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series.getName(), series.getColumns(), series.getValues().get(0),"newark",1.0);
}
 
Example #8
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidTwoPoints() {
    String message = "water,country=US,city=newark rain=1,humidity=0.6 1501002274856668652" +
        System.lineSeparator() +
        "water,country=US,city=nyc rain=2,humidity=0.6 1501002274856668652";
    influxDB.write(dbName, DEFAULT_RETENTION_POLICY, InfluxDB.ConsistencyLevel.ONE, message);
    String query = "select * from water";
    byte [] bytes = query.getBytes();
    runner.enqueue(bytes);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertNull("Value should be null", flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query, flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));
    QueryResult queryResult = gson.fromJson(new StringReader(new String(flowFiles.get(0).toByteArray())), QueryResult.class);
    assertNotNull("QueryResult should not be null", queryResult.getResults());
    assertEquals("results array should be same size", 1, queryResult.getResults().size());
    assertEquals("Series size should be same",1, queryResult.getResults().get(0).getSeries().size());
    Series series1 = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series1.getName(),series1.getColumns(), series1.getValues().get(0),"newark",1.0);

    Series series2 = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series2.getName(),series2.getColumns(), series2.getValues().get(1),"nyc",2.0);
}
 
Example #9
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidSameTwoPoints() {
    String message = "water,country=US,city=nyc rain=1,humidity=0.6 1501002274856668652" +
        System.lineSeparator() +
        "water,country=US,city=nyc rain=1,humidity=0.6 1501002274856668652";
    influxDB.write(dbName, DEFAULT_RETENTION_POLICY, InfluxDB.ConsistencyLevel.ONE, message);
    String query = "select * from water";
    byte [] bytes = query.getBytes();
    runner.enqueue(bytes);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertNull("Value should be null", flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query, flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));

    QueryResult queryResult = gson.fromJson(new StringReader(new String(flowFiles.get(0).toByteArray())), QueryResult.class);
    assertNotNull("QueryResult should not be null", queryResult.getResults());
    assertEquals("Result size should be same", 1, queryResult.getResults().size());
    Series series = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series.getName(), series.getColumns(), series.getValues().get(0),"nyc",1.0);
}
 
Example #10
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 #11
Source File: InfluxDBTest.java    From influxdb-java with MIT License 4 votes vote down vote up
@Test
public void testBoundParameterQuery() throws InterruptedException {
  // set up
  Point point = Point
      .measurement("cpu")
      .tag("atag", "test")
      .addField("idle", 90L)
      .addField("usertime", 9L)
      .addField("system", 1L)
      .build();
  this.influxDB.setDatabase(UDP_DATABASE);
  this.influxDB.write(point);

  // test
  Query query = QueryBuilder.newQuery("SELECT * FROM cpu WHERE atag = $atag")
      .forDatabase(UDP_DATABASE)
      .bind("atag", "test")
      .create();
  QueryResult result = this.influxDB.query(query);
  Assertions.assertTrue(result.getResults().get(0).getSeries().size() == 1);
  Series series = result.getResults().get(0).getSeries().get(0);
  Assertions.assertTrue(series.getValues().size() == 1);

  result = this.influxDB.query(query, TimeUnit.SECONDS);
  Assertions.assertTrue(result.getResults().get(0).getSeries().size() == 1);
  series = result.getResults().get(0).getSeries().get(0);
  Assertions.assertTrue(series.getValues().size() == 1);

  Object waitForTestresults = new Object();
  Consumer<QueryResult> check = (queryResult) -> {
    if (!"DONE".equals(queryResult.getError())) {
      Assertions.assertTrue(queryResult.getResults().get(0).getSeries().size() == 1);
      Series s = queryResult.getResults().get(0).getSeries().get(0);
      Assertions.assertTrue(s.getValues().size() == 1);
      synchronized (waitForTestresults) {
        waitForTestresults.notifyAll();
      }
    }
  };
  this.influxDB.query(query, 10, check);
  synchronized (waitForTestresults) {
    waitForTestresults.wait(2000);
  }
}