Code Monkey home page Code Monkey logo

astra-db-java's Introduction

Java Client for Data API

License Apache2 Maven Central

Security Rating Maintainability Rating Reliability Rating

Bugs Vulnerabilities Duplicated Lines (%)

Lines of Code Coverage

This client library provides a simplified way to interact with Data API for AstraDB or local instances. For detailed documentation, each operation comes with a detailed description and examples.

This library is under development and is available in Maven Central. You can build it locally and install it in your local repository.

1. Installation

1.1 Prerequisites

๐Ÿ“ฆ Java Development Kit (JDK) 11

java --version

๐Ÿ“ฆ Apache Maven

mvn -version

๐Ÿ“ฆ Docker (local Installation)

Docker is an open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualization on Linux.

1.2 Packaging

  • Clone the repository
git clone [email protected]:datastax/astra-db-java.git
  • Build the project (java 11 and Maven is required)

Note: You should skip the tests if you want to speed up the build, to run the test you need to have a bit of setup:

  • An environment variable ASTRA_DB_APPLICATION_TOKEN with your an Organization Administrator Astra token (PROD)
  • An environment variable ASTRA_DB_APPLICATION_TOKEN_DEV with your an Organization Administrator Astra token (DEV)
  • A running Data API locally with docker (see the docker-compose.yml in the root of the project)
mvn clean install -Dtest.skipped=true

2. QuickStart with Astra DB

2.1. Sign up for Astra DB

  • Access https://astra.datastax.com and register with Google or Github account. It is free to use. There is free forever tiers of up to 25$ of consumption every month.

2.2. Create a Database

If you are creating a new account, you will be brought to the DB-creation form directly.

  • Get to the databases dashboard (by clicking on Databases in the left-hand navigation bar, expanding it if necessary), and click the [Create Database] button on the right.

  • โ„น๏ธ Fields Description
Field Description
Vector Database vs Serverless Database Choose Vector Database In june 2023, Cassandra introduced the support of vector search to enable Generative AI use cases.
Database name It does not need to be unique, is not used to initialize a connection, and is only a label (keep it between 2 and 50 characters). It is recommended to have a database for each of your applications. The free tier is limited to 5 databases.
Cloud Provider Choose whatever you like. Click a cloud provider logo, pick an Area in the list and finally pick a region. We recommend choosing a region that is closest to you to reduce latency. In free tier, there is very little difference.
Cloud Region Pick region close to you available for selected cloud provider and your plan.

If all fields are filled properly, clicking the "Create Database" button will start the process.

It should take a couple of minutes for your database to become Active.

2.3. Get your credentials

To connect to your database, you need the API Endpoint and a token. The api endpoint is available on the database screen, there is a little icon to copy the URL in your clipboard. (it should look like https://<db-id>-<db-region>.apps.astra.datastax.com).

To get a token click the [Generate Token] button on the right. It will generate a token that you can copy to your clipboard.

2.4 Create a new project and add the dependency

Add the following dependency to your pom.xml file:

<dependency>
  <groupId>com.datastax.astra</groupId>
  <artifactId>astra-db-java</artifactId>
  <version>1.1.0</version>
</dependency>

Here is a sample class that demonstrates how to use the library:

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.Database;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.FindIterable;
import java.util.List;
import static com.datastax.astra.client.model.Filters.eq;
import static com.datastax.astra.client.model.SimilarityMetric.cosine;

public class GettingStarted {
 public static void main(String[] args) {
  // Initializing client with a token
  DataAPIClient client = new DataAPIClient("my_token");

  // Accessing the Database through the HTTP endpoint
  Database db = client.getDatabase("http://db-region.apps.astra.datastax.com");

  // Create collection with vector support
  Collection<Document> col = db.createCollection("demo", 2, cosine);

  // Insert records
  col.insertMany(List.of(
   new Document("doc1").vector(new float[]{.1f, 0.2f}).append("key", "value1"),
   new Document().id("doc2").vector(new float[]{.2f, 0.4f}).append("hello", "world"),
   new Document("doc3").vector(new float[]{.5f, 0.6f}).append("key", "value1"))
  );

  // Search
  FindIterable<Document> docs = col.find(
    eq("key", "value1"), // metadata filter
    new float[] {.5f, .5f},              //vector
    10);                                 // maxRecord

  // Iterate and print your results
  for (Document doc : docs) System.out.println(doc);
 }
}

3. QuickStart with Local Instance

3.1. Start Data Api.

  • Start the 2 containers with the following command:
docker-compose up -d
  • Check the status of the containers with the following command:
docker-compose ps

Output

NAME                          IMAGE                                           COMMAND                
astra-db-java-coordinator-1   stargateio/coordinator-dse-next:v2.1.0-BETA-9   "./starctl"             
astra-db-java-jsonapi-1       stargateio/jsonapi:v1.0.6         
  • Here are the information to connect to the local instance:
Field Description
Data API Spec http://localhost:8181/swagger-ui/#/
Data API Endpoint http://localhost:8181
Token Header Key Token
Token Header Value Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh (aka Cassandra:Base64(userName):Base64(password))
Authentication API Spec (before 1.0.6) http://localhost:8081/swagger-ui/#/
  • The API will have 3 resources
Field Url Description
Namespace /v1/ Interact with namespaces (not available in Astra)
Data API Endpoint /v1/{namespace} Interact with collections of a namespace
Token Header Key /v1/{namespace}/{collection} Interact with documents of a collection
  • Sample curl to create a namespace:
curl -X 'POST' \
  'http://localhost:8181/v1' \
  -H 'accept: application/json' \
  -H 'Token: Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh' \
  -H 'Content-Type: application/json' \
  -d '{
  "createNamespace": {
    "name": "default_keyspace"
  }
}'

