Cassandra Predicates on non-primary-key columns (eventtype) are not yet supported for non secondary index queries

你说的曾经没有我的故事 提交于 2021-02-10 16:48:29

问题


i developed a table as shown as below with primary key as id which is a uuid type

 id                                   | date                     | eventtype    | log      | password | priority | sessionid | sourceip     | user       | useragent
--------------------------------------+--------------------------+--------------+----------+----------+----------+-----------+--------------+------------+------------
 6b47e9b0-d11a-11e8-883c-5153f134200b |                     null | LoginSuccess |  demolog |     1234 |       10 |    Demo_1 | 123.12.11.11 |       Aqib |  demoagent
 819a58d0-cd3f-11e8-883c-5153f134200b |                     null | LoginSuccess |  demolog |     1234 |       10 |    Demo_1 | 123.12.11.11 |       Aqib |  demoagent
 f4fae220-d133-11e8-883c-5153f134200b | 2018-10-01 04:01:00+0000 | LoginSuccess |  demolog |     1234 |       10 |    Demo_1 | 123.12.11.11 |       Aqib |  demoagent

But when i try to query some thing like below

select * from loginevents where eventtype='LoginSuccess';

i get an error like below

InvalidRequest: Error from server: code=2200 [Invalid query] message="Predicates on non-primary-key columns (eventtype) are not yet supported for non secondary index queries"

This is my table

cqlsh:events> describe loginevents;

CREATE TABLE events.loginevents (
    id uuid PRIMARY KEY,
    date timestamp,
    eventtype text,
    log text,
    password text,
    priority int,
    sessionid text,
    sourceip text,
    user text,
    useragent text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
    AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';

How can i solve this


回答1:


An immediate answer to your question would be to create a secondary index on the column eventtype like this:

CREATE INDEX my_index ON events.loginevents (eventtype);

Then you can filter on this particular column :

SELECT * FROM loginevents WHERE eventtype='LoginSuccess';

However this solution can badly impact the performances of your cluster.

If you come from the SQL world and are new to Cassandra, go read an introduction on cassandra modeling, like this one.

The first thing is to identify the query, then create the table according to.

In Cassandra, data are distributed in the cluster according to the partition key, so reading records that belong to the same partition is very fast.

In your case, maybe a good start would be to group your records based on the eventtype :

CREATE TABLE events.loginevents (
  id uuid,
  date timestamp,
  eventtype text,
  log text,
  password text,
  priority int,
  sessionid text,
  sourceip text,
  user text,
  useragent text,
  PRIMARY KEY (eventtype, id)

)

Then you can do select like this :

SELECT * FROM loginevents WHERE eventtype='LoginSuccess';

or even :

SELECT * FROM loginevents WHERE eventtype in ('LoginSuccess', 'LoginFailure');

(It's not a perfect model, it definitely needs to be improved before production.)




回答2:


In Cassandra, you can only query on the PRIMARY key and some of the clustering columns and it's not possible to query on all of the fields. if you want to query on "eventtype" you should use secondary indexes in the definition of table or index table by Apache Solr and query using Solr.Some things like below:

CREATE INDEX loginevents_type
   ON events.loginevents (eventtype);


来源:https://stackoverflow.com/questions/52868299/cassandra-predicates-on-non-primary-key-columns-eventtype-are-not-yet-supporte

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!