3
votes

Lets say that I have column family like this:

CREATE TABLE test(
id text,
meta map <text, text>,
PRIMARY KEY (key)
);

Then I put some data to my meta map, for example: {'username' : 'ivan'}

I would like to query my test column family by elements in meta map. Something like this:

SELECT * FROM test WHERE meta['username'] = 'ivan';

According to cassandra documentation this should be possible:

As its name implies, a map maps one thing to another. A map is a name and a pair of typed values. Using the map type, you can store timestamp-related information in user profiles. Each element of the map is internally stored as one Cassandra column that you can modify, replace, delete, and query.

But I cannot find any example of this online so is this really possible?

Thanks, Ivan

2

2 Answers

3
votes

You need to add a secondary index to query a map by key or value:

CREATE INDEX meta_idx ON test (ENTRIES(meta));
SELECT * FROM test WHERE meta['username'] = 'ivan';

See also:

https://docs.datastax.com/en/cql/3.3/cql/cql_using/useIndexColl.html

1
votes

Query meaning that you can get it using select statement not that you can use it in where clause. That's why you can't have map as part of your primary key.