| 1 | ## Frequently Asked Questions |
| 2 | |
| 3 | ### How do I implement paging? |
| 4 | |
| 5 | When using [native protocol](../manual/native_protocol/) version 2 or |
| 6 | higher, the driver automatically pages large result sets under the hood. |
| 7 | You can also save the paging state to resume iteration later. See [this |
| 8 | page](../manual/paging/) for more information. |
| 9 | |
| 10 | Native protocol v1 does not support paging, but you can emulate it in |
| 11 | CQL 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 | |
| 17 | When executing a conditional statement, the `ResultSet` will contain a single `Row` with a |
| 18 | column named "applied" of type boolean. This tells whether the conditional statement was |
| 19 | successful or not. |
| 20 | |
| 21 | The driver provides a convenience method [wasApplied] to check this on the result set directly: |
| 22 | |
| 23 | ```java |
| 24 | ResultSet rset = session.execute(conditionalStatement); |
| 25 | rset.wasApplied(); |
| 26 | ``` |
| 27 | |
| 28 | You may also inspect the value yourself: |
| 29 | |
| 30 | ```java |
| 31 | ResultSet rset = session.execute(conditionalStatement); |
| 32 | Row row = rset.one(); |
| 33 | row.getBool(0); // this is equivalent row.getBool("applied") |
| 34 | ``` |
| 35 | |
| 36 | Note 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 | |
| 43 | Starting with Cassandra 2.0, normal statements (that is non-prepared statements) do |
| 44 | not need to concatenate parameter values inside a query string. Instead you can use |
| 45 | `?` markers and provide the values separately: |
| 46 | |
| 47 | ```java |
| 48 | session.execute( "INSERT INTO contacts (email, firstname, lastname) |
| 49 | VALUES (?, ?, ?)", "clint.barton@hawkeye.com", "Barney", "Barton"); |
| 50 | ``` |
| 51 | |
| 52 | See [Simple statements](../manual/statements/simple/) for more information. |
| 53 | |
| 54 | |
| 55 | ### Does a parameterized statement escape parameters? |
| 56 | |
| 57 | A 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 |
| 59 | parameters. |
| 60 | |
| 61 | |
| 62 | ### What's the difference between a parameterized statement and a Prepared statement? |
| 63 | |
| 64 | The only similarity between a parameterized statement and a prepared statement is in |
| 65 | the 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 | |
| 72 | See [Prepared statements](../manual/statements/prepared/) for more information. |
| 73 | |
| 74 | |
| 75 | ### Can I combine `PreparedStatements` and normal statements in a batch? |
| 76 | |
| 77 | Yes. A batch can include both bound statements and simple statements: |
| 78 | |
| 79 | ```java |
| 80 | PreparedStatement ps = session.prepare( "INSERT INTO contacts (email, firstname, lastname) |
| 81 | VALUES (?, ?, ?)"); |
| 82 | BatchStatement batch = new BatchStatement(); |
| 83 | batch.add(ps.bind(...)); |
| 84 | batch.add(ps.bind(...)); |
| 85 | // here's a simple statement |
| 86 | batch.add(new SimpleStatement( "INSERT INTO contacts (email, firstname, lastname) VALUES (?, ?, ?)", ...)); |
| 87 | session.execute(batch); |
| 88 | ``` |
| 89 | |
| 90 | |
| 91 | ### Why do my 'SELECT *' `PreparedStatement`-based queries stop working after a schema change? |
| 92 | |
| 93 | Both the driver and Cassandra maintain a mapping of `PreparedStatement` queries to their |
| 94 | metadata. When a change is made to a table, such as a column being added or dropped, there |
| 95 | is currently no mechanism for Cassandra to invalidate the existing metadata. Because of this, |
| 96 | the driver is not able to properly react to these changes and will improperly read rows after |
| 97 | a schema change is made. |
| 98 | |
| 99 | See [Prepared statements](../manual/statements/prepared) for more information. |
| 100 | |
| 101 | |
| 102 | ### Can I get the raw bytes of a text column? |
| 103 | |
| 104 | If you need to access the raw bytes of a text column, call the |
| 105 | `Row.getBytesUnsafe("columnName")` method. |
| 106 | |
| 107 | Trying to use `Row.getBytes("columnName")` for the same purpose results in an |
| 108 | exception, as the `getBytes` method can only be used if the column has the CQL type `BLOB`. |
| 109 | |
| 110 | |
| 111 | ### How do I increment counters with `QueryBuilder`? |
| 112 | |
| 113 | Considering the following query: |
| 114 | |
| 115 | ```java |
| 116 | UPDATE clickstream SET clicks = clicks + 1 WHERE userid = id; |
| 117 | ``` |
| 118 | |
| 119 | To do this using `QueryBuilder`: |
| 120 | |
| 121 | ```java |
| 122 | Statement query = QueryBuilder.update("clickstream") |
| 123 | .with(incr("clicks", 1)) // Use incr for counters |
| 124 | .where(eq("userid", id)); |
| 125 | ``` |
| 126 | |
| 127 | |
| 128 | ### Is there a way to control the batch size of the results returned from a query? |
| 129 | |
| 130 | Use the `setFetchSize()` method on your `Statement` object. The fetch size controls |
| 131 | how many resulting rows are retrieved simultaneously (the goal being to avoid |
| 132 | loading too many results in memory for queries yielding large result sets). |
| 133 | |
| 134 | Keep in mind that if your code iterates the `ResultSet` entirely, the driver may |
| 135 | run additional background queries to fetch the rest of the data. The fetch size |
| 136 | only affects what is retrieved at a time, not the overall number of rows. |
| 137 | |
| 138 | See [Paging](../manual/paging/) for more information. |
| 139 | |
| 140 | |
| 141 | ### What's the difference between using `setFetchSize()` and `LIMIT`? |
| 142 | |
| 143 | Basically, `LIMIT` controls the maximum number of results returned by the query, |
| 144 | while the `setFetchSize()` method controls the amount of data transferred at a time. |
| 145 | |
| 146 | For example, if you limit is 30 and your fetch size is 10, the `ResultSet` will contain |
| 147 | 30 rows, but under the hood the driver will perform 3 requests that will transfer 10 |
| 148 | rows each. |
| 149 | |
| 150 | See [Paging](../manual/paging/) for more information. |
| 151 | |
| 152 | |
| 153 | ### I'm reading a BLOB column and the driver returns incorrect data. |
| 154 | |
| 155 | Check your code to ensure that you read the returned `ByteBuffer` correctly. `ByteBuffer` is a very error-prone API, |
| 156 | and we've had many reports where the problem turned out to be in user code. |
| 157 | |
| 158 | See [Blobs.java] in the `driver-examples` module for some examples and explanations. |
| 159 | |
| 160 | [Blobs.java]: https://github.com/datastax/java-driver/tree/3.0/driver-examples/src/main/java/com/datastax/driver/examples/datatypes/Blobs.java |
| 161 | |
| 162 | |
| 163 | ### How do I use the driver in an OSGi application? |
| 164 | |
| 165 | Read our [OSGi-specific FAQ section](osgi/) to find out. |