Cassandra 3.4 now offers the LIKE operator by way of a custom index.
It works fine in my test example using cqlsh with something like SELECT * FROM table WHERE indexed_column LIKE '%VALU%'. I've set up an index and it all functions perfectly.
How do I do this in the Java Driver (I'm using version 3.0.2) with dynamic values? My query works using the QueryBuilder and using PreparedStatements when using a literal value like so (PS example but QueryBuilder works with literal values also):
PreparedStatement ps = PreparedStatement("SELECT * FROM table WHERE indexed_column LIKE '%VALU%'");
But not if I want to use ? to bind a value (e.g. bind '%VAL%') in either the QueryBuilder or using PreparedStatements. E.g.
PreparedStatement ps = PreparedStatement("SELECT * FROM table WHERE indexed_column LIKE ?");
BoundStatement boundStatement = new BoundStatement(ps);
cassandraSession.execute(boundStatement.bind('%VAL%');
I get the following error:
com.datastax.driver.core.exceptions.SyntaxError: line 1:53 mismatched input '?' expecting STRING_LITERAL (...test.table WHERE indexed_column LIKE [?];)
This is a bug in 3.4 that was fixed in C* 3.6 via CASSANDRA-11456 in that you couldn't provide a bind marker (?) for a value on the LIKE operator. If you upgrade to Cassandra 3.6 this should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With