| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.examples.basic; |
| 17 | |
| 18 | import com.datastax.driver.core.*; |
| 19 | |
| 20 | /** |
| 21 | * Gathers information about a Cassandra cluster's topology (which nodes belong to the cluster) and schema (what |
| 22 | * keyspaces, tables, etc. exist in this cluster). |
| 23 | * <p/> |
| 24 | * Preconditions: |
| 25 | * - a Cassandra cluster is running and accessible through the contacts points identified by CONTACT_POINTS and PORT. |
| 26 | * <p/> |
| 27 | * Side effects: none. |
| 28 | * |
| 29 | * @see <a href="http://datastax.github.io/java-driver/manual/">Java driver online manual</a> |
| 30 | */ |
| 31 | public class ReadTopologyAndSchemaMetadata { |
| 32 | |
| 33 | static String[] CONTACT_POINTS = {"127.0.0.1"}; |
| 34 | static int PORT = 9042; |
| 35 | |
| 36 | public static void main(String[] args) { |
| 37 | |
| 38 | Cluster cluster = null; |
| 39 | try { |
| 40 | cluster = Cluster.builder() |
| 41 | .addContactPoints(CONTACT_POINTS).withPort(PORT) |
| 42 | .build(); |
| 43 | |
| 44 | Metadata metadata = cluster.getMetadata(); |
| 45 | System.out.printf("Connected to cluster: %s%n", metadata.getClusterName()); |
| 46 | |
| 47 | for (Host host : metadata.getAllHosts()) { |
| 48 | System.out.printf("Datatacenter: %s; Host: %s; Rack: %s%n", |
| 49 | host.getDatacenter(), host.getAddress(), host.getRack()); |
| 50 | } |
| 51 | |
| 52 | for (KeyspaceMetadata keyspace : metadata.getKeyspaces()) { |
| 53 | for (TableMetadata table : keyspace.getTables()) { |
| 54 | System.out.printf("Keyspace: %s; Table: %s%n", |
| 55 | keyspace.getName(), table.getName()); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } finally { |
| 60 | if (cluster != null) |
| 61 | cluster.close(); |
| 62 | } |
| 63 | } |
| 64 | } |