Search code examples
javadatabaseinfluxdb

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;
    }

Solution

  • 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