Skip to content

Document API

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

Before Starting

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.

Client Initialisation

The class to work with document API is ApiDocumentClient. You can create it in the following ways:

📘 With AstraClient

ApiDocumentClient apiDocClientAstra1 = AstraClient.builder()
 .astraDatabaseId("dbId").astraDatabaseRegion("dbRegion")
 .username("dbUser")
 .password("dbPasswd")
 .build().apiDocument();

ApiDocumentClient apiDocClientStargate1 = AstraClient.builder()
 .baseUrl("url")
 .username("dbUser")
 .password("dbPasswd")
 .build().apiDocument();

📘 With constructor

ApiDocumentClient apiDocClientAstra2 = 
    new ApiDocumentClient(dbId, dbRegion, username, password);

ApiDocumentClient apiDocClientStargate2 = 
    new ApiDocumentClient(baseUrl, username, password);

From now in other sample we will use variable apiDocClient.

You can use this class as reference for the code.

Working with namespaces

✅ Lists available Namespaces reference documentation

Stream<Namespace> namespaces = apiDocClient.namespaces();

✅ Find namespace by its id reference documentation

Optional<Namespace> ns1 = apiDocClient..namespace("ns1").find();

✅ Check namespace exists

apiDocClient.namespace("ns1").exist();

✅ Create a new namespace reference documentation

DataCenter dc1 = new DataCenter("dc-1", 1);
apiDocClient.namespace("ns1").create(dc1);

This operation required elevated privilege in Astra but works in Stargate. To create a namespace in Astra you can also use the DevopsAPI.

✅ Delete reference documentation

apiDocClient.namespace("ns1").delete();

This operation required elevated privilege in Astra but works in Stargate. To delete a namespace in Astra you can also use the DevopsAPI.

ℹ️ Tips

You can simply the code by assigning apiDocClient.namespace("ns1") to a variable;

NamespaceClient nsClient = apiDocClient.namespace("ns1");
nsClient.exist();
nsClient.delete();
nsClient.find();
//...

Working with Collections

**✅ Lists available Collection in namespace ** reference documentation

Stream<String> colNames = apiDocClient.namespace("ns1").collectionNames();

✅ Check if collection exists reference documentation

boolean colExist = apiDocClient.namespace("ns1").collection("col1").exist();

**✅ Create an empty collection ** reference documentation

apiDocClient.namespace("ns1").collection("col1").create();

**✅ Delete a collection ** reference documentation

apiDocClient.namespace("ns1").collection("col1").delete();

**✅ Rename a collection ** reference documentation

apiDocClient.namespace("ns1").collection("col1").rename("col2);

ℹ️ Tips

You can simply the code by assigning apiDocClient.namespace("ns1").collection("col1") to a variable;

NamespaceClient nsClient   = apiDocClient.namespace("ns1");
CollectionClient colClient = nsClient.collection("col1");
colClient.exist();
//...

Working with Documents