| 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.Cluster; |
| 19 | import com.datastax.driver.core.ResultSet; |
| 20 | import com.datastax.driver.core.Row; |
| 21 | import com.datastax.driver.core.Session; |
| 22 | |
| 23 | /** |
| 24 | * Connects to a Cassandra cluster and extracts basic information from it. |
| 25 | * <p/> |
| 26 | * Preconditions: |
| 27 | * - a Cassandra cluster is running and accessible through the contacts points identified by CONTACT_POINTS and PORT. |
| 28 | * <p/> |
| 29 | * Side effects: none. |
| 30 | * |
| 31 | * @see <a href="http://datastax.github.io/java-driver/manual/">Java driver online manual</a> |
| 32 | */ |
| 33 | public class ReadCassandraVersion { |
| 34 | |
| 35 | static String[] CONTACT_POINTS = {"127.0.0.1"}; |
| 36 | static int PORT = 9042; |
| 37 | |
| 38 | public static void main(String[] args) { |
| 39 | |
| 40 | Cluster cluster = null; |
| 41 | try { |
| 42 | // The Cluster object is the main entry point of the driver. |
| 43 | // It holds the known state of the actual Cassandra cluster (notably the Metadata). |
| 44 | // This class is thread-safe, you should create a single instance (per target Cassandra cluster), and share |
| 45 | // it throughout your application. |
| 46 | cluster = Cluster.builder() |
| 47 | .addContactPoints(CONTACT_POINTS).withPort(PORT) |
| 48 | .build(); |
| 49 | |
| 50 | // The Session is what you use to execute queries. Likewise, it is thread-safe and should be reused. |
| 51 | Session session = cluster.connect(); |
| 52 | |
| 53 | // We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection |
| 54 | // of Row objects. |
| 55 | ResultSet rs = session.execute("select release_version from system.local"); |
| 56 | // Extract the first row (which is the only one in this case). |
| 57 | Row row = rs.one(); |
| 58 | |
| 59 | // Extract the value of the first (and only) column from the row. |
| 60 | String releaseVersion = row.getString("release_version"); |
| 61 | System.out.printf("Cassandra version is: %s%n", releaseVersion); |
| 62 | |
| 63 | } finally { |
| 64 | // Close the cluster after we’re done with it. This will also close any session that was created from this |
| 65 | // cluster. |
| 66 | // This step is important because it frees underlying resources (TCP connections, thread pools...). In a |
| 67 | // real application, you would typically do this at shutdown (for example, when undeploying your webapp). |
| 68 | if (cluster != null) |
| 69 | cluster.close(); |
| 70 | } |
| 71 | } |
| 72 | } |