JAVA client Library sort() does not return the data sorted

Hi,
I’ve been using the java client library to make write/read queries to influx. However, I found this problem when trying to sort data using the sort() method.

For context, my app is a chatbot that writes recognized messages/responses into the database, which works perfectly fine. On the other hand, I read the data to create visualizations. I do use this flux query to do so:

from(bucket: "xatbot")
    |> range(start: 2018-05-22T23:30:00Z, stop: now())
    |> filter(fn:(r) => r._measurement == "intent" and r.bot_id == "xatkitBot" and r.session_id == "sessionId")
    |> pivot(columnKey: ["_field"], rowKey: ["_time"], valueColumn: "_value")
    |> group()
    |> sort(columns: ["_time"])

This works perfectly fine in the localhost:xxxx instance of influx (the dashboard influxdata provides) and I get the desired data ordered in the desired way (timestamp). Making the same query in the APP does return the data without sorting for some reason, sample output:

02:57:10.588 [pool-3-thread-1] INFO   - ----------------------------------------------------------------------------
02:57:10.589 [pool-3-thread-1] INFO   - 2020-06-04T12:58:07.164Z
02:57:10.589 [pool-3-thread-1] INFO   - 2020-06-04T12:58:03.949Z
02:57:10.589 [pool-3-thread-1] INFO   - ----------------------------------------------------------------------------
02:57:10.590 [pool-3-thread-1] INFO   - 2020-06-18T14:04:14.495Z
02:57:10.590 [pool-3-thread-1] INFO   - 2020-06-18T14:03:45.277Z
02:57:10.591 [pool-3-thread-1] INFO   - 2020-06-18T14:03:47.713Z
02:57:10.591 [pool-3-thread-1] INFO   - ----------------------------------------------------------------------------
02:57:10.591 [pool-3-thread-1] INFO   - 2020-06-11T13:07:24.434Z
02:57:10.592 [pool-3-thread-1] INFO   - 2020-06-11T13:07:22.559Z
02:57:10.592 [pool-3-thread-1] INFO   - ----------------------------------------------------------------------------
02:57:10.592 [pool-3-thread-1] INFO   - 2020-05-26T14:18:01.286Z
02:57:10.593 [pool-3-thread-1] INFO   - 2020-05-26T14:18:16.674Z
02:57:10.593 [pool-3-thread-1] INFO   - ----------------------------------------------------------------------------
02:57:10.593 [pool-3-thread-1] INFO   - 2020-05-25T14:01:55.883Z
02:57:10.593 [pool-3-thread-1] INFO   - ----------------------------------------------------------------------------
02:57:10.594 [pool-3-thread-1] INFO   - 2020-05-25T14:01:39.396Z
02:57:10.594 [pool-3-thread-1] INFO   - 2020-05-25T14:01:50.796Z

The lines with “-----------” means they are different session in my bot, sort should be happening inside each session.

The query in the java app is created this way:

String query = "from(bucket: \"xatbot\")
    |> range(start: 2018-05-22T23:30:00Z, stop: now())
    |> filter(fn:(r) => r._measurement == \"intent\" and r.bot_id == \"xatkitBot\" and r.session_id == \"sessionId\")
    |> pivot(columnKey: [\"_field\"], rowKey: [\"_time\"], valueColumn: \"_value\")
    |> group()
    |> sort(columns: [\"_time\"])" //I make different ones based on session_id field

//retrieves tables:
List<FluxTable> tables = db.getQueryApi().query(query);
List<FluxRecord> sessionData = tables.get(0).getRecords(); //I have only 1 table per query in this scenario.
Log.info("----------------------------------------------------------------------------");
        for (FluxRecord sessionEntry : sessionData) {
            Log.info(String.valueOf(sessionEntry.getValueByKey("_time")));
        }

Hi @Lluis_m ,

thanks for using our client. I tested sorting by next piece of code and everything looks good:

package example;

import java.util.List;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.query.FluxTable;

public class CloudQuery {

    public static void main(final String[] args) {

        String url = "https://us-west-2-1.aws.cloud2.influxdata.com";
        char[] token = "my-token".toCharArray();
        String org = "my-org";
        String bucket = "my-bucket";
        
        InfluxDBClient client = InfluxDBClientFactory.create(url, token, org, bucket);

        String query = "from(bucket: \"github\")"
                + "  |> range(start: 2018-05-22T23:30:00Z, stop: now())"
                + "  |> filter(fn: (r) => r[\"_measurement\"] == \"github_repository\")"
                + "  |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")"
                + "  |> group()\n"
                + "  |> sort(columns: [\"_time\"])\n";

        List<FluxTable> result = client.getQueryApi().query(query);
        result.get(0).getRecords().forEach(fluxRecord -> System.out.println("time = " + fluxRecord.getTime()));

        client.close();
    }
}

Could you also check that the response from InfluxDB server contains correct order of records?

Just enable logging by and see log output:

client.setLogLevel(LogLevel.BODY);

Regards