Elffuss BraidHub

kikocis/java-driver public

7beeb2fc4b47 · 5.5 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
15### Can I check if a conditional statement (lightweight transaction) was successful?
16
17When executing a conditional statement, the `ResultSet` will contain a single `Row` with a
18column named "applied" of type boolean. This tells whether the conditional statement was
19successful or not.
20
21The driver provides a convenience method [wasApplied] to check this on the result set directly:
22
23```java
24ResultSet rset = session.execute(conditionalStatement);
25rset.wasApplied();
26```
27
28You may also inspect the value yourself:
29
30```java
31ResultSet rset = session.execute(conditionalStatement);
32Row row = rset.one();
33row.getBool(0); // this is equivalent row.getBool("applied")
34```
35
36Note that, unlike manual inspection, `wasApplied` does not consume the first row.
37
38[wasApplied]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ResultSet.html#wasApplied--
39
40
41### What is a parameterized statement and how can I use it?
42
43Starting with Cassandra 2.0, normal statements (that is non-prepared statements) do
44not need to concatenate parameter values inside a query string. Instead you can use
45`?` markers and provide the values separately:
46
47```java
48session.execute( "INSERT INTO contacts (email, firstname, lastname)
49 VALUES (?, ?, ?)", "clint.barton@hawkeye.com", "Barney", "Barton");
50```
51
52See [Simple statements](../manual/statements/simple/) for more information.
53
54
55### Does a parameterized statement escape parameters?
56
57A parameterized statement sends the values of parameters separate from the query
58(similar to the way a prepared statement does) as bytes so there is no need to escape
59parameters.
60
61
62### What's the difference between a parameterized statement and a Prepared statement?
63
64The only similarity between a parameterized statement and a prepared statement is in
65the way that the parameters are sent. The difference is that a prepared statement:
66
67* is already known on the cluster side (it has been compiled and there is an execution
68 plan available for it) which leads to better performance
69* sends only the statement id and its parameters (thus reducing the amount of data sent
70 to the cluster)
71
72See [Prepared statements](../manual/statements/prepared/) for more information.
73
74
75### Can I combine `PreparedStatements` and normal statements in a batch?
76
77Yes. A batch can include both bound statements and simple statements:
78
79```java
80PreparedStatement ps = session.prepare( "INSERT INTO contacts (email, firstname, lastname)
81 VALUES (?, ?, ?)");
82BatchStatement batch = new BatchStatement();
83batch.add(ps.bind(...));
84batch.add(ps.bind(...));
85// here's a simple statement
86batch.add(new SimpleStatement( "INSERT INTO contacts (email, firstname, lastname) VALUES (?, ?, ?)", ...));
87session.execute(batch);
88```
89
90
91### Can I get the raw bytes of a text column?
92
93If you need to access the raw bytes of a text column, call the
94`Row.getBytesUnsafe("columnName")` method.
95
96Trying to use `Row.getBytes("columnName")` for the same purpose results in an
97exception, as the `getBytes` method can only be used if the column has the CQL type `BLOB`.
98
99
100### How do I increment counters with `QueryBuilder`?
101
102Considering the following query:
103
104```java
105UPDATE clickstream SET clicks = clicks + 1 WHERE userid = id;
106```
107
108To do this using `QueryBuilder`:
109
110```java
111Statement query = QueryBuilder.update("clickstream")
112 .with(incr("clicks", 1)) // Use incr for counters
113 .where(eq("userid", id));
114```
115
116
117### Is there a way to control the batch size of the results returned from a query?
118
119Use the `setFetchSize()` method on your `Statement` object. The fetch size controls
120how many resulting rows are retrieved simultaneously (the goal being to avoid
121loading too many results in memory for queries yielding large result sets).
122
123Keep in mind that if your code iterates the `ResultSet` entirely, the driver may
124run additional background queries to fetch the rest of the data. The fetch size
125only affects what is retrieved at a time, not the overall number of rows.
126
127See [Paging](../manual/paging/) for more information.
128
129
130### What's the difference between using `setFetchSize()` and `LIMIT`?
131
132Basically, `LIMIT` controls the maximum number of results returned by the query,
133while the `setFetchSize()` method controls the amount of data transferred at a time.
134
135For example, if you limit is 30 and your fetch size is 10, the `ResultSet` will contain
13630 rows, but under the hood the driver will perform 3 requests that will transfer 10
137rows each.
138
139See [Paging](../manual/paging/) for more information.
140
141
142### I'm reading a BLOB column and the driver returns incorrect data.
143
144Check your code to ensure that you read the returned `ByteBuffer` correctly. `ByteBuffer` is a very error-prone API,
145and we've had many reports where the problem turned out to be in user code.
146
147See [Blobs.java] in the `driver-examples` module for some examples and explanations.
148
149[Blobs.java]: https://github.com/datastax/java-driver/tree/3.0.x/driver-examples/src/main/java/com/datastax/driver/examples/datatypes/Blobs.java