Skip to content

Latest commit

 

History

History
72 lines (54 loc) · 2.85 KB

README.md

File metadata and controls

72 lines (54 loc) · 2.85 KB

Connect to Astra with Java Cassandra Drivers version 3.x

📦. Prerequisites [ASTRA]

📦. Prerequisites [Development Environment]

  • You should install Java Development Kit (JDK) 8: Use the reference documentation to install a Java Development Kit, Validate your installation with
java --version
mvn -version

📦. Setup Project

  • Please note that version 3.8+ is required to connect to Astra.

  • Update your pom.xml file with the latest version of the 3.x libraries: Maven Central

<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-core</artifactId>
  <version>${latest3x}</version>
</dependency>

🖥️. Sample Code (project astra-driver3x)

import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

public class AstraDriver3x {    
 
 // Define inputs
 static final String ASTRA_ZIP_FILE = "<path_to_secureConnectBundle.zip>";
 static final String ASTRA_USERNAME = "<provide_a_clientId>";
 static final String ASTRA_PASSWORD = "<provide_a_clientSecret>";
 static final String ASTRA_KEYSPACE = "<provide_your_keyspace>";    
    
 public static void main(String[] args) {
  Logger logger = LoggerFactory.getLogger(AstraDriver3x.class);
  try(Cluster cluster = Cluster.builder()
    .withCloudSecureConnectBundle(new File(ASTRA_ZIP_FILE))
    .withCredentials(ASTRA_USERNAME, ASTRA_PASSWORD)
    .build() ) {
      Session session = cluster.connect(ASTRA_KEYSPACE);
      logger.info("[OK] Welcome to ASTRA. Connected to Keyspace {}", session.getLoggedKeyspace());
  }
  logger.info("[OK] Success");
  System.exit(0);
 }

}

🔗. Extra Resources