Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a subselect in cassandra db

Tags:

cassandra

I'm trying to recover the last state of my object, but this sub select does not work, I've seen that the new version of cassandra db supports aggregate operations and subqueries.

select * from event_store 
   where event_version = (select max(event_version) from event_store) 
    ALLOW FILTERING;

SyntaxException: line 1:49 no viable alternative at input 'select' (...from event_store where event_version = [(]select...)

like image 235
Tiago Costa Avatar asked Apr 11 '17 12:04

Tiago Costa


People also ask

Can we perform subquery in Cassandra?

The main difference from SQL is that Cassandra does not support joins or subqueries.

How do I limit the number of rows in Cassandra?

The LIMIT option sets the maximum number of rows that the query returns: SELECT lastname FROM cycling. cyclist_name LIMIT 50000; Even if the query matches 105,291 rows, Cassandra only returns the first 50,000.

How do you do a group by in Cassandra?

The GROUP BY option can condense all selected rows that share the same values for a set of columns into a single row. Using the GROUP BY option, rows can be grouped at the partition key or clustering column level. Consequently, the GROUP BY option only accepts primary key columns in defined order as arguments.

Can we do joins in Cassandra?

No joins. You cannot perform joins in Cassandra. If you have designed a data model and find that you need something like a join, you'll have to either do the work on the client side, or create a denormalized second table that represents the join results for you.


1 Answers

You can do this by using 2 separate queries:

select max(event_version) from event_store;

and then

select * from event_store where event_version = 2 allow filtering;

I might be a bit outdated but it looks like there is no sub select support:

Work on this ticket is stopped

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

There were some attempts to do this:

https://github.com/jobmthomas/Cassandra-SubQuery

But overall this is just not supported in cassandra.

like image 113
Marko Švaljek Avatar answered Nov 16 '22 02:11

Marko Švaljek