Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use cql to query collections in a row?

Tags:

cassandra

cql

Assuming you have movies as a row, and a list of categories, i.e.

create table movie(
    uuid timeuuid,
    name text,
    category list<text>
    primary key (uuid)
);

insert into movie (uuid,name,category) values(now(), 'my movie', ['romance','comedy']);
insert into movie (uuid,name,category) values(now(), 'other movie', ['action','comedy']);

Is it possible to efficiently do something like:

select * from movie where category='comedy'
select * from movie where category='comedy' and category='action'

This use case is the most common query against our MySQL database.

like image 395
我不是一個鳳梨 Avatar asked Feb 19 '14 07:02

我不是一個鳳梨


People also ask

How are the Cassandra CQL collections used?

CQL provides the facility of using Collection data types. Using these Collection types, you can store multiple values in a single variable.

What is a difference between CQL and SQL?

Query languages are computer languages used to make queries in databases and information systems. Cassandra Query Language is the primary query language for communicating with the Apache Cassandra database. SQL (Structured Query Language) is the standard query language for dealing with Relational Databases.

Does Cassandra support nested queries?

Cassandra cannot do joins or subqueries. Rather, Casssandra emphasizes denormalization through features like collections. A column family (called "table" since CQL 3) resembles a table in a RDBMS (Relational Database Management system).

How do you ORDER BY CQL?

You can fine-tune the display order using the ORDER BY clause. The partition key must be defined in the WHERE clause and the ORDER BY clause defines the clustering column to use for ordering. cqlsh> CREATE TABLE cycling.


1 Answers

As of cassandra 2.1, yes it is:

create table cinema.movie(
    id timeuuid,
    name text,
    category list<text>, 
    primary key (id)
);

CREATE index ON cinema.movie (category);
INSERT INTO cinema.movie (id, name, category) VALUES (now(), 'my movie', ['romance','comedy']);
SELECT * FROM cinema.movie WHERE category CONTAINS 'romance';


 uuid                                 | category               | name
--------------------------------------+------------------------+----------
 b9e0d7f0-995f-11e3-b11c-c5d4ddc8930a | ['romance', 'comed`y'] | my movie

(1 rows)

P.S. you have errors in your queries, you are missing a , in your create table statement after category's declaration and you cant use the now() function on UUID, you are looking for the TIMEUUID type instead.

Bare in mind that performance wont be magnificent (how's that for quantitative) when it come's to using these collections, it might be better to create a separate table for this sort of thing.

like image 131
Lyuben Todorov Avatar answered Oct 19 '22 07:10

Lyuben Todorov