|
1 | 1 | ## Manual |
2 | 2 |
|
3 | | -This part of the documentation is organized into sub-sections covering |
4 | | -specific topics. |
| 3 | +### Quick start |
| 4 | + |
| 5 | +Here's a short program that connects to Cassandra and executes a query: |
| 6 | + |
| 7 | +```java |
| 8 | +Cluster cluster = null; |
| 9 | +try { |
| 10 | + cluster = Cluster.builder() // (1) |
| 11 | + .addContactPoint("127.0.0.1") |
| 12 | + .build(); |
| 13 | + Session session = cluster.connect(); // (2) |
| 14 | + |
| 15 | + ResultSet rs = session.execute("select release_version from system.local"); // (3) |
| 16 | + Row row = rs.one(); |
| 17 | + System.out.println(row.getString("release_version")); // (4) |
| 18 | +} finally { |
| 19 | + if (cluster != null) cluster.close(); // (5) |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +1. the [Cluster] object is the main entry point of the driver. It holds the known state of the actual Cassandra cluster |
| 24 | + (notably the [Metadata](metadata/)). This class is thread-safe, you should create a single instance (per target |
| 25 | + Cassandra cluster), and share it throughout your application; |
| 26 | +2. the [Session] is what you use to execute queries. Likewise, it is thread-safe and should be reused; |
| 27 | +3. we use `execute` to send a query to Cassandra. This returns a [ResultSet], which is essentially a collection of [Row] |
| 28 | + objects. On the next line, we extract the first row (which is the only one in this case); |
| 29 | +4. we extract the value of the first (and only) column from the row; |
| 30 | +5. finally, we close the cluster after we're done with it. This will also close any session that was created from this |
| 31 | + cluster. This step is important because it frees underlying resources (TCP connections, thread pools...). In a real |
| 32 | + application, you would typically do this at shutdown (for example, when undeploying your webapp). |
| 33 | + |
| 34 | +Note: this example uses the synchronous API. Most methods have [asynchronous](async/) equivalents. |
| 35 | + |
| 36 | +### More information |
5 | 37 |
|
6 | 38 | If you're reading this from the [generated HTML documentation on |
7 | | -github.io](http://datastax.github.io/java-driver/), use the "Read More" |
8 | | -menu on the right hand side. If you're [browsing the source files on |
| 39 | +github.io](http://datastax.github.io/java-driver/), use the "Contents" |
| 40 | +menu on the left hand side to navigate sub-sections. If you're [browsing the source files on |
9 | 41 | github.com](https://github.com/datastax/java-driver/tree/2.1/manual), |
10 | 42 | simply navigate to each sub-directory. |
11 | 43 |
|
12 | | -This is a work in progress: new sections will be added to cover existing |
13 | | -features or document new ones. |
14 | | - |
15 | | -You can also find more help in the legacy |
16 | | -[user documentation](http://docs.datastax.com/en/developer/java-driver/2.1/java-driver/whatsNew2.html) |
17 | | -on the DataStax website. |
| 44 | +[Cluster]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Cluster.html |
| 45 | +[Session]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Session.html |
| 46 | +[ResultSet]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ResultSet.html |
| 47 | +[Row]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Row.html |
0 commit comments