2015-02-10 63 views
1

我在cassandra.yaml文件中将我的Cassandra数据库的authenticator值更改为'PasswordAuthenticator'。 以前我用下面的代码用java连接到数据库。访问在Java中使用'PasswordAuthenticator'的Cassandra数据库

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连接到数据库时提供这些详细信息?

回答

4

你可以通过添加.withCredentials方法到群集的建设者,是这样的:

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