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.
astra-db-ts
is the equivalent for typescriptastrapy
is the equivalent in pythonThis library is under development and is available in Maven Central. You can build it locally and install it in your local repository.
📦 Java Development Kit (JDK) 11Docker 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.
git clone git@github.com:datastax/astra-db-java.git
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=true2. QuickStart with Astra DB 2.1. Sign up for Astra DB
Google
or Github
account. It is free to use. There is free forever tiers of up to 25$ of consumption every month.If you are creating a new account, you will be brought to the DB-creation form directly.
[Create Database]
button on the right.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
.
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.
Add the following dependency to your pom.xml
file:
<dependency> <groupId>com.datastax.astra</groupId> <artifactId>astra-db-java</artifactId> <version>1.3.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.collections.Collection; import com.datastax.astra.client.databases.Database; import com.datastax.astra.client.collections.documents.Document; import com.datastax.astra.client.core.paging.FindIterable; import java.util.List; import static com.datastax.astra.client.core.query.Filters.eq; import static com.datastax.astra.client.core.vector.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 Instances
Prequisite. You need HCD, DSE or CASSANDRA running on your machine and listening on
9042
. One good way is to run HCD as a docker image following the instruction here.
git clone git@github.com:stargate/data-api.git
localhost
and datacenter to dc1
.cd data-api
STARGATE_DATA_STORE_SAI_ENABLED=true \
STARGATE_DATA_STORE_VECTOR_SEARCH_ENABLED=true \
STARGATE_JSONAPI_OPERATIONS_VECTORIZE_ENABLED=true \
STARGATE_DATA_STORE_IGNORE_BRIDGE=true \
STARGATE_JSONAPI_OPERATIONS_DATABASE_CONFIG_LOCAL_DATACENTER=dc1 \
STARGATE_JSONAPI_OPERATIONS_DATABASE_CONFIG_CASSANDRA_END_POINTS=localhost \
QUARKUS_HTTP_ACCESS_LOG_ENABLED=FALSE \
QUARKUS_LOG_LEVEL=INFO \
JAVA_MAX_MEM_RATIO=75 \
JAVA_INITIAL_MEM_RATIO=50 \
GC_CONTAINER_OPTIONS="-XX:+UseG1GC" \
JAVA_OPTS_APPEND="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" \
mvn quarkus:dev -Dstargate.data-store.ignore-bridge=true -Dstargate.jsonapi.operations.vectorize-enabled=true -Dstargate.jsonapi.operations.database-config.local-datacenter=dc1 -Dquarkus.log.console.darken=2 -Dstargate.feature.flags.tables=true -Dstargate.jsonapi.operations.extend-error=true -Dstargate.feature.flags.reranking=true
/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
default_keyspace
curl --location 'http://localhost:8181//v1' \ --header 'token: Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh' \ --header 'Content-Type: application/json' \ --data '{"createKeyspace":{"name":"default_keyspace"}}'
person
with CURL (indexing)curl --location 'http://localhost:8181//v1/default_keyspace' \ --header 'token: Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh' \ --header 'Content-Type: application/json' \ --data '{ "createCollection": { "name": "collection_person", "options": { "indexing": { "allow": [ "firstname", "lastname" ] } } } }'
curl --location 'http://localhost:8181//v1/default_keyspace' \ --header 'token: Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh' \ --header 'Content-Type: application/json' \ --data '{ "findCollections": {} }'
curl --location 'http://localhost:8181/v1/default_keyspace/collection_person' \ --header 'token: Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh' \ --header 'Content-Type: application/json' \ --data-raw '{ "insertMany": { "documents": [ { "_id": 1, "firstname": "Lucas", "lastname": "Hernandez", "age": 22 }, { "_id": 2, "firstname": "Antoine", "lastname": "Griezmann", "age": 25 }, { "_id": 3, "firstname": "N'\''Golo", "lastname": "Kanté", "age": 29 }, { "_id": 4, "firstname": "Tanguy", "lastname": "Ndombele", "age": 24 }, { "_id": 5, "firstname": "Raphaël", "lastname": "Varane", "age": 10 }, { "_id": 6, "firstname": "Hugo", "lastname": "Lloris", "age": 41 }, { "_id": 7, "firstname": "Olivier", "lastname": "Giroud", "age": 36 } ] } }'
curl --location 'http://localhost:8181/v1/default_keyspace/collection_person' \ --header 'token: Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh' \ --header 'Content-Type: application/json' \ --data-raw '{ "find": { "filter": { "lastname": "Varane" }, "projection": { "lastname": true, "firstname": true }, "sort": {"lastname":-1} } }'
curl --location 'http://localhost:8181/v1/default_keyspace/collection_person' \ --header 'token: Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh' \ --header 'Content-Type: application/json' \ --data-raw '{ "find": { "filter": { "age": 24 }, "projection": { "lastname": true, "firstname": true }, "sort": {"lastname":-1} } }'Using Java client with Local Instance
public class QuickStartLocal { public static void main(String[] args) { // Create a token String token = new UsernamePasswordTokenProvider("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"); // Create a default keyspace ((DataAPIDatabaseAdmin) client .getDatabase(dataApiUrl) .getDatabaseAdmin()).createNamespace(keyspaceName, NamespaceOptions.simpleStrategy(1)); System.out.println("Keyspace created "); 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("simple", 5, COSINE); System.out.println("Created a Collection simple"); // Create a collection with Vector embeddings OPEN AI Collection<Document> collectionLyrics = db.createCollection("vector", CollectionOptions.builder() .vectorSimilarity(SimilarityMetric.COSINE) .vectorDimension(1536) .vectorize("openai", "text-embedding-3-small") .build(), new CommandOptions<>().embeddingAPIKey("sk-.....")); } }
This is an the organization of the different classes of the framework.
For more information use the JAVADOC documentation
The examples
directory contains more examples on how to use the library.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4