Trying to get a simple influxdb-client-java example to work

I have an InfluxDB 1.8 install running that I have enabled flux for. I have a database named “weather” which has measurements named “conditions” which I’m populating from a local airport feed using Telegraf. From the influx command line I can do use weather, then select * from conditions and see the data I’ve collected.

The tag is called “station_id” and one of the fields is named “wind_string” so I’d like to try a simple POJO query using the influxdb-client-java library.

       // already have a connection above 
       String flux = "from(bucket:\"weather/autogen\") |> range(start: 0) |> filter(fn: (r) => r._measurement == \"conditions\" )";

        QueryApi queryApi = influxDBClient.getQueryApi();
        
        List<Conditions> records = queryApi.query(flux, Conditions.class);
        for (Conditions conditions : records) {
            System.out.println(conditions.time + ": " + conditions.station_id + ": " + conditions.wind_string);
        }

as well as:

    @Measurement(name = "conditions")
    public static class Conditions {

        @Column(tag = true)
        String station_id;
        
        @Column(name = "wind_string")
        String wind_string;

        @Column(timestamp = true)
        Instant time;
        
    }

I know I’m missing something because the time and station_id print fine, but conditions.wind_string is always null.

Can I get a hint?

Hi @sprior,

thanks for using our client.

Try to use a pivot() function:

String flux = "from(bucket:\"weather/autogen\") |> range(start: 0) |> filter(fn: (r) => r._measurement == \"conditions\") |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";

        QueryApi queryApi = influxDBClient.getQueryApi();
        
        List<Conditions> records = queryApi.query(flux, Conditions.class);
        for (Conditions conditions : records) {
            System.out.println(conditions.time + ": " + conditions.station_id + ": " + conditions.wind_string);
        }

Regards

Wow, thanks. I tried that and it worked but I have no clue what’s going on and why a pivot is needed (or what it really is). I guess I’ve got some reading to do. Any good tutorials on that topic? I’m a relational DB programmer and trying out InfluxDB just for fun at home.

@bednar A bit of a follow on. What I was really interested in was the last wind_string, so I modified the flux slightly to be:

String flux = "from(bucket:\"weather/autogen\") |> range(start: 0) |> filter(fn: (r) => r._measurement == \"conditions\") |> last() |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";

And it almost works, but it returns 2 records when I only expected one. Can you suggest the correct form?