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);
}
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());
}
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();
}
}
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);
}
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);
}
Aggregations