Elffuss BraidHub

kikocis/java-driver public

77adf3ba1b9a · 5.0 KiBRenderedRawPermalink
1## Frequently Asked Questions
2
3### How do I implement paging?
4
5When using [native protocol](../manual/native_protocol/) version 2 or
6higher, the driver automatically pages large result sets under the hood.
7You can also save the paging state to resume iteration later. See [this
8page](../manual/paging/) for more information.
9
10Native protocol v1 does not support paging, but you can emulate it in
11CQL with `LIMIT` and the `token()` function. See
12[this conversation](https://groups.google.com/a/lists.datastax.com/d/msg/java-driver-user/U2KzAHruWO4/6vDmUVDDkOwJ) on the mailing list.
13
14### Can I check if a conditional statement (lightweight transaction) was successful?
15
16When executing a conditional statement, the `ResultSet` will contain a single `Row` with a
17column named "applied" of type boolean. This tells whether the conditional statement was
18successful or not.
19
20The driver provides a convenience method [wasApplied] to check this on the result set directly:
21
22```java
23ResultSet rset = session.execute(conditionalStatement);
24rset.wasApplied();
25```
26
27You may also inspect the value yourself:
28
29```java
30ResultSet rset = session.execute(conditionalStatement);
31Row row = rset.one();
32row.getBool(0); // this is equivalent row.getBool("applied")
33```
34
35Note that, unlike manual inspection, `wasApplied` does not consume the first row.
36
37[wasApplied]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ResultSet.html#wasApplied--
38
39### What is a parameterized statement and how can I use it?
40
41Starting with Cassandra 2.0, normal statements (that is non-prepared statements) do
42not need to concatenate parameter values inside a query string. Instead you can use
43`?` markers and provide the values separately:
44
45```java
46session.execute( "INSERT INTO contacts (email, firstname, lastname)
47 VALUES (?, ?, ?)", "clint.barton@hawkeye.com", "Barney", "Barton");
48```
49
50See [Simple statements](../manual/statements/simple/) for more information.
51
52### Does a parameterized statement escape parameters?
53
54A parameterized statement sends the values of parameters separate from the query
55(similar to the way a prepared statement does) as bytes so there is no need to escape
56parameters.
57
58### What's the difference between a parameterized statement and a Prepared statement?
59
60The only similarity between a parameterized statement and a prepared statement is in
61the way that the parameters are sent. The difference is that a prepared statement:
62
63* is already known on the cluster side (it has been compiled and there is an execution
64 plan available for it) which leads to better performance
65* sends only the statement id and its parameters (thus reducing the amount of data sent
66 to the cluster)
67
68See [Prepared statements](../manual/statements/prepared/) for more information.
69
70### Can I combine `PreparedStatements` and normal statements in a batch?
71
72Yes. A batch can include both bound statements and simple statements:
73
74```java
75PreparedStatement ps = session.prepare( "INSERT INTO contacts (email, firstname, lastname)
76 VALUES (?, ?, ?)");
77BatchStatement batch = new BatchStatement();
78batch.add(ps.bind(...));
79batch.add(ps.bind(...));
80// here's a simple statement
81batch.add(new SimpleStatement( "INSERT INTO contacts (email, firstname, lastname) VALUES (?, ?, ?)", ...));
82session.execute(batch);
83```
84
85### Can I get the raw bytes of a text column?
86
87If you need to access the raw bytes of a text column, call the
88`Row.getBytesUnsafe("columnName")` method.
89
90Trying to use `Row.getBytes("columnName")` for the same purpose results in an
91exception, as the `getBytes` method can only be used if the column has the CQL type `BLOB`.
92
93### How do I increment counters with `QueryBuilder`?
94
95Considering the following query:
96
97```java
98UPDATE clickstream SET clicks = clicks + 1 WHERE userid = id;
99```
100
101To do this using `QueryBuilder`:
102
103```java
104Statement query = QueryBuilder.update("clickstream")
105 .with(incr("clicks", 1)) // Use incr for counters
106 .where(eq("userid", id));
107```
108
109### Is there a way to control the batch size of the results returned from a query?
110
111Use the `setFetchSize()` method on your `Statement` object. The fetch size controls
112how many resulting rows are retrieved simultaneously (the goal being to avoid
113loading too many results in memory for queries yielding large result sets).
114
115Keep in mind that if your code iterates the `ResultSet` entirely, the driver may
116run additional background queries to fetch the rest of the data. The fetch size
117only affects what is retrieved at a time, not the overall number of rows.
118
119See [Paging](../manual/paging/) for more information.
120
121### What's the difference between using `setFetchSize()` and `LIMIT`?
122
123Basically, `LIMIT` controls the maximum number of results returned by the query,
124while the `setFetchSize()` method controls the amount of data transferred at a time.
125
126For example, if you limit is 30 and your fetch size is 10, the `ResultSet` will contain
12730 rows, but under the hood the driver will perform 3 requests that will transfer 10
128rows each.
129
130See [Paging](../manual/paging/) for more information.