use of com.influxdb.query.FluxTable in project oner365-cloud by xiaozhao32.
the class InfluxdbTest method queryData.
/**
* 查询
*/
public static void queryData(InfluxDBClient client, String bucket, String org) {
String query = String.format("from(bucket: \"%s\") |> range(start: 0) |> filter(fn: (r) => r._measurement == \"%s\")", bucket, "mem");
List<FluxTable> tables = client.getQueryApi().query(query, org);
for (FluxTable table : tables) {
for (FluxRecord record : table.getRecords()) {
LOGGER.info("Table:{}, Measurement:{}, Field:{}, Value:{}", record.getTable(), record.getMeasurement(), record.getField(), record.getValue());
}
}
}
use of com.influxdb.query.FluxTable in project openhab-addons by openhab.
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);
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 nifi-influxdb-bundle by influxdata.
the class ITPutInfluxDatabaseRecord_2 method storeComplexData.
@Test
public void storeComplexData() {
runner.setProperty(InfluxDBUtils.TAGS, "lang,keyword");
runner.setProperty(InfluxDBUtils.TIMESTAMP_FIELD, "timestamp");
runner.setProperty(InfluxDBUtils.FIELDS, "retweet_count,tweet_id,followers_count,screen_name,friends_count,favourites_count,user_verified,raw");
recordReader.addSchemaField("lang", RecordFieldType.STRING);
recordReader.addSchemaField("keyword", RecordFieldType.STRING);
recordReader.addSchemaField("retweet_count", RecordFieldType.INT);
recordReader.addSchemaField("tweet_id", RecordFieldType.STRING);
recordReader.addSchemaField("followers_count", RecordFieldType.INT);
recordReader.addSchemaField("screen_name", RecordFieldType.STRING);
recordReader.addSchemaField("friends_count", RecordFieldType.INT);
recordReader.addSchemaField("favourites_count", RecordFieldType.INT);
recordReader.addSchemaField("user_verified", RecordFieldType.BOOLEAN);
recordReader.addSchemaField("raw", RecordFieldType.STRING);
recordReader.addSchemaField("timestamp", RecordFieldType.LONG);
Date timeValue = new Date();
recordReader.addRecord("en", "crypto", 10, "123456789id", 15, "my name", 5, 20, false, "crypto rules!", timeValue);
runner.enqueue("");
runner.run();
runner.assertAllFlowFilesTransferred(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("en", record.getValueByKey("lang"));
Assert.assertEquals("crypto", record.getValueByKey("keyword"));
Assert.assertEquals("123456789id", record.getValueByKey("tweet_id"));
Assert.assertEquals(15L, record.getValueByKey("followers_count"));
Assert.assertEquals("my name", record.getValueByKey("screen_name"));
Assert.assertEquals(5L, record.getValueByKey("friends_count"));
Assert.assertEquals(20L, record.getValueByKey("favourites_count"));
Assert.assertEquals(false, record.getValueByKey("user_verified"));
long millis = TimeUnit.MILLISECONDS.convert(timeValue.getTime(), TimeUnit.MILLISECONDS);
Assert.assertEquals(Instant.ofEpochMilli(millis), record.getTime());
}
Aggregations