Skip to content
This repository was archived by the owner on Nov 20, 2026. It is now read-only.

Document API

Cedrick Lunven edited this page Jan 25, 2021 · 33 revisions

Initialisation

📘 Setup with Astra

AstraClient astraClient = AstraClient.builder()
                .astraDatabaseId(<youdbId>")
                .astraDatabaseRegion("<youdbregion>")
                .username("<username>")
                .password("<password>")
                .tokenTtl(Duration.ofSeconds(300)) // optional default is 300s
                .build()

📘 Setup with Stargate

AstraClient stargateClient = AstraClient.builder()
                .baseUrl("<stargate_url>")
                .username("<username>")
                .password("<password>")
                .tokenTtl(Duration.ofSeconds(300)) // optional default is 300s
                .build();

Configure client AstraClient with Constructor

📘 Working with Astra

AstraClient astraClient = new AstraClient("<youdbId>", "<youdbregion>", "<username>", "<password>");
AstraClient astraClient = new AstraClient("<youdbId>", "<youdbregion>", "<username>", "<password>", Duration.ofSeconds(300));

📘 Working with Stargate

AstraClient astraClient = new AstraClient("<baseUrl>", "<username>", "<password>");
AstraClient astraClient = new AstraClient("<baseUrl>", "<username>", "<password>", Duration.ofSeconds(300));          

Configure client AstraClient with Spring Framework

As the AstraClient can be initialized with a constructor you want to read values from configuration files like application.properties or application.yaml and inject them in the constructor.

A Spring Boot starter will look for the following key to initiate the client for you (see SPRING BOOT STARTER. You would also reuse environment variables defined before.

astra:
  username: $(ASTRA_DB_ID:username}
  password: <password>
  dataBaseId: <dbId>
  databaseRegion: <dbRegion>
  baseUrl: <baseUrl>
  tokenTtl: <tokenUrl>

Sample Configuration Class

@Configuration
public class AstraConfiguration {

 @Value("${astra.username}")
 private String atraUsername;
 
 @Value("${astra.password}")
 private String atraPassword;
 
 @Value("${astra.dataBaseId}")
 private String dataBaseId;
 
 @Value("${astra.databaseRegion}")
 private String databaseRegion;

 @Bean
 protected Astraclient astraClient() {
	return new AstraClient(dataBaseId, databaseRegion, atraUsername, atraPassword);
 }
}

Test connectivity

To interact with Astra and Stargate you need to create an Authentication Token and provides it in the header for each of you query using key X-Cassandra-Token. The work is done for you at the SDK level but not only. It will store it a renew it after a time to leave you setup. Before every class to the API the SDK check the token. To create a token and as such validate your connection details you can do:

boolean isConnectionEstablished = astraClient.connect();

2. Schema API

As presented in the Astra Reference Documentation the Astra Document APIS is divided in 4 spaces: Authentications, Namespace, Keyspaces and Schemas.

3. Document API

Astra and Stargate bring great innovation by allowing Apache Cassandra to store Documents like a document-oriented noSQL database. To cope with Cassandra data model constraints the document shredding function has been used.

As a Java developer you want to work with objects (entities) and let the SDK interact with the API performing operations you need Create, Read, Update, Delete and search.

ARCHITECTURE PICS

Namespaces and Collection

  • Namespace: Namespace are simply keyspaces in Cassandra that will *only store documents you cannot mix documents and non document data. As such when working with namespace we know that we are working with the Document API.

*Astra Client is ready to work with the document API on namespace namespace_x

astraClient.namespace("namespace_x");
  • Collections: Collections are simply tables in Cassandra. Those tables are generated and managed for you but you need to know on which collection you are working for.

Working with collections

**✅ List available collections in a namespace. **

astraClient.namespace("namespace1")                 // select namespace1
           .findAllCollections()                    // list collection names
           .stream().forEach(System.out::println);; // Show values in console

**✅ Check if a collection exist. **

boolean isAAAExist = astraClient.namespace("namespace1").existCollection("AAA");

**✅ Create a new collection **

Collection name should be only Alpha Numeric characters.

 astraClient.namespace("namespace1").createCollection("AAA");

Using Astra Client

asas

Using DocumentApiClient

aasa

Schema management

List Collections

Find a collections from its id

CRUD Operations

Search

Paging

Partial update

Clone this wiki locally