Prepared Statement with collection in IN clause in Datastax Cassandra CQL driver
Asked Answered
B

3

20

I am trying to run the following query

SELECT edge_id, b_id FROM booking_by_edge WHERE edge_id IN ?

I bind Java list of Long's as a parameter and I get an exception

SyntaxError: line 0:-1 mismatched input '<EOF>' expecting ')' (ResultSetFuture.java:242)

If I try to use (?) it expects single Long item to be bound, but I need a collection

Is there an error in my syntax?

Begrime answered 4/6, 2013 at 13:14 Comment(0)
B
7

Got response on Datastax bugzilla, it is currently not supported, but planned

https://issues.apache.org/jira/browse/CASSANDRA-4210

Update: Supported in Cassandra 2.0.1

Begrime answered 4/6, 2013 at 15:22 Comment(1)
Could you tell me if it is solved now? I could not quite get the idea from the links.Leaseholder
S
23

Tested in Cassandra 2.1.3, the following code snippet works:

PreparedStatement prepared = session.prepare("SELECT edge_id, b_id FROM booking_by_edge WHERE edge_id IN ?;");
List<Long> edgeIds = Arrays.asList(1L, 2L, 3L);
session.execute(prepared.bind(edgeIds));
Superorganic answered 25/3, 2015 at 12:0 Comment(0)
B
7

Got response on Datastax bugzilla, it is currently not supported, but planned

https://issues.apache.org/jira/browse/CASSANDRA-4210

Update: Supported in Cassandra 2.0.1

Begrime answered 4/6, 2013 at 15:22 Comment(1)
Could you tell me if it is solved now? I could not quite get the idea from the links.Leaseholder
W
2

It's a bit hard to find in the documentation but it is described in the tuples section of the manual.

If you want to use named parameters you should use the setList() method.

BoundStatement bs = session.prepare("select col from table where col in :values").bind();
bs.setList("values", Arrays.asList(v1, v2, v3));
Wing answered 17/10, 2017 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.