3.2. Using Java client with local instance

public class QuickStartLocal {

    public static void main(String[] args) {

        // Create a token
        String token = new TokenProviderStargateV2("cassandra", "cassandra").getToken();
        System.out.println("Token: " + token);

        // Initialize the client
        DataAPIClient client = new DataAPIClient(token, builder().withDestination(CASSANDRA).build());
        System.out.println("Connected to Data API");

        Database db = client.getDatabase("http://localhost:8181", "default_keyspace");
        System.out.println("Connected to Database");

        // Create a collection. The default similarity metric is cosine.
        Collection<Document> collection = db.createCollection("vector_test", 5, COSINE);
        System.out.println("Created a Collection");
    }
}

4. What's Next

This is an the organization of the different classes of the framework.

overview

  • For more information use the JAVADOC documentation

  • The examples directory contains more examples on how to use the library.

astra-db-java's People

Contributors

clun avatar tatu-at-datastax avatar

Stargazers

 avatar Jonathan Shook avatar Dave Fisher avatar Taketoday avatar Madhavan avatar Tatu Saloranta avatar  avatar  avatar Pravin Bhat avatar

Watchers

Kiyu Gabriel avatar Alex Leventer avatar  avatar  avatar

Forkers

clun aploetz

astra-db-java's Issues

Enable Fast Double parse/generation for Jackson

As per:

there are settings to enable optimized non-JDK reading and writing of floating-point values.

Let's enable those; should have meaningful impact for $vector value handling in particular.

Need a way to override limit for maximum allowed batch size for "InsertMany" (from built-in 20)

Although default Data API version limits maximum number of documents for "InsertMany" to 20, there are ways to override this -- and limit may be changed in near future.
Collection.insertMany() allows specifying any chunk size via options, but DataAPIOptions.getMaxDocumentsInInsert() is used to figure out if actual value is allowed -- if that is exceeded, client will throw an exception.

So, for testing of higher allowed values (with matching Data API overrides or changes), I would need a mechanism to either override maximum value, or -- probably better -- have a way of saying "just let me pass possible invalid value" (overriding either just this value, or other settings).
I am not sure what'd be the best way but will think about this and maybe submit a PR.

I suspect similar needs may arise wrt other guardrail settings -- they are good in helping developers avoid server-side failures, but may need to be ignored for some specific use cases.

`mvn [clean] install` should succeed (or document alternative) to locally build snapshot version

Looks like basic mvn install does not quite work as suggested in README, due to couple of problems. It'd be good to resolve that to allow local builds (I could use that as work-around for #3 for example :) ).

First thing is that README.MD recommended:

mvn clean install -DskipTests=true

does not actually skip tests (so build fails): I think this needs to instead be

mvn clean install -Dtest.skipped=true

(so needs a minor edit of README.MD)

Second problem comes with JDK 11 javac that has by default fail on javadoc compilation errors: this does not happen with JDK 17 so that may be a work-around.
Otherwse either (all!) Javadoc "error" level problems need to be fixed (this may be doable, it's not a ton, and probably a good idea anyway). Or maybe it'd be enough to pass compiler arguments to prevent this failure; I forget what is needed but should be simple enough to google.

Third problem comes when compiling with JDK17; client package (./astra-db-java) actually builds but then build of ./examples fails with:

[ERROR] Failed to execute goal on project astra-db-java-examples: Could not resolve dependencies for project com.datastax.astra:astra-db-java-examples:jar:1.0.0-SNAPSHOT: The following artifacts could not be resolved: com.datastax.astra:astra-db-java:jar:1.0.0-beta2-SNAPSHOT (absent): Could not find artifact com.datastax.astra:astra-db-java:jar:1.0.0-beta2-SNAPSHOT -> [Help 1]

for some reason. I am not 100% sure why Maven build (with 3.9.6 if that matters) fails here, but I get same problem on 2 separate systems.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.