Java Flight SQL package
Apache Arrow Flight SQL for Java integrates with Java applications to query and retrieve data from Flight database servers using RPC and SQL.
Use InfluxDB 3 client libraries
We recommend using the influxdb3-java Go client library for integrating InfluxDB 3 with your Java application code.
InfluxDB 3 client libraries wrap Apache Arrow Flight clients and provide convenient methods for writing, querying, and processing data stored in InfluxDB Clustered. Client libraries can query using SQL or InfluxQL.
Get started using the Java Flight SQL client to query InfluxDB
Write a Java class for a Flight SQL client that connects to InfluxDB Clustered, executes an SQL query, and retrieves data stored in an InfluxDB Clustered database.
The example uses the Apache Arrow Java implementation (org.apache.arrow) for interacting with Flight database servers like InfluxDB 3.
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 repository on GitHub.
Set up InfluxDB
To configure the application for querying InfluxDB Clustered, youβll need the following InfluxDB resources:
- InfluxDB Clustered database
- InfluxDB Clustered database token with read permission to the database
If you donβt already have a database token and a database, see how to set up InfluxDB. If you donβt already have data to query, see how to write data to a database.
Install prerequisites
The following uses Docker and Maven to build and run the Java application and avoid platform-specific dependency problems.
The example 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
Create the FlightQuery class
In your
<PROJECT_ROOT>/src/main/javadirectory, create thecom/influxdb/examplessubdirectories for thecom.influxdb.examplespackage.In the
examplesdirectory from the preceding step, create theFlightQuery.javaclass file. You should have the following directory structure:PROJECT_ROOT βββsrc βββmain βββjava βββcom βββinfluxdb βββexamples βββFlightQuery.javaIn
FlightQuery.java:Add the package name:
package com.influxdb.examples;Add
importstatements for the following packages. Youβll use classes and methods from these packages in the remaining steps:org.apache.arrow.flight.auth2.BearerCredentialWriterorg.apache.arrow.flight.CallHeadersorg.apache.arrow.flight.CallStatusorg.apache.arrow.flight.grpc.CredentialCallOptionorg.apache.arrow.flight.Locationorg.apache.arrow.flight.FlightClientorg.apache.arrow.flight.FlightClientMiddlewareorg.apache.arrow.flight.FlightInfoorg.apache.arrow.flight.FlightStreamorg.apache.arrow.flight.sql.FlightSqlClientorg.apache.arrow.flight.Ticketorg.apache.arrow.memory.BufferAllocatororg.apache.arrow.memory.RootAllocatororg.apache.arrow.vector.VectorSchemaRoot
Create a
FlightQueryclass.In the
FlightQueryclass:Define constants for server credentials.
DATABASE_NAMEHOSTTOKEN
The example
Dockerfiledefines environment variables for these credentials.Create a
main()method.
Create a query client
In the FlightQuery.main() method, do the following to create an SQL client that can connect to HOST and DATABASE_NAME:
Construct a gRPC+TLS channel URI with
HOSTand port443for communicating with a gRPC server over TLS.Instantiate
FlightClientMiddlewareand define an event callback that inserts the following Flight request metadata header property:"database": "DATABASE_NAME"Instantiate a
BufferAllocatorthat sets the memory allowed for the client.Create a
FlightClientwith the allocator and gRPC channel.Instantiate a
FlightSqlClientthat wraps theFlightClientinstance.
Execute a query
In the FlightQuery.main method:
Instantiate a
CredentialCallOptionwithTOKENas 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";Call the
FlightSqlClient.executemethod with the SQL query and theCredentialCallOption.If successful, the
FlightSqlClient.executemethod responds with aFlightInfoobject that contains metadata and anendpoints: [...]list. Each endpoint contains the following:- A list of addresses where you can retrieve the data.
- A
ticketvalue that identifies the data to retrieve.
Extract the ticket from the response.
Retrieve and process Arrow data
In the FlightQuery.main() method, do the following to retrieve the data stream described in the FlightInfo response:
Call the
FlightSqlClient.getStreammethod with the ticket and theCredentialCallOptionto fetch the Arrow stream.Call the
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
System.out.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:
Copy the
Dockerfileandpom.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 Clustered databaseHOST: your [InfluxDB cluster hostname (URL without the βhttps://β)TOKEN: your InfluxDB Clustered database token with read permission to the database
docker build \ --build-arg DATABASE_NAME=INFLUX_DATABASE \ --build-arg HOST=cluster-host.com \ --build-arg TOKEN=INFLUX_TOKEN \ -t javaflight .The command builds a Docker image named
javaflight.To run the application in a new Docker container, enter the following command:
docker run javaflightThe 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 Clustered and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.