访问使用Java中的“PasswordAuthenticator”的Cassandra数据库

时间:2015-02-10 20:20:45

标签: java cassandra datastax

我将Cassandra数据库的身份验证器值更改为' PasswordAuthenticator'在cassandra.yaml文件中。 以前我使用以下代码使用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();
   }

现在这段代码给我错误说

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

我知道我需要使用我的超级用户用户名和密码连接到数据库。使用java连接到数据库时,如何提供这些详细信息?

1 个答案:

答案 0 :(得分:4)

您可以通过将.withCredentials方法添加到群集构建器来实现,如下所示:

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