简体   繁体   中英

com.datastax.driver.core.exceptions.InvalidQueryException: Invalid operator IN for PRIMARY KEY part

I have cassandra 2.1.15. I have this table

CREATE TABLE ks_mobapp.messages (
    pair_id text,
    belong_to text,
    message_id timeuuid,
    cli_time bigint,
    sender text,
    text text,
    time bigint,
    PRIMARY KEY ((pair_id, belong_to), message_id)
) WITH CLUSTERING ORDER BY (message_id DESC)

I was trying to delete multiple record as

        instances.getCqlSession().execute(QueryBuilder.delete()
                .from(AppConstants.KEYSPACE, "messages")
                .where(QueryBuilder.eq("pair_id", pairId))
                .and(QueryBuilder.eq("belong_to", currentUser.value("userId")))
                .and(QueryBuilder.in("message_id", msgId)));

I am getting error:

Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Invalid operator IN for PRIMARY KEY part message_id

Then I tried:

        Session session = instances.getCqlSession();
        PreparedStatement statement = session.prepare("DELETE FROM ks_mobApp.messages WHERE pair_id = ? AND belong_to = ? AND message_id = ?;");
        Iterator<String> iterator = msgId.iterator();
        while(iterator.hasNext()) {
            try {
                session.executeAsync(statement.bind(pairId, currentUser.value("userId"), UUID.fromString(iterator.next())));
            } catch(Exception ex) {

            }
        }

Its working nice. Is this the correct way? I can't use IN for same partition key ?

DELETE in Query only supported for partition key.

Delete IN relation is only supported for partition key)

There are some WHERE clause restrictions for the UPDATE and DELETE statements in cassandra 2.x

more specifically you can only use the IN operator on the last partition key column . So in your case the last partition column is belong_to . so IN can only be used on that column.

However these limitation are removed in cassandra 3.0. and it will allow

IN to be specified on any partition key column

IN to be specified on any clustering column

Here is the patch https://issues.apache.org/jira/browse/CASSANDRA-6237

Read this also http://www.datastax.com/dev/blog/a-deep-look-to-the-cql-where-clause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM