There is an Inflix DB database in which values are written. How do I get the last recorded data (for example, protocol number and time in long) for a device named "test"? I tried to get it through query
, but it returns only one point, although there are about 40 of them in the database.
Below is a sample code.
public static long getTime( String devName ) {
InfluxDB influxDB = InfluxDBFactory.connect("http://10.10.1.72:8086");
String sql = "select time, protocol from device";
Query query = new Query( sql, dbName );
QueryResult result = influxDB.query(query);
Series series = result.getResults().get(0).getSeries().get(0);
//long time = (long)(double)series.getValues().get(0).get(0);
System.out.println(series.getColumns().get(0) + "=" + series.getValues().get(0).get(0) + " " + series.getValues().get(0).get(0).getClass().getName());
System.out.println(series.getColumns().get(1) + "=" + series.getValues().get(0).get(1) + " " + series.getValues().get(0).get(1).getClass().getName());
return 0;
}
To perform a query send a GET request to the /query endpoint, set the URL parameter db as the target database, and set the URL parameter q as your query. You may also use a POST request by sending the same parameters either as URL parameters or as part of the body with application/x-www-form-urlencoded .
Table behavior. The table visualization renders queried data in structured, easy-to-read tables. Columns and rows match those in the query output. If query results contain multiple tables, only one table is shown at a time.
InfluxDB stores data in shard groups. Shard groups are organized by retention policy (RP) and store data with timestamps that fall within a specific time interval called the shard duration. The shard group duration is also configurable per RP. To configure the shard group duration, see Retention Policy Management.
Timestamps are UNIX timestamps. The minimum valid timestamp is -9223372036854775806 or 1677-09-21T00:12:43.145224194Z . The maximum valid timestamp is 9223372036854775806 or 2262-04-11T23:47:16.854775806Z . As mentioned above, by default, InfluxDB assumes that timestamps have nanosecond precision.
You can get the last item using last
function:
SELECT last(protocol) FROM device [optional: WHERE <your condition>]
or you can use LIMIT
keyword (note that, we should use descending order):
SELECT * FROM device ORDER BY desc LIMIT 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With