Flux import for java client library

I am using the java client library, and I am trying to figure out how build a Flux query in the JAVA client library to import schema so that I can use schema.fieldsAsCols() like in the below Flux query:

import "influxdata/influxdb/schema"
from(bucket:"mybucket")
        |> range(start:-1w)
        |> filter(fn: (r) => r["_measurement"] == "mymeasurement")
         |> filter(fn: (r) => r["mytag"] == "abc")
         |> filter(fn: (r) => r["mytag2"] == "def")
        |> schema.fieldsAsCols()
        |> filter(fn: (r) => r["field2"] == "xyz")
        |> keep(columns: ["_time", "field1", "field2", "field3"])

Hello @stanton009,
As per this documentation:

You’ll do something like:

package example;

import java.time.Instant;
import java.util.List;

import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.QueryApi;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;

public class InfluxDB2Example {

    private static char[] token = "my-token".toCharArray();
    private static String org = "my-org";
    private static String bucket = "my-bucket";

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

        InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token, org, bucket);

String flux = "from(bucket:\"my-bucket\") |> range(start: 0)";

        QueryApi queryApi = influxDBClient.getQueryApi();

        List<FluxTable> tables = queryApi.query(flux);
        for (FluxTable fluxTable : tables) {
            List<FluxRecord> records = fluxTable.getRecords();
            for (FluxRecord fluxRecord : records) {
                System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));
            }
        }

        influxDBClient.close();
    }

Where you’ll input your query into the flux string. Just make sure to escape the double quotes successfully like:

String flux = "import \"influxdata/influxdb/schema\"  from(bucket:\"mybucket\") |> range(start: -1w)...";

Excuse the bad indents please.

1 Like