Search in sources :

Example 1 with FluxTable

use of com.influxdb.query.FluxTable in project iotdb-benchmark by thulab.

the class InfluxDB method executeQueryAndGetStatus.

private Status executeQueryAndGetStatus(String sql) {
    if (!config.isIS_QUIET_MODE()) {
        LOGGER.debug("{} query SQL: {}", Thread.currentThread().getName(), sql);
    }
    int cnt = 0;
    List<FluxTable> tables = new ArrayList<>();
    try {
        tables = client.getQueryApi().query(sql);
    } catch (Exception e) {
        LOGGER.error("Error when query {} : {}", sql, e.getMessage());
    }
    for (FluxTable table : tables) {
        List<FluxRecord> fluxRecords = table.getRecords();
        cnt += fluxRecords.size();
    }
    return new Status(true, cnt);
}
Also used : Status(cn.edu.tsinghua.iotdb.benchmark.measurement.Status) FluxTable(com.influxdb.query.FluxTable) ArrayList(java.util.ArrayList) TsdbException(cn.edu.tsinghua.iotdb.benchmark.tsdb.TsdbException) FluxRecord(com.influxdb.query.FluxRecord)

Example 2 with FluxTable

use of com.influxdb.query.FluxTable in project nifi-influxdb-bundle by influxdata.

the class ITPutInfluxDatabaseRecord_2 method saveInfluxLineProtocol.

@Test
public void saveInfluxLineProtocol() throws InitializationException {
    String data = "weather,location=us-midwest temperature=82 1465839830100400200";
    InfluxLineProtocolReader readerFactory = new InfluxLineProtocolReader();
    runner.addControllerService("inline-reader", readerFactory);
    runner.setProperty(readerFactory, InfluxLineProtocolReader.CHARSET, StandardCharsets.UTF_8.name());
    runner.enableControllerService(readerFactory);
    runner.setProperty(PutInfluxDatabaseRecord.RECORD_READER_FACTORY, "inline-reader");
    runner.setProperty(InfluxDBUtils.TAGS, "tags");
    runner.setProperty(InfluxDBUtils.FIELDS, "fields");
    runner.setProperty(InfluxDBUtils.TIMESTAMP_FIELD, "timestamp");
    runner.enqueue(data);
    runner.run();
    runner.assertTransferCount(AbstractInfluxDatabaseProcessor.REL_SUCCESS, 1);
    List<FluxTable> tables = queryApi.query("from(bucket:\"" + bucketName + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")");
    Assert.assertEquals(1, tables.size());
    Assert.assertEquals(1, tables.get(0).getRecords().size());
    FluxRecord record = tables.get(0).getRecords().get(0);
    Assert.assertEquals("testRecordMeasurement", record.getMeasurement());
    Assert.assertEquals("us-midwest", record.getValueByKey("location"));
    Assert.assertEquals(82D, record.getValueByKey("temperature"));
    Assert.assertEquals(Instant.ofEpochSecond(0, 1465839830100400200L), record.getTime());
}
Also used : InfluxLineProtocolReader(org.influxdata.nifi.serialization.InfluxLineProtocolReader) FluxTable(com.influxdb.query.FluxTable) FluxRecord(com.influxdb.query.FluxRecord) Test(org.junit.Test)

Example 3 with FluxTable

use of com.influxdb.query.FluxTable in project addons by smarthomej.

the class InfluxDB2RepositoryImpl method getStoredItemsCount.

/**
 * Return all stored item names with it's count of stored points
 *
 * @return Map with <ItemName,ItemCount> entries
 */
@Override
public Map<String, Integer> getStoredItemsCount() {
    final QueryApi currentQueryAPI = queryAPI;
    if (currentQueryAPI != null) {
        Map<String, Integer> result = new LinkedHashMap<>();
        // Query wrote by hand https://github.com/influxdata/influxdb-client-java/issues/75
        String query = "from(bucket: \"" + configuration.getRetentionPolicy() + "\")\n" + "  |> range(start:-365d)\n" + "  |> filter(fn: (r) => exists r." + TAG_ITEM_NAME + " )\n" + "  |> group(columns: [\"" + TAG_ITEM_NAME + "\"], mode:\"by\")\n" + "  |> count()\n" + "  |> group()";
        List<FluxTable> queryResult = currentQueryAPI.query(query);
        Objects.requireNonNull(queryResult.stream().findFirst().orElse(new FluxTable())).getRecords().forEach(row -> {
            result.put((String) row.getValueByKey(TAG_ITEM_NAME), ((Number) row.getValue()).intValue());
        });
        return result;
    } else {
        logger.warn("Returning empty result  because queryAPI isn't present");
        return Collections.emptyMap();
    }
}
Also used : QueryApi(com.influxdb.client.QueryApi) FluxTable(com.influxdb.query.FluxTable) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with FluxTable

