Chamila Wijayarathna
Chamila Wijayarathna

Reputation: 1943

Accesing Cassandra Database which uses 'PasswordAuthenticator' in Java

I changed the authenticator value of my Cassandra database to 'PasswordAuthenticator' in cassandra.yaml file. Previously I used following code to connect to the database using java.

public void connect(String node) {
      cluster = Cluster.builder()
            .addContactPoint(node).build();
      Metadata metadata = cluster.getMetadata();
      System.out.printf("Connected to cluster: %s\n", 
            metadata.getClusterName());
      for ( Host host : metadata.getAllHosts() ) {
         System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
               host.getDatacenter(), host.getAddress(), host.getRack());
      }
      session = cluster.connect();
   }

Now this code gives me error saying

Exception in thread "main" com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host /127.0.0.1: Host /127.0.0.1 requires authentication, but no authenticator found in Cluster configuration

I understand that I need to connect to the database with my super user username and password. How can I give those details when connecting to database using java?

Upvotes: 1

Views: 359

Answers (1)

Aaron
Aaron

Reputation: 57748

You can do that by adding the .withCredentials method to your cluster builder, like this:

  cluster = Cluster.builder()
        .addContactPoint(node)
        .withCredentials("yourusername", "yourpassword")
        .build();

Upvotes: 4

Related Questions