Elffuss BraidHub

kikocis/java-driver public

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