For the complete documentation index, see llms.txt. This page is also available as Markdown.

Rust SDK

Rust SDK for Spice.ai

The Rust SDK crate spiceai is the easiest way to query Spice.ai from Rust. It is developed in the spice-rs repository.

It uses Apache Arrow Flight to efficiently stream data to the client and Apache Arrow Records as data frames.

Requirements

  • A Rust toolchain supporting edition 2021

  • Tokio — every client method is async

Installation

Add Spice SDK

cargo add spiceai

Note the crate is named spiceai, while the repository is named spice-rs. All imports use spiceai.

Usage

1. Create a client by providing your API key to ClientBuilder. Get your free API key at spice.ai.

use spiceai::ClientBuilder;

#[tokio::main]
async fn main() {
  let client = ClientBuilder::new()
    .api_key("API_KEY")
    .use_spiceai_cloud()
    .build()
    .await
    .unwrap();
}

ClientBuilder also accepts .flight_url() to set a custom endpoint, plus .user_agent(), .max_retries() (default 3), and .cache_control(). .flight_url() and .use_spiceai_cloud() are last-writer-wins, in either order.

For a Cloud connection with no other configuration, spiceai::Client::new("API_KEY").await is equivalent.

2. Execute a query and get back a stream of Apache Arrow record batches.

The binding must be mut, because advancing the stream takes a mutable borrow.

3. Iterate through the stream to access the records. StreamExt must be in scope; the crate re-exports it.

Parameterized queries

query_with_params binds parameters supplied as an Arrow RecordBatch whose fields are named $1, $2, and so on.

Usage with local Spice runtime

Follow the quickstart guide to install and run spice locally. ClientBuilder defaults to the local runtime, so no endpoint is needed:

Default endpoints

Target
Arrow Flight

Spice.ai Cloud

https://flight.spiceai.io

Local runtime

http://localhost:50051

ClientBuilder defaults to the local runtime; Client::new(api_key) defaults to Spice.ai Cloud. Override either with .flight_url().

Contributing

Contribute to or file an issue with the spiceai crate at: https://github.com/spiceai/spice-rs

Last updated

Was this helpful?