Dark Mode

Prerequisites

Set up a Rust project

Use the following command to initialize a Rust project in an empty directory:

Two files will be created, Cargo.toml and src/main.rs .

Add dependencies

  • Add required dependencies to the Cargo.toml file in your Rust project.You'll need at least stargate-grpc and an async framework, such as tokio:
  • Build the project with cargo build and fetch and compile the dependencies.
  • Add the following line to the includes in the source code of your app, such as main.rs :

This set-up will make all the Stargate gRPC functionality available.

The next sections explain the parts of a script to use the Stargate functionality.A full working script is included below.

Authentication

This example assumes that you're running Stargate on Astra DB.For more information regarding authentication please see theManage application tokens section.

You'll need to download your bearer token from the Astra DB dashboard andadd the token to the connection portion of the script.

Set up client

The main structure that provides the interface to Stargate is StargateClient .The simplest way to obtain an instance is to use the provided builder :

Use QueryBuilder to create a query, bind query values, and pass query parameters.The query is followed by the execute commands that actually run the command andreturn the results.

It is also possible to use a bind statement to insert values:

Data definition language (DDL) queries are supported in the same manner:

In general, users will create a keyspace and table first.

The ExecuteQuery function can be used to execute a single query.

If you need to group several commands together as abatch statement,the client also provides an `ExecuteBatch()` function to execute a batch query:

This example inserts two values into the keyspace table test.users .Only INSERT , UPDATE , and DELETE operations can be used in a batch query.

The result set comes back as a collection of rows.A row can be easily unpacked:

To put all the pieces together, here is a sample script that combines all the piecesshown above:

Running the examples

For your convenience, this project contains a bunch of examples located in theexamples directory, which demonstrate connecting, creating schemas,inserting data and querying.You'll need a working instance of a Stargate cluster to run the examples.Refer to the official Stargate documentationfor more details on how to setup Stargate.

Each example program accepts an URL of the Stargate coordinator,the authentication token and the keyspace name:

The authentication token value can be also defined in the SG_TOKEN environment variable.

Run examples

Run the keyspace example to test the connection and create the testkeyspace with the default keyspace name of stargate_examples :

Run the query example to create a keyspace and table, insert some data, andselect data to check the insertion:

The Stargate gRPC Rust Client repository is located athttps://github.com/stargate/stargate-grpc-rust-client.