Java 連線到本地 Cassandra 例項
連線到 Cassandra 非常類似於連線到其他資料來源。使用 Cassandra,不需要憑據。
String cassandraIPAddress = "127.0.0.1";
String cassandraKeyspace = "myKeyspace";
String username = "foo";
String password = "bar";
com.datastax.driver.core.Cluster cluster = Cluster.builder()
.addContactPoint(cassandraIPAddress)
.withCredentials(username, password) // If you have setup a username and password for your node.
.build();
com.datastax.driver.core.Session session = cluster.connect(cassandraKeyspace);
com.datastax.driver.core.Metadata metadata = cluster.getMetadata();
// Output Cassandra connection status
System.out.println("Connected to Cassandra cluster: " + metadata.getClusterName() + " with Partitioner: " + metadata.getPartitioner());
// Loop through your entire Cluster.
for (Host host : metadata.getAllHosts()) {
System.out.println("Cassandra Host Address: " + host.getAddress() + " | Is Up = " + host.isUp());
}