2015-02-10 31 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();