Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InfluxDB how to get the last recorded values

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;
    }
like image 561
Egor Vasilyev Avatar asked Jul 23 '18 12:07

Egor Vasilyev


People also ask

How do I get data from InfluxDB?

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 .

Does InfluxDB have tables?

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.

How does influx database store data?

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.

What is timestamp in InfluxDB?

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.


1 Answers

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
like image 121
m.ghoreshi Avatar answered Oct 12 '22 03:10

m.ghoreshi