Skip to content
This repository was archived by the owner on Nov 20, 2026. It is now read-only.
Cedrick Lunven edited this page Jan 24, 2021 · 51 revisions

License Apache2

Introduction

The SDK makes it easy to call Astra services using idiomatic Java APIs. On top of the SDK the repository provide tool like a Spring Boot starter and another samples helping you starting code with ASTRA and JAVA.

Single Class for interface AstraClient as a builder A class for each API exposed by Astra.

Astra use the open source data gateway Stargate to expose REST, Document and GraphQL APIs, design is done to work with both

Installation

Prerequisites:

The SDK has been build with the HttpClient provided in JDK. A

You need JDK 11.0.9+ and a dependency management too as Maven or Gradle

Declare the maven dependency in your pom.xml

<dependency>
  <groupId>com.datastax.astra</groupId>
  <artifactId>astra-sdk-java</artifactId>
  <version>${2021.1}</version>
</dependency>

Declare the Gradle dependency in your build.gradle

dependencies {
    astra_sdk 'com.datastax.astra:astra-sdk-java:2021.1'
}

That's it the SDK is a single library all included.

5 min Getting started

To start using the library you can either use AstraClient with the builder (recommended) or instanciate sub clients you need.

With AstraClient not all parameters are needed, it depends on which API you want to use. Values are initialised with environment variables if exist full list here but in this tutorial we will be explicit.

To find the values in the Astra user interface you can refer to Where are variables values. Final word, we will with entity Person in our sample.

// Enable every api (REST, Doc, Cql, Devops)
AstraClient astraClient = AstraClient
 .builder()
 .astraDatabaseId("1111111-1111-1111-1111-111111111111") // Use in Rest,Doc,Devops,Cql APIs
 .astraDatabaseRegion("europe-west1")                     // Use in Rest,Doc,Devops,Cql APIs
 .username("astraUser")                                   // Use in Rest,Doc,Cql APIs
 .password("astraPassword")                               // Use in Rest,Doc,Cql APIs
 .clientId("2222222-2222-2222-2222-22222222222")          // Use in Devops only API
 .clientName("cedric.lunven@datastax.com")                // Use in Devops only API
 .clientSecret("3333333-3333-3333-3333-333333333333")     // Use in Devops only API
 .build();

// DOCUMENT API
ApiDocumentClient apiDocClient = astraClient.apiDocument();

// DOC1. Create Namespace
if (!apiDocClient.namespace("five_min_namespace").exist()) {
  // apiDocClient.namespace("five_min_namespace").create();
  // We use the devops API with higher privileges
  astraClient.apiDevops()
             .createNamespace("1111111-1111-1111-1111-111111111111", "five_min_namespace");
}

// DOC2. Create a collection
if (!apiDocClient.namespace("five_min_namespace").collection("person").exist()) {
   apiDocClient.namespace("five_min_namespace")
               .collection("person")
               .create();
}

// DOC3. Create a document Insert a Person and system generates id
String johnId = apiDocClient.namespace("five_min_namespace")
               .collection("person")
               .save(new Person("John", "Doe", 20, new Address("Paris", 75000)));

// DOC4. Upsert a document by providing id
apiDocClient.namespace("five_min_namespace")
            .collection("person")
            .document("saradoe")
            .save(new Person("Sara", "Doe", 20, new Address("Paris", 75000)));

// DOC5. Find all (notice that everything is paged)
ResultListPage<Person> results = apiDocClient
    .namespace("five_min_namespace")
    .collection("person")
    .findAll(Person.class);

// DOC6. Search: Find with a where Clause
ResultListPage<Person> results = collectionPerson.search(QueryDocument.builder()
                .where("lastname").isEqualsTo("Doe").build(), Person.class);

Initializing Astra Client

Clone this wiki locally