kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 66730eaae48a841848fdc50fd9ba6043b2d1b2160af394a88a30e8187bdb9f50 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-01-08 17:23 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-01-15 17:54 +01:00 |
| Parents | c3f6f598876e |
7 files changed
modified manual/README.md +132 −1
| @@ -33,6 +33,128 @@ | ||
| 33 | 33 | |
| 34 | 34 | Note: this example uses the synchronous API. Most methods have [asynchronous](async/) equivalents. |
| 35 | 35 | |
| 36 | + | |
| 37 | +### Setting up the driver | |
| 38 | + | |
| 39 | +#### [Cluster] | |
| 40 | + | |
| 41 | +##### Creating an instance | |
| 42 | + | |
| 43 | +The simplest approach is to do it programmatically with [Cluster.Builder], which provides a fluent API: | |
| 44 | + | |
| 45 | +```java | |
| 46 | +Cluster cluster = Cluster.builder() | |
| 47 | + .withClusterName("myCluster") | |
| 48 | + .addContactPoint("127.0.0.1") | |
| 49 | + .build(); | |
| 50 | +``` | |
| 51 | + | |
| 52 | +Alternatively, you might want to retrieve the settings from an external source (like a properties file or a web | |
| 53 | +service). You'll need to provide an implementation of [Initializer] that loads these settings: | |
| 54 | + | |
| 55 | +```java | |
| 56 | +Initializer myInitializer = ... // your implementation | |
| 57 | +Cluster cluster = Cluster.buildFrom(myInitializer); | |
| 58 | +``` | |
| 59 | + | |
| 60 | +##### Creation options | |
| 61 | + | |
| 62 | +The only required option is the list of contact points, i.e. the hosts that the driver will initially contact to | |
| 63 | +discover the cluster topology. You can provide a single contact point, but it is usually a good idea to provide more, so | |
| 64 | +that the driver can fallback if the first one is down. | |
| 65 | + | |
| 66 | +The other aspects that you can configure on the `Cluster` are: | |
| 67 | + | |
| 68 | +* [address translation](address_resolution/); | |
| 69 | +* [authentication](auth/); | |
| 70 | +* [compression](compression/); | |
| 71 | +* [load balancing](load_balancing/); | |
| 72 | +* [metrics](metrics/); | |
| 73 | +* low-level [Netty configuration][NettyOptions]; | |
| 74 | +* [query options][QueryOptions]; | |
| 75 | +* [reconnections](reconnection/); | |
| 76 | +* [retries](retries/); | |
| 77 | +* [socket options][SocketOptions]; | |
| 78 | +* [SSL](ssl/); | |
| 79 | +* [speculative executions](speculative_execution/); | |
| 80 | +* [query timestamps](query_timestamps/). | |
| 81 | + | |
| 82 | +In addition, you can register various types of listeners to be notified of cluster events; see [Host.StateListener], | |
| 83 | +[LatencyTracker], and [SchemaChangeListener]. | |
| 84 | + | |
| 85 | +##### Cluster initialization | |
| 86 | + | |
| 87 | +A freshly-built `Cluster` instance does not initialize automatically; that will be triggered by one of the following | |
| 88 | +actions: | |
| 89 | + | |
| 90 | +* an explicit call to `cluster.init()`; | |
| 91 | +* a call to `cluster.getMetadata()`; | |
| 92 | +* creating a session with `cluster.connect()` or one of its variants; | |
| 93 | +* calling `session.init()` on a session that was created with `cluster.newSession()`. | |
| 94 | + | |
| 95 | +The initialization sequence is the following: | |
| 96 | + | |
| 97 | +* initialize internal state (thread pools, utility components, etc.); | |
| 98 | +* try to connect to each of the contact points in sequence. The order is not deterministic (in fact, the driver shuffles | |
| 99 | + the list to avoid hotspots if a large number of clients share the same contact points). If no contact point replies, | |
| 100 | + a [NoHostAvailableException] is thrown and the process stops here; | |
| 101 | +* otherwise, the successful contact point is elected as the [control host](control_connection/). The driver negotiates | |
| 102 | + the [native protocol version](native_protocol/) with it, and queries its system tables to discover the addresses of | |
| 103 | + the other hosts. | |
| 104 | + | |
| 105 | +Note that, at this stage, only the control connection has been established. Connections to other hosts will only be | |
| 106 | +opened when a session gets created. | |
| 107 | + | |
| 108 | +#### [Session] | |
| 109 | + | |
| 110 | +By default, a session isn't tied to any specific keyspace. You'll need to prefix table names in your queries: | |
| 111 | + | |
| 112 | +```java | |
| 113 | +Session session = cluster.connect(); | |
| 114 | +session.execute("select * from myKeyspace.myTable where id = 1"); | |
| 115 | +``` | |
| 116 | + | |
| 117 | +You can also specify a keyspace name at construction time, it will be used as the default when table names are not | |
| 118 | +qualified: | |
| 119 | + | |
| 120 | +```java | |
| 121 | +Session session = cluster.connect("myKeyspace"); | |
| 122 | +session.execute("select * from myTable where id = 1"); | |
| 123 | +session.execute("select * from otherKeyspace.otherTable where id = 1"); | |
| 124 | +``` | |
| 125 | + | |
| 126 | +You might be tempted to open a separate session for each keyspace used in your application; however, note that | |
| 127 | +[connection pools](pooling/) are created at the session level, so each new session will consume additional system | |
| 128 | +resources: | |
| 129 | + | |
| 130 | +```java | |
| 131 | +// Warning: creating two sessions doubles the number of TCP connections opened by the driver | |
| 132 | +Session session1 = cluster.connect("ks1"); | |
| 133 | +Session session2 = cluster.connect("ks2"); | |
| 134 | +``` | |
| 135 | + | |
| 136 | +Also, there is currently a [known limitation](async/#known-limitations) with named sessions, that causes the driver to | |
| 137 | +unexpectedly block the calling thread in certain circumstances; if you use a fully asynchronous model, you should use a | |
| 138 | +session with no keyspace. | |
| 139 | + | |
| 140 | +Finally, if you issue a `USE` statement, it will change the default keyspace on that session: | |
| 141 | + | |
| 142 | +```java | |
| 143 | +Session session = cluster.connect(); | |
| 144 | +// No default keyspace set, need to prefix: | |
| 145 | +session.execute("select * from myKeyspace.myTable where id = 1"); | |
| 146 | + | |
| 147 | +session.execute("USE myKeyspace"); | |
| 148 | +// Now the keyspace is set, unqualified query works: | |
| 149 | +session.execute("select * from myTable where id = 1"); | |
| 150 | +``` | |
| 151 | + | |
| 152 | +Be very careful though: if the session is shared by multiple threads, switching the keyspace at runtime could easily | |
| 153 | +cause unexpected query failures. | |
| 154 | + | |
| 155 | +Generally, the recommended approach is to use a single session with no keyspace, and prefix all your queries. | |
| 156 | + | |
| 157 | + | |
| 36 | 158 | ### More information |
| 37 | 159 | |
| 38 | 160 | If you're reading this from the [generated HTML documentation on |
| @@ -42,6 +164,15 @@ | ||
| 42 | 164 | simply navigate to each sub-directory. |
| 43 | 165 | |
| 44 | 166 | [Cluster]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Cluster.html |
| 167 | +[Cluster.Builder]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Cluster.Builder.html | |
| 168 | +[Initializer]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Cluster.Initializer.html | |
| 45 | 169 | [Session]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Session.html |
| 46 | 170 | [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 | |
| No newline at the end of the file | ||
| 171 | +[Row]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Row.html | |
| 172 | +[NettyOptions]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/NettyOptions.html | |
| 173 | +[QueryOptions]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/QueryOptions.html | |
| 174 | +[SocketOptions]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/SocketOptions.html | |
| 175 | +[Host.StateListener]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Host.StateListener.html | |
| 176 | +[LatencyTracker]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/LatencyTracker.html | |
| 177 | +[SchemaChangeListener]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/SchemaChangeListener.html | |
| 178 | +[NoHostAvailableException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/NoHostAvailableException.html | |
| No newline at the end of the file |
added manual/auth/README.md +5 −0
| @@ -0,0 +1,5 @@ | ||
| 1 | +## Authentication | |
| 2 | + | |
| 3 | +*Coming soon... In the meantime, see the javadoc for [AuthProvider].* | |
| 4 | + | |
| 5 | +[AuthProvider]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/AuthProvider.html | |
| No newline at the end of the file |
added manual/control_connection/README.md +10 −0
| @@ -0,0 +1,10 @@ | ||
| 1 | +## Control connection | |
| 2 | + | |
| 3 | +*Coming soon...* | |
| 4 | + | |
| 5 | +<!-- | |
| 6 | +TODO cover: | |
| 7 | +- what the control connection is (link to init sequence in ../README.md) | |
| 8 | +- server-sent events (gossip + schema changes) | |
| 9 | +- maybe event debouncing (although it sometimes applies to other types of events) | |
| 10 | +--> | |
| No newline at the end of the file |
added manual/load_balancing/README.md +5 −0
| @@ -0,0 +1,5 @@ | ||
| 1 | +## Load balancing | |
| 2 | + | |
| 3 | +*Coming soon... In the meantime, see the javadoc for [LoadBalancingPolicy].* | |
| 4 | + | |
| 5 | +[LoadBalancingPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/LoadBalancingPolicy.html | |
| No newline at the end of the file |
added manual/metrics/README.md +12 −0
| @@ -0,0 +1,12 @@ | ||
| 1 | +## Metrics | |
| 2 | + | |
| 3 | +*Coming soon... In the meantime, see the javadoc for [Metrics].* | |
| 4 | + | |
| 5 | +<!-- | |
| 6 | +TODO cover: | |
| 7 | +- methods on Cluster.builder() | |
| 8 | +- using your own MetricsRegistry (see JAVA-494) | |
| 9 | +- getMetrics() returns null until Cluster initialized | |
| 10 | +--> | |
| 11 | + | |
| 12 | +[Metrics]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Metrics.html | |
| No newline at the end of the file |
added manual/reconnection/README.md +11 −0
| @@ -0,0 +1,11 @@ | ||
| 1 | +## Reconnection | |
| 2 | + | |
| 3 | +*Coming soon... In the meantime, see the javadoc for [ReconnectionPolicy].* | |
| 4 | + | |
| 5 | +<!-- | |
| 6 | +TODO cover: | |
| 7 | +- reconnection policy | |
| 8 | +- scheduled reconnections vs. gossip events | |
| 9 | +--> | |
| 10 | + | |
| 11 | +[ReconnectionPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/ReconnectionPolicy.html | |
| No newline at the end of the file |
added manual/retries/README.md +6 −0
| @@ -0,0 +1,6 @@ | ||
| 1 | +## Retries | |
| 2 | + | |
| 3 | +*Coming soon... In the meantime, see the javadoc for [RetryPolicy].* | |
| 4 | + | |
| 5 | + | |
| 6 | +[RetryPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.html | |
| No newline at the end of the file |