| 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 | ### Can I check if a conditional statement (lightweight transaction) was successful? |
| 15 | |
| 16 | When executing a conditional statement, the `ResultSet` will contain a single `Row` with a |
| 17 | column named "applied" of type boolean. This tells whether the conditional statement was |
| 18 | successful or not. |
| 19 | |
| 20 | The driver provides a convenience method [wasApplied] to check this on the result set directly: |
| 21 | |
| 22 | ```java |
| 23 | ResultSet rset = session.execute(conditionalStatement); |
| 24 | rset.wasApplied(); |
| 25 | ``` |
| 26 | |
| 27 | You may also inspect the value yourself: |
| 28 | |
| 29 | ```java |
| 30 | ResultSet rset = session.execute(conditionalStatement); |
| 31 | Row row = rset.one(); |
| 32 | row.getBool(0); // this is equivalent row.getBool("applied") |
| 33 | ``` |
| 34 | |
| 35 | Note 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 | |
| 41 | Starting with Cassandra 2.0, normal statements (that is non-prepared statements) do |
| 42 | not need to concatenate parameter values inside a query string. Instead you can use |
| 43 | `?` markers and provide the values separately: |
| 44 | |
| 45 | ```java |
| 46 | session.execute( "INSERT INTO contacts (email, firstname, lastname) |
| 47 | VALUES (?, ?, ?)", "clint.barton@hawkeye.com", "Barney", "Barton"); |
| 48 | ``` |
| 49 | |
| 50 | See [Simple statements](../manual/statements/simple/) for more information. |
| 51 | |
| 52 | ### Does a parameterized statement escape parameters? |
| 53 | |
| 54 | A 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 |
| 56 | parameters. |
| 57 | |
| 58 | ### What's the difference between a parameterized statement and a Prepared statement? |
| 59 | |
| 60 | The only similarity between a parameterized statement and a prepared statement is in |
| 61 | the 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 | |
| 68 | See [Prepared statements](../manual/statements/prepared/) for more information. |
| 69 | |
| 70 | ### Can I combine `PreparedStatements` and normal statements in a batch? |
| 71 | |
| 72 | Yes. A batch can include both bound statements and simple statements: |
| 73 | |
| 74 | ```java |
| 75 | PreparedStatement ps = session.prepare( "INSERT INTO contacts (email, firstname, lastname) |
| 76 | VALUES (?, ?, ?)"); |
| 77 | BatchStatement batch = new BatchStatement(); |
| 78 | batch.add(ps.bind(...)); |
| 79 | batch.add(ps.bind(...)); |
| 80 | // here's a simple statement |
| 81 | batch.add(new SimpleStatement( "INSERT INTO contacts (email, firstname, lastname) VALUES (?, ?, ?)", ...)); |
| 82 | session.execute(batch); |
| 83 | ``` |
| 84 | |
| 85 | ### Can I get the raw bytes of a text column? |
| 86 | |
| 87 | If you need to access the raw bytes of a text column, call the |
| 88 | `Row.getBytesUnsafe("columnName")` method. |
| 89 | |
| 90 | Trying to use `Row.getBytes("columnName")` for the same purpose results in an |
| 91 | exception, 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 | |
| 95 | Considering the following query: |
| 96 | |
| 97 | ```java |
| 98 | UPDATE clickstream SET clicks = clicks + 1 WHERE userid = id; |
| 99 | ``` |
| 100 | |
| 101 | To do this using `QueryBuilder`: |
| 102 | |
| 103 | ```java |
| 104 | Statement 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 | |
| 111 | Use the `setFetchSize()` method on your `Statement` object. The fetch size controls |
| 112 | how many resulting rows are retrieved simultaneously (the goal being to avoid |
| 113 | loading too many results in memory for queries yielding large result sets). |
| 114 | |
| 115 | Keep in mind that if your code iterates the `ResultSet` entirely, the driver may |
| 116 | run additional background queries to fetch the rest of the data. The fetch size |
| 117 | only affects what is retrieved at a time, not the overall number of rows. |
| 118 | |
| 119 | See [Paging](../manual/paging/) for more information. |
| 120 | |
| 121 | ### What's the difference between using `setFetchSize()` and `LIMIT`? |
| 122 | |
| 123 | Basically, `LIMIT` controls the maximum number of results returned by the query, |
| 124 | while the `setFetchSize()` method controls the amount of data transferred at a time. |
| 125 | |
| 126 | For example, if you limit is 30 and your fetch size is 10, the `ResultSet` will contain |
| 127 | 30 rows, but under the hood the driver will perform 3 requests that will transfer 10 |
| 128 | rows each. |
| 129 | |
| 130 | See [Paging](../manual/paging/) for more information. |