Connect the Java driver to your database
This driver supports the vector and non-vector data types. DataStax recommends the Data API and clients for Serverless (Vector) databases. You can use the Data API to perform CQL operations on your table data in Serverless (Vector) databases. DataStax recommends drivers only for Serverless (Non-Vector) databases, existing applications that previously used a CQL-based driver, or if you need to use some CQL functions that aren’t supported by the Data API. For more information, see Connection methods comparison. |
Initialize the Java driver to connect to your database.
Prerequisites
-
Download your database’s Secure Connect Bundle (SCB).
Connect
This code imports the necessary classes, sets up a CqlSession with an SCB, authenticates credentials, and specifies a default keyspace.
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.data.CqlVector;
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
public class VectorTest {
public static void main(String[] args) {
// Initialize the Java driver
String keyspace = "default_keyspace";
CqlSessionBuilder builder = CqlSession.builder();
builder.withCloudSecureConnectBundle(Paths.get(System.getenv("ASTRA_DB_SECURE_BUNDLE_PATH")));
builder.withAuthCredentials("token", System.getenv("ASTRA_DB_APPLICATION_TOKEN"));
builder.withKeyspace(keyspace);
try (CqlSession session = builder.build()) {
int v_dimension = 5;
// ...
}
}
}
See the Java quickstart for details on how to add dependencies, connect to a Serverless (Vector) database, create a table and vector-compatible SAI (Storage-Attached Index), load data, and perform a similarity search.