Use Java and the Flight SQL package to query data
Use Java and the Flight SQL package to query data stored in an InfluxDB Cloud Serverless bucket.
- Get started using Java to query InfluxDB
- Troubleshoot Arrow Flight requests
Get started using Java to query InfluxDB
Write a Java class for a Flight SQL client that connects to InfluxDB Cloud Serverless, executes an SQL query, and retrieves data stored in an InfluxDB Cloud Serverless bucket.
The example uses the Apache Arrow Java implementation (
org.apache.arrow) for interacting with Flight database servers like InfluxDB v3.
org.apache.arrow: Provides classes and methods for integrating Java applications with Apache Arrow data and protocols.
org.apache.arrow.flight.sql: Provides classes and methods for interacting with Flight database servers using Arrow Flight RPC and Flight SQL.
- Set up InfluxDB
- Install prerequisites
- Create the FlightQuery class
- Create a query client
- Execute a query
- Retrieve and process Arrow data
To clone or download the example application that you can run with Docker, see the InfluxCommunity/ArrowFlightClient_Query_Examples repo on GitHub.
Set up InfluxDB
To configure the application for querying InfluxDB Cloud Serverless, you’ll need the following InfluxDB resources:
- InfluxDB Cloud Serverless bucket
- InfluxDB Cloud Serverless API token with read permission to the bucket
If you don’t already have an API token and a bucket, see how to set up InfluxDB. If you don’t already have data to query, see how to write data to a bucket.
We recommend using Docker and Maven for building and running the Java application and avoiding platform-specific dependency problems.
Dockerfile installs compatible versions of Maven
and Java JDK in the Docker container, and then runs the Maven commands to download dependencies and compile the application.
Follow the instructions to download and install Docker for your system:
- macOS: Install Docker for macOS
- Linux: Install Docker for Linux
View the Dockerfile
View the Maven pom.xml
Create the FlightQuery class
<PROJECT_ROOT>/src/main/javadirectory, create the
com/influxdb/examplessubdirectories for the
examplesdirectory from the preceding step, create the
FlightQuery.javaclass file. You should have the following directory structure:
PROJECT_ROOT └──src └──main └──java └──com └──influxdb └──examples └──FlightQuery.java
Add the package name:
importstatements for the following packages. You’ll use classes and methods from these packages in the remaining steps:
Define constants for server credentials.
Dockerfiledefines environment variables for these credentials.
Create a query client
FlightQuery.main() method, do the following to create an SQL client that can connect to
Construct a gRPC+TLS channel URI with
443for communicating with a gRPC server over TLS.
FlightClientMiddlewareand define an event callback that inserts the following Flight request metadata header property:
BufferAllocatorthat sets the memory allowed for the client.
FlightClientwith the allocator and gRPC channel.
FlightSqlClientthat wraps the
Execute a query
TOKENas a bearer credential. The result is a credential object that you’ll pass in each request to the server.
Define a string that contains the SQL query to execute–for example:
String query = "SELECT * FROM home";
FlightSqlClient.executemethod with the SQL query and the
If successful, the
FlightSqlClient.executemethod responds with a
FlightInfoobject that contains metadata and an
endpoints: [...]list. Each endpoint contains the following:
- A list of addresses where you can retrieve the data.
ticketvalue that identifies the data to retrieve.
Extract the ticket from the response.
Retrieve and process Arrow data
FlightQuery.main() method, do the following to retrieve the data stream described in the
FlightSqlClient.getStreammethod with the ticket and the
CredentialCallOptionto fetch the Arrow stream.
FlightStream.getRootmethod to get the current vector data from the stream.
Process the data and handle exceptions. The example converts the vector data into tab-separated values and prints the result to
For more examples using Java to work with Arrow data, see the Apache Arrow Java Cookbook.
Finally, close the stream and client.
Run the application
Follow these steps to build and run the application using Docker:
pom.xmlto your project root directory.
Open a terminal in your project root directory.
In your terminal, run the
docker buildcommand and pass
--build-argflags for the server credentials:
DATABASE_NAME: your InfluxDB Cloud Serverless bucket
HOST: your InfluxDB Cloud Serverless region hostname (URL without the “https://”)
TOKEN: your InfluxDB Cloud Serverless API token with read permission to the bucket
docker build \ --build-arg DATABASE_NAME=INFLUX_BUCKET \ --build-arg HOST=cloud2.influxdata.com \ --build-arg TOKEN=INFLUX_TOKEN \ -t javaflight .
The command builds a Docker image named
To run the application in a new Docker container, enter the following command:
docker run javaflight
The output is the query data in TSV-format.
Troubleshoot Arrow Flight requests
For the list of Arrow Flight error response codes, see the Arrow Flight RPC documentation.
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support, use the following resources:
InfluxDB Cloud and InfluxDB Enterprise customers can contact InfluxData Support.