Cassandra and querying hierarchies

2019-07-30 17:23发布

问题:

I am investigating Cassandra, but cannot find an answer in any documentation to the following.

I need to query ranges across a deep hierarchy. I have determined that the easiest way to represent the hierarchy is to have each level as a column. For example:

Origin           Manufacturer    Price    ID
Europe.Germany   VW Group.Audi   20000    1
Europe.Germany   VW Group.Porshe 21000    2
Europe.Germany   BMW             19000    3

Here is a pseudo SQL example:

SELECT ID FROM CompositeTable WHERE (Origin STARTS WITH 'Europe')
AND (Manufacturer STARTS WITH 'VW Group' AND IS NOT 'VW Group.Porshe' OR IS 'BMW')
AND (Price BETWEEN 18000 AND 22000)

Result:

ID = [1, 3]

Can Cassandra perform this type of search across a composite index?

回答1:

Composite Keys in Cassandra is a kind of multiple-column index in DBMS, where if you have index on c1, c2 and c3 in a table with columns from c1 to c6. DB will always try to use you index if the query is for =, > , <, >=, <= operation on (c1), (c1,c2) or (c1, c2, c3) but not for (c2), (c2, c3), (c3) or (c1, c3). The case is the same in cassandra but here you have multiple-column index on c1 to c6 [since the columns are sorted first based on c1 and the collision carries over to c2 and follows on]