title: "Java SDK"
description: "Connect to Spice using Spice Java SDK"
pagination_prev: null
pagination_next: null
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Java SDK for Spice.ai
https://github.com/spiceai/spice-java (opens in a new tab)
Installation
Connect to Spice runtime
Create a SpiceClient using default configuration.
Requires local Spice OSS running: follow the quickstart (opens in a new tab)
import org.apache.arrow.flight.FlightStream;
import ai.spice.SpiceClient;
public class App
{
public static void main( String[] args )
{
try {
SpiceClient client = SpiceClient.builder()
.build();
FlightStream res = client.query("SELECT \"VendorID\", \"tpep_pickup_datetime\", \"fare_amount\" FROM taxi_trips LIMIT 10");
while (res.next()) {
System.out.println(res.getRoot().contentToTSVString());
}
} catch (Exception e) {
System.err.println("An unexpected error occurred: " + e.getMessage());
}
}
}
import org.apache.arrow.flight.FlightStream;
import ai.spice.SpiceClient;
public class App
{
public static void main( String[] args )
{
try {
SpiceClient client = SpiceClient.builder()
.build();
FlightStream res = client.query("SELECT \"VendorID\", \"tpep_pickup_datetime\", \"fare_amount\" FROM taxi_trips LIMIT 10");
while (res.next()) {
System.out.println(res.getRoot().contentToTSVString());
}
} catch (Exception e) {
System.err.println("An unexpected error occurred: " + e.getMessage());
}
}
}
Or pass custom flight address:
SpiceClient client = SpiceClient.builder()
.withFlightAddress(new URI("grpc://my_remote_spice_instance:50051"))
.build();
SpiceClient client = SpiceClient.builder()
.withFlightAddress(new URI("grpc://my_remote_spice_instance:50051"))
.build();
Connection retry
The SpiceClient implements connection retry mechanism (3 attempts by default).
The number of attempts can be configured with withMaxRetries:
SpiceClient client = SpiceClient.builder()
.withMaxRetries(5) // Setting to 0 will disable retries
.build();
SpiceClient client = SpiceClient.builder()
.withMaxRetries(5) // Setting to 0 will disable retries
.build();
Retries are performed for connection and system internal errors. It is the SDK user's responsibility to properly
handle other errors, for example RESOURCE_EXHAUSTED (HTTP 429).