Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra 3.4 Java LIKE operator

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 [?];)
like image 465
JAC2703 Avatar asked Mar 05 '26 20:03

JAC2703


1 Answers

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.

like image 169
Andy Tolbert Avatar answered Mar 07 '26 08:03

Andy Tolbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!