kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 91d30dbebd4b18440e3a6b41ae457b05f2a3edd0a92bfb7bab12c73efba0ae35 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-01-10 18:10 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-01-15 17:54 +01:00 |
| Parents | 66730eaae48a |
3 files changed
modified manual/README.md +117 −0
| @@ -155,6 +155,123 @@ | ||
| 155 | 155 | Generally, the recommended approach is to use a single session with no keyspace, and prefix all your queries. |
| 156 | 156 | |
| 157 | 157 | |
| 158 | +### Running queries | |
| 159 | + | |
| 160 | +You run queries with the session's `execute` method: | |
| 161 | + | |
| 162 | +```java | |
| 163 | +ResultSet rs = session.execute("select release_version from system.local"); | |
| 164 | +``` | |
| 165 | + | |
| 166 | +As shown here, the simplest form is to pass a query string directly. You can also pass an instance of | |
| 167 | +[Statement](statements/). | |
| 168 | + | |
| 169 | +#### Processing rows | |
| 170 | + | |
| 171 | +Executing a query produces a [ResultSet], which is an iterable of [Row]. The basic way to process all rows is to use | |
| 172 | +Java's for-each loop: | |
| 173 | + | |
| 174 | +```java | |
| 175 | +for (Row row : rs) { | |
| 176 | + // process the row | |
| 177 | +} | |
| 178 | +``` | |
| 179 | + | |
| 180 | +Note that this will return **all results** without limit (even though the driver might use multiple queries in the | |
| 181 | +background). To handle large result sets, you might want to use a `LIMIT` clause in your CQL query, or use one of the | |
| 182 | +techniques described in the [paging](paging/) documentation. | |
| 183 | + | |
| 184 | +When you know that there is only one row (or are only interested in the first one), the driver provides a convenience | |
| 185 | +method: | |
| 186 | + | |
| 187 | +```java | |
| 188 | +Row row = rs.one(); | |
| 189 | +``` | |
| 190 | + | |
| 191 | +#### Reading columns | |
| 192 | + | |
| 193 | +[Row] provides getters to extract column values; they can be either positional or named: | |
| 194 | + | |
| 195 | +```java | |
| 196 | +Row row = session.execute("select first_name, last_name from users where id = 1").one(); | |
| 197 | + | |
| 198 | +// The two are equivalent: | |
| 199 | +String firstName = row.getString(0); | |
| 200 | +String firstName = row.getString("first_name"); | |
| 201 | +``` | |
| 202 | + | |
| 203 | +##### CQL to Java type mapping | |
| 204 | + | |
| 205 | +<table border="1" style="text-align:center; width:100%;margin-bottom:1em;"> | |
| 206 | + <tr> <td><b>CQL3 data type</b></td> <td><b>Getter name</b></td> <td><b>Java type</b></td> </tr> | |
| 207 | + <tr> <td>ascii</td> <td>getString</td> <td>java.lang.String</td> </tr> | |
| 208 | + <tr> <td>bigint</td> <td>getLong</td> <td>long</td> </tr> | |
| 209 | + <tr> <td>blob</td> <td>getBytes</td> <td>java.nio.ByteBuffer</td> </tr> | |
| 210 | + <tr> <td>boolean</td> <td>getBool</td> <td>boolean</td> </tr> | |
| 211 | + <tr> <td>counter</td> <td>getLong</td> <td>long</td> </tr> | |
| 212 | + <tr> <td>decimal</td> <td>getDecimal</td> <td>java.math.BigDecimal</td> </tr> | |
| 213 | + <tr> <td>double</td> <td>getDouble</td> <td>double</td> </tr> | |
| 214 | + <tr> <td>float</td> <td>getFloat</td> <td>float</td> </tr> | |
| 215 | + <tr> <td>inet</td> <td>getInet</td> <td>java.net.InetAddress</td> </tr> | |
| 216 | + <tr> <td>int</td> <td>getInt</td> <td>int</td> </tr> | |
| 217 | + <tr> <td>list</td> <td>getList</td> <td>java.util.List<T></td> </tr> | |
| 218 | + <tr> <td>map</td> <td>getMap</td> <td>java.util.Map<K, V></td> </tr> | |
| 219 | + <tr> <td>set</td> <td>getSet</td> <td>java.util.Set<T></td> </tr> | |
| 220 | + <tr> <td>text</td> <td>getString</td> <td>java.lang.String</td> </tr> | |
| 221 | + <tr> <td>timestamp</td> <td>getDate</td> <td>java.util.Date</td> </tr> | |
| 222 | + <tr> <td>timeuuid</td> <td>getUUID</td> <td>java.util.UUID</td> </tr> | |
| 223 | + <tr> <td>tuple</td> <td>getTupleValue</td> <td><a href="tuples/">TupleValue</a></td> </tr> | |
| 224 | + <tr> <td>user-defined types</td> <td>getUDTValue</td> <td><a href="udts/">UDTValue</a></td> </tr> | |
| 225 | + <tr> <td>uuid</td> <td>getUUID</td> <td>java.util.UUID</td> </tr> | |
| 226 | + <tr> <td>varchar</td> <td>getString</td> <td>java.lang.String</td> </tr> | |
| 227 | + <tr> <td>varint</td> <td>getVarint</td> <td>java.math.BigInteger</td> </tr> | |
| 228 | +</table> | |
| 229 | + | |
| 230 | +##### Primitive types | |
| 231 | + | |
| 232 | +For performance reasons, the driver uses primitive Java types wherever possible (`boolean`, `int`...); the CQL value | |
| 233 | +`NULL` is encoded as the type's default value (`false`, `0`...), which can be ambiguous. To distinguish `NULL` from | |
| 234 | +actual values, use `isNull`: | |
| 235 | + | |
| 236 | +```java | |
| 237 | +Integer age = row.isNull("age") ? null : row.getInt("age"); | |
| 238 | +``` | |
| 239 | + | |
| 240 | +##### Collection types | |
| 241 | + | |
| 242 | +To ensure type safety, collection getters are generic. You need to provide type parameters matching your CQL type when | |
| 243 | +calling the methods: | |
| 244 | + | |
| 245 | +```java | |
| 246 | +// Assuming given_names is a list<text>: | |
| 247 | +List<String> givenNames = row.getList("given_names", String.class); | |
| 248 | +``` | |
| 249 | + | |
| 250 | +For nested collections, element types are generic and cannot be expressed as Java `Class` instances. We use Guava's | |
| 251 | +[TypeToken](https://github.com/google/guava/wiki/ReflectionExplained) instead: | |
| 252 | + | |
| 253 | +```java | |
| 254 | +// Assuming teams is a set<list<text>>: | |
| 255 | +TypeToken<List<String>> listOfStrings = new TypeToken<List<String>>() {}; | |
| 256 | +Set<List<String>> teams = row.getSet("teams", listOfStrings); | |
| 257 | +``` | |
| 258 | + | |
| 259 | +Since type tokens are anonymous inner classes, it's recommended to store them as constants in a utility class instead of | |
| 260 | +re-creating them each time. | |
| 261 | + | |
| 262 | +##### Row metadata | |
| 263 | + | |
| 264 | +`Row` exposes an API to explore the column metadata at runtime: | |
| 265 | + | |
| 266 | +```java | |
| 267 | +for (ColumnDefinitions.Definition definition : row.getColumnDefinitions()) { | |
| 268 | + System.out.printf("Column %s has type %s%n", | |
| 269 | + definition.getName(), | |
| 270 | + definition.getType()); | |
| 271 | +} | |
| 272 | +``` | |
| 273 | + | |
| 274 | + | |
| 158 | 275 | ### More information |
| 159 | 276 | |
| 160 | 277 | If you're reading this from the [generated HTML documentation on |
added manual/tuples/README.md +5 −0
| @@ -0,0 +1,5 @@ | ||
| 1 | +## Tuples | |
| 2 | + | |
| 3 | +*Coming soon... In the meantime, see the javadoc for [TupleType].* | |
| 4 | + | |
| 5 | +[TupleType]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/TupleType.html | |
| No newline at the end of the file |
added manual/udts/README.md +5 −0
| @@ -0,0 +1,5 @@ | ||
| 1 | +## User-defined types | |
| 2 | + | |
| 3 | +*Coming soon... In the meantime, see the javadoc for [UserType].* | |
| 4 | + | |
| 5 | +[UserType]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/UserType.html | |
| No newline at the end of the file |