use of com.influxdb.query.FluxTable in project IginX by thulab.

the class InfluxDBStorage method executeProjectTask.

private TaskExecuteResult executeProjectTask(TimeInterval timeInterval, TimeSeriesInterval tsInterval, String storageUnit, Project project) {
    Organization organization = client.getOrganizationsApi().findOrganizations().stream().filter(o -> o.getName().equals(this.organization)).findFirst().orElseThrow(IllegalStateException::new);
    if (client.getBucketsApi().findBucketByName(storageUnit) == null) {
        logger.warn("storage engine {} doesn't exist", storageUnit);
        return new TaskExecuteResult(new InfluxDBQueryRowStream(Collections.emptyList()));
    }
    String statement = generateQueryStatement(storageUnit, project.getPatterns(), timeInterval.getStartTime(), timeInterval.getEndTime());
    List<FluxTable> tables = client.getQueryApi().query(statement, organization.getId());
    InfluxDBQueryRowStream rowStream = new InfluxDBQueryRowStream(tables);
    return new TaskExecuteResult(rowStream);
}
Also used : Organization(com.influxdb.client.domain.Organization) FluxTable(com.influxdb.query.FluxTable) TaskExecuteResult(cn.edu.tsinghua.iginx.engine.physical.task.TaskExecuteResult) InfluxDBQueryRowStream(cn.edu.tsinghua.iginx.influxdb.query.entity.InfluxDBQueryRowStream)

Example 5 with FluxTable

use of com.influxdb.query.FluxTable in project IginX by thulab.

the class InfluxDBQueryRowStream method next.

@Override
public Row next() throws PhysicalException {
    long timestamp = Long.MAX_VALUE;
    for (int i = 0; i < this.tables.size(); i++) {
        int index = indices[i];
        FluxTable table = this.tables.get(i);
        List<FluxRecord> records = table.getRecords();
        if (index == records.size()) {
            // 数据已经消费完毕了
            continue;
        }
        FluxRecord record = records.get(index);
        timestamp = Math.min(record.getTime().toEpochMilli(), timestamp);
    }
    if (timestamp == Long.MAX_VALUE) {
        return null;
    }
    Object[] values = new Object[this.tables.size()];
    for (int i = 0; i < this.tables.size(); i++) {
        int index = indices[i];
        FluxTable table = this.tables.get(i);
        List<FluxRecord> records = table.getRecords();
        if (index == records.size()) {
            // 数据已经消费完毕了
            continue;
        }
        FluxRecord record = records.get(index);
        if (record.getTime().toEpochMilli() == timestamp) {
            DataType dataType = header.getField(i).getType();
            Object value = record.getValue();
            if (dataType == DataType.BINARY) {
                value = ((String) value).getBytes();
            }
            values[i] = value;
            indices[i]++;
            if (indices[i] == records.size()) {
                hasMoreRecords--;
            }
        }
    }
    return new Row(header, timestamp, values);
}
Also used : FluxTable(com.influxdb.query.FluxTable) DataType(cn.edu.tsinghua.iginx.thrift.DataType) Row(cn.edu.tsinghua.iginx.engine.shared.data.read.Row) FluxRecord(com.influxdb.query.FluxRecord)

Aggregations

FluxTable (com.influxdb.query.FluxTable)8 FluxRecord (com.influxdb.query.FluxRecord)5 QueryApi (com.influxdb.client.QueryApi)2 LinkedHashMap (java.util.LinkedHashMap)2 Test (org.junit.Test)2 TaskExecuteResult (cn.edu.tsinghua.iginx.engine.physical.task.TaskExecuteResult)1 Row (cn.edu.tsinghua.iginx.engine.shared.data.read.Row)1 InfluxDBQueryRowStream (cn.edu.tsinghua.iginx.influxdb.query.entity.InfluxDBQueryRowStream)1 DataType (cn.edu.tsinghua.iginx.thrift.DataType)1 Status (cn.edu.tsinghua.iotdb.benchmark.measurement.Status)1 TsdbException (cn.edu.tsinghua.iotdb.benchmark.tsdb.TsdbException)1 Organization (com.influxdb.client.domain.Organization)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 InfluxLineProtocolReader (org.influxdata.nifi.serialization.InfluxLineProtocolReader)1