| 1 | Upgrade Guide to 2.0 |
| 2 | ==================== |
| 3 | |
| 4 | The purpose of this guide is to detail the changes made by version 2.0 of |
| 5 | the Java Driver. |
| 6 | |
| 7 | |
| 8 | 1.0 to 2.0 |
| 9 | ---------- |
| 10 | |
| 11 | We used the opportunity of a major version bump to incorporate your feedback |
| 12 | and improve the API, to fix a number of inconsistencies and remove cruft. |
| 13 | Unfortunately this means there are some breaking changes, but the new API should |
| 14 | be both simpler and more complete. |
| 15 | |
| 16 | The following describes the changes for 2.0 that are breaking changes of the |
| 17 | 1.0 API. For ease of use, we distinguish two categories of API changes: the "main" |
| 18 | ones and the "other" ones. |
| 19 | |
| 20 | The "main" API changes are the ones that are either |
| 21 | likely to affect most upgraded apps or are incompatible changes that, even if minor, |
| 22 | will not be detected at compile time. Upgraders are highly encouraged to check |
| 23 | this list of "main" changes while upgrading their application to 2.0 (even |
| 24 | though most applications are likely to be affected by only a handful of |
| 25 | changes). |
| 26 | |
| 27 | The "other" list is, well, other changes: those that are likely to |
| 28 | affect a minor number of applications and will be detected by compile time |
| 29 | errors anyway. It is ok to skip those initially and only come back to them if |
| 30 | you have trouble compiling your application after an upgrade. |
| 31 | |
| 32 | Main API changes |
| 33 | ~~~~~~~~~~~~~~~~ |
| 34 | |
| 35 | 1. The ``Query`` class has been renamed into ``Statement`` (it was confusing to some |
| 36 | that the ``BoundStatement`` was not a ``Statement``). |
| 37 | |
| 38 | To allow this, the old ``Statement`` class has been renamed to ``RegularStatement``. |
| 39 | |
| 40 | 2. The ``Cluster`` and ``Session`` shutdown API has changed. There is now a ``closeAsync`` |
| 41 | that is asynchronous but returns a ``Future`` on the completion of the shutdown |
| 42 | process. |
| 43 | |
| 44 | There is also a ``close`` shortcut that does the same but blocks. |
| 45 | Also, ``close`` now waits for ongoing queries to complete by default (but you |
| 46 | can force the closing of all connections if you want to). |
| 47 | |
| 48 | 3. ``NoHostAvailableException#getErrors`` now returns the full exception objects for |
| 49 | each node instead of just a message. In other words, it returns a |
| 50 | ``Map<InetAddress, Throwable>`` instead of a ``Map<InetAddress, String>``. |
| 51 | |
| 52 | 4. ``Statement#getConsistencyLevel`` (previously ``Query#getConsistencyLevel``, see |
| 53 | first point) will now return ``null`` by default (instead of ``CL.ONE``), with the |
| 54 | meaning of "use the default consistency level". |
| 55 | |
| 56 | The default consistency level can now be configured through the new ``QueryOptions`` |
| 57 | object in the cluster ``Configuration``. |
| 58 | |
| 59 | 5. The ``Metrics`` class now uses the Codahale metrics library version 3 (version 2 was |
| 60 | used previously). This new major version of the library has many API changes |
| 61 | compared to its version 2 (see http://metrics.codahale.com/about/release-notes/ for details), |
| 62 | which can thus impact consumers of the Metrics class. |
| 63 | Furthermore, the default JmxReporter now includes a name specific to the |
| 64 | cluster instance (to avoid conflicts when multiple Cluster instances are created |
| 65 | in the same JVM). As a result, tools that were polling JMX info will |
| 66 | have to be updated accordingly. |
| 67 | |
| 68 | 6. The ``QueryBuilder#in`` method now has the following special case: using |
| 69 | ``QueryBuilder.in(QueryBuilder.bindMarker())`` will generate the string ``IN ?``, |
| 70 | not ``IN (?)`` as was the case in 1.0. The reasoning being that the former |
| 71 | syntax, made valid by https://issues.apache.org/jira/browse/CASSANDRA-4210 |
| 72 | is a lot more useful than "IN (?)", as the latter can more simply use an |
| 73 | equality. |
| 74 | |
| 75 | Note that if you really want to output "IN (?)" with the query |
| 76 | builder, you can use QueryBuilder.in(QueryBuilder.raw("?")). |
| 77 | |
| 78 | 7. When binding values by name in ``BoundStatement`` (i.e. using the |
| 79 | ``setX(String, X)`` methods), if more than one variable have the same name, |
| 80 | then all values corresponding to that variable |
| 81 | name are set instead of just the first occurrence. |
| 82 | |
| 83 | 8. The ``QueryBuilder#raw`` method does not automatically add quotes anymore, but |
| 84 | rather output its result without any change (as the raw name implies). |
| 85 | |
| 86 | This means for instance that ``eq("x", raw(foo))`` will output ``x = foo``, |
| 87 | not ``x = 'foo'`` (you don't need the raw method to output the latter string). |
| 88 | |
| 89 | |
| 90 | 9. The ``QueryBuilder`` will now sometime use the new ability to send value as |
| 91 | bytes instead of serializing everything to string. In general the QueryBuilder |
| 92 | will do the right thing, but if you were calling the ``getQueryString()`` method |
| 93 | on a Statement created with a QueryBuilder (for other reasons than to prepare a query) |
| 94 | then the returned string may contain bind markers in place of some of the values |
| 95 | provided (and in that case, ``getValues()`` will contain the values corresponding |
| 96 | to those markers). If need be, it is possible to force the old behavior by |
| 97 | using the new ``setForceNoValues()`` method. |
| 98 | |
| 99 | |
| 100 | Other API Changes |
| 101 | ~~~~~~~~~~~~~~~~~ |
| 102 | |
| 103 | 1. Creating a Cluster instance (through Cluster#buildFrom or the |
| 104 | Cluster.Builder#build method) **does not create any connection right away |
| 105 | anymore** (and thus cannot throw a NoHostAvailableException or an |
| 106 | AuthenticationException). Instead, the initial contact points are checked |
| 107 | the first time a call to ``Cluster#connect`` is done. If for some reason you |
| 108 | want to emulate the previous behavior, you can use the new method |
| 109 | Cluster#init: Cluster.builder().build() in 1.0 is equivalent to |
| 110 | Cluster.builder().build().init() in 2.0. |
| 111 | |
| 112 | 2. Methods from Metadata, KeyspaceMetadata and TableMetadata now use by default |
| 113 | case insensitive identifiers (for keyspace, table and column names in |
| 114 | parameter). You can double-quote an identifier if you want it to be a |
| 115 | case sensitive one (as you would do in CQL) and there is a Metadata.quote |
| 116 | helper method for that. |
| 117 | |
| 118 | 3. The TableMetadata#getClusteringKey method has been renamed |
| 119 | TableMetadata#getClusteringColumns to match the "official" vocabulary. |
| 120 | |
| 121 | 4. The UnavailableException#getConsistency method has been renamed to |
| 122 | UnavailableException#getConsistencyLevel for consistency with the method of |
| 123 | QueryTimeoutException. |
| 124 | |
| 125 | 5. The RegularStatement class (ex-Statement class, see above) must now |
| 126 | implement two additional methods: RegularStatement#getKeyspace and |
| 127 | RegularStatement#getValues. If you had extended this class, you will have to |
| 128 | implement those new methods, but both can return null if they are not useful |
| 129 | in your case. |
| 130 | |
| 131 | 6. The Cluster.Initializer interface should now implement 2 new methods: |
| 132 | Cluster.Initializer#getInitialListeners (which can return an empty |
| 133 | collection) and Cluster.Initializer#getClusterName (which can return null). |
| 134 | |
| 135 | 7. The Metadata#getReplicas method now takes 2 arguments. On top of the |
| 136 | partition key, you must now provide the keyspace too. The previous behavior |
| 137 | was buggy: it's impossible to properly return the full list of replica for a |
| 138 | partition key without knowing the keyspace since replication may depend on |
| 139 | the keyspace). |
| 140 | |
| 141 | 8. The method LoadBalancingPolicy#newQueryPlan() method now takes the currently |
| 142 | logged keyspace as 2nd argument. This information is necessary to do proper |
| 143 | token aware balancing (see preceding point). |
| 144 | |
| 145 | 9. The ResultSetFuture#set and ResultSetFuture#setException methods have been |
| 146 | removed (from the public API at least). They were never meant to be exposed |
| 147 | publicly: a resultSetFuture is always set by the driver itself and should |
| 148 | not be set manually. |
| 149 | |
| 150 | 10. The deprecated since 1.0.2 Host.HealthMonitor class has been removed. You |
| 151 | will now need to use Host#isUp and Cluster#register if you were using that |
| 152 | class. |
| 153 | |
| 154 | |
| 155 | 2.0.10 |
| 156 | ------ |
| 157 | |
| 158 | 1. LatencyTracker#update now has a different signature and takes two new |
| 159 | parameters: the statement that has been executed (never null), and the exception |
| 160 | thrown while executing the query (or null, if the query executed successfully). |
| 161 | Existing implementations of this interface, once upgraded to the new method |
| 162 | signature, should continue to work as before. |
| 163 | |
| 164 | 2. SocketOptions#getTcpNoDelay() is now TRUE by default (it was previously undefined). |
| 165 | This reflects the new behavior of Netty (which was upgraded from version 3.9.0 to |
| 166 | 4.0.27): TCP_NODELAY is now turned on by default, instead of depending on the OS |
| 167 | default like in previous versions. |
| 168 | |
| 169 | 3. Netty is not shaded anymore in the default Maven artifact. However we publish a |
| 170 | shaded artifact under a different classifier. See `Using the shaded JAR<http://datastax.github.io/java-driver/features/shaded_jar/>`_ |
| 171 | for more information. |
| 172 | |
| 173 | 4. The internal initialization sequence of the Cluster object has been slightly changed: |
| 174 | some fields that were previously initialized in the constructor are now set when |
| 175 | the init() method is called. This is unlikely to affect regular driver users. |
| 176 | |
| 177 | |
| 178 | Features available only with Cassandra 2.0 |
| 179 | ------------------------------------------ |
| 180 | |
| 181 | This section details the biggest additions to 2.0 API wise. It is not an |
| 182 | exhaustive list of new features in 2.0. |
| 183 | |
| 184 | 1. The new ``BatchStatement`` class allows to group any type of insert Statements |
| 185 | (BoundStatement or RegularStatement) for execution as a batch. For instance, |
| 186 | you can do something like:: |
| 187 | |
| 188 | List<String> values = ...; |
| 189 | PreparedStatement ps = session.prepare("INSERT INTO myTable(value) VALUES (?)"); |
| 190 | BatchStatement bs = new BatchStatement(); |
| 191 | for (String value : values) |
| 192 | bs.add(ps.bind(value)); |
| 193 | session.execute(bs); |
| 194 | |
| 195 | 2. ``SimpleStatement`` can now take a list of values in addition to the query. This |
| 196 | allows to do the equivalent of a prepare+execute but with only one round-trip |
| 197 | to the server and without keeping the prepared statement after the |
| 198 | execution. |
| 199 | |
| 200 | This is typically useful if a given query should be executed only |
| 201 | once (i.e. you don't want to prepare it) but you also don't want to |
| 202 | serialize all values into strings. Shortcut Session#execute() and |
| 203 | Session#executeAsync() methods are also provided so you that you can do:: |
| 204 | |
| 205 | String imgName = ...; |
| 206 | ByteBuffer imgBytes = ...; |
| 207 | session.execute("INSERT INTO images(name, bytes) VALUES (?, ?)", imgName, imgBytes); |
| 208 | |
| 209 | 3. SELECT queries are now "paged" under the hood. In other words, if a query |
| 210 | yields a very large result, only the beginning of the ResultSet will be fetch |
| 211 | initially, the rest being fetch "on-demand". In practice, this means that:: |
| 212 | |
| 213 | for (Row r : session.execute("SELECT * FROM mytable")) |
| 214 | ... process r ... |
| 215 | |
| 216 | should not timeout or OOM the server anymore even if "mytable" contains a lot |
| 217 | of data. In general paging should be transparent for the application (as in |
| 218 | the example above), but the implementation provides a number of knobs to |
| 219 | fine tune the behavior of that paging: |
| 220 | |
| 221 | * the size of each "page" can be set per-query (Statement#setFetchSize()) |
| 222 | * the ResultSet object provides 2 methods to check the state of paging |
| 223 | (ResultSet#getAvailableWithoutFetching and ResultSet#isFullyFetched) |
| 224 | as well as a mean to force the pre-fetching of the next page (ResultSet#fetchMoreResults). |