generated from DataStax-Examples/datastax-examples-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSampleCode3x_CONNECT_ClusterShowMetaData.java
66 lines (56 loc) · 2.45 KB
/
SampleCode3x_CONNECT_ClusterShowMetaData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.datastax.samples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Metadata;
/**
* Standalone class to log metadata of a running cluster.
*
*
* Pre-requisites:
* - Cassandra running locally (127.0.0.1, port 9042)
*
* @author DataStax Developer Advocate Team
*
* Need Help ? Join us on community.datastax.com to ask your questions for free.
*/
public class SampleCode3x_CONNECT_ClusterShowMetaData implements ExampleSchema {
/** Logger for the class. */
private static Logger LOGGER = LoggerFactory.getLogger(SampleCode3x_CONNECT_ClusterShowMetaData.class);
/** StandAlone (vs JUNIT) to help you running. */
public static void main(String[] args) {
LOGGER.info("Starting 'ClusterShowMetaData' sample...");
/**
* Connecting to the cluster using a single endpoint:
* - Note that we don't provide any keyspace informations
*/
try(Cluster cluster = Cluster.builder()
.addContactPoint("127.0.0.1") // Single EndPoint is required, 2 is better
.withPort(9042) // Default port, optional here
.build()) {
// Enforcing meta datas to be retrieved
cluster.init();
Metadata metadata = cluster.getMetadata();
LOGGER.info("Connected to cluster '{}'", metadata.getClusterName());
LOGGER.info("Protocol Version: {}",
cluster.getConfiguration()
.getProtocolOptions()
.getProtocolVersion());
LOGGER.info("Listing available Nodes:");
for (Host host : metadata.getAllHosts()) {
LOGGER.info("+ [{}]: datacenter='{}' and rack='{}'",
host.getListenAddress(),
host.getDatacenter(),
host.getRack());
}
LOGGER.info("Listing available keyspaces:");
for (KeyspaceMetadata meta : metadata.getKeyspaces()) {
LOGGER.info("+ [{}] \t with replication={}", meta.getName(), meta.getReplication());
}
LOGGER.info("[OK] Success");
}
System.exit(0);
}
}