Cassandra IN query not working if table has SET type column

后端 未结 3 1314
别跟我提以往
别跟我提以往 2021-02-06 02:02

I am new to Cassandra. I got a issue in CQL IN query ,if table has SET type column it works.

CREATE TABLE test (
    test_date bigint, 
    test_id          


        
3条回答
  •  独厮守ぢ
    2021-02-06 02:33

    I think you are seeing this error due to Cassandra's underlying storage model. When I query your test1 table within CQLSH (with my own test data), this is what I see:

    aploetz@cqlsh:stackoverflow> SELECT * FROM test1;
    
     test_date | test_id | caption   | tags
    -----------+---------+-----------+-------------------------
       2022015 |       1 | blah blah | {'one', 'three', 'two'}
       2022015 |       2 | blah blah | {'one', 'three', 'two'}
    
    (2 rows)
    

    This view gives a misleading interpretation of how the data is actually stored. This is what it looks like when I query the same table from within cassandra-cli:

    [default@stackoverflow] list test1;
    Using default limit of 100
    Using default cell limit of 100
    -------------------
    RowKey: 2022015
    => (name=1:, value=, timestamp=1422895168730184)
    => (name=1:caption, value=626c616820626c6168, timestamp=1422895168730184)
    => (name=1:tags:6f6e65, value=, timestamp=1422895168730184)
    => (name=1:tags:7468726565, value=, timestamp=1422895168730184)
    => (name=1:tags:74776f, value=, timestamp=1422895168730184)
    => (name=2:, value=, timestamp=1422895161891116)
    => (name=2:caption, value=626c616820626c6168, timestamp=1422895161891116)
    => (name=2:tags:6f6e65, value=, timestamp=1422895161891116)
    => (name=2:tags:7468726565, value=, timestamp=1422895161891116)
    => (name=2:tags:74776f, value=, timestamp=1422895161891116)
    
    1 Row Returned.
    

    This suggests that collection (set) values are stored as additional column keys. A restriction on using the IN relation, is that it must operate on the last key (partitioning or clustering) of a primary key. So I would guess that this is a limitation based on how Cassandra stores the collection data "under the hood."

    And just a warning, but using IN for production-level queries is not recommended. Some have even gone as far as to put it on the list of Cassandra anti-patterns. My answer to this question (Is the IN relation in Cassandra bad for queries?) explains why IN queries are not optimal.

    EDIT

    Just to see, I tried your schema with a list instead of a set to see if that made any difference. It still didn't work, but from within the cassandra-cli it appeared to add an additional UUID identifier to the key, and stored the actual value as the column value. Which is different from how a set was treated...this must be how sets are restricted to unique values.

提交回复
热议问题