2

I am trying to query a table containing Map. Is it possible to apply contains clause on map data type table?

CREATE TABLE data.Table1 (
   fetchDataMap map<text, frozen<Config>>,
   userId text ,
   PRIMARY KEY(userId)
);

Getting following Error:

cqlsh> SELECT * FROM data.Table1 WHERE fetchDataMap CONTAINS '233322554843924';
InvalidRequest: code=2200 [Invalid query] message="No secondary indexes on
    the restricted columns support the provided operators: "

Please enlighten me with better query approach on this requirement.

Raghu
  • 722
  • 3
  • 8
  • 19
  • FYI- Due to several feature improvements in Cassandra, the solution suggested below was not possible at the time which the (two year old) "duplicate" question was answered. – Aaron Apr 06 '15 at 15:42

1 Answers1

2

For this to work, you have to create a secondary index on the map. But, you first have to ask yourself if you want to index your map keys or values (cannot do both). Given your CQL statement, I'll assume that you want to index your map key (and we'll go from there).

CREATE INDEX table1_fetchMapKey ON table1(KEYS(fetchDataMap));

After inserting some data (making a guess as to what your Config UDT looks like), I can SELECT with a slightly modified version of your CQL query above:

aploetz@cqlsh:stackoverflow> SELECT * FROm table1 WHERE
    fetchDataMap CONTAINS KEY '233322554843924';

 userid | fetchdatamap
--------+------------------------------------------------------------
 B26354 | {'233322554843924': {key: 'location', value: '~/scripts'}}

(1 rows)

Note that I cannot in good conscience provide you with this solution, without passing along a link to the DataStax doc When To Use An Index. Secondary indexes are known to not perform well. So I can only imagine that a secondary index on a collection would perform worse, but I suppose that really depends on the relative cardinality. If it were me, I would re-model my table to avoid using a secondary index, if at all possible.

Aaron
  • 55,518
  • 11
  • 116
  • 132