2016-03-01 66 views
1

我有以下卡桑德拉表如何爲WHERE條件

cqlsh:mydb> describe table events; 

CREATE TABLE mydb.events (
    id uuid PRIMARY KEY, 
    country text, 
    insert_timestamp timestamp 
) 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'; 
CREATE INDEX country_index ON mydb.events (country); 
CREATE INDEX insert_timestamp_index ON mydb.events (insert_timestamp); 

正如你可以看到Cassandra的timestamp列進行查詢,指數已經在insert_timestamp列上創建。

我已通過https://stackoverflow.com/a/18698386/3238864

走後,我雖然下面是正確的查詢

cqlsh:mydb> select * from events where insert_timestamp >= '2016-03-01 08:27:22+0000'; 
InvalidRequest: code=2200 [Invalid query] message="No secondary indexes on the restricted columns support the provided operators: 'insert_timestamp >= <value>'" 

cqlsh:mydb> select * from events where insert_timestamp >= '2016-03-01 08:27:22+0000' ALLOW FILTERING; 
InvalidRequest: code=2200 [Invalid query] message="No secondary indexes on the restricted columns support the provided operators: 'insert_timestamp >= <value>'" 

但是,隨着countryWHERE條件不工作查詢。

cqlsh:mydb> select * from events where country = 'my'; 

id         | country | insert_timestamp 
--------------------------------------+---------+-------------------------- 
53167d6a-e125-46ff-bacf-f5b267de0258 |  my | 2016-03-01 08:27:22+0000 

任何想法爲什麼查詢與時間戳條件不起作用?我的查詢語法有什麼問題嗎?

+0

[Cassandra CQL範圍查詢可能被重複,儘管等於運算符和二級索引](http://stackoverflow.com/questions/24894393/cassandra-cql-range-query-rejected-despite-equality-operator-and- secondary index) –

回答

1

對二級索引的直接查詢僅支持=,CONTAINS或 CONTAINS KEY限制。

次要索引的查詢可以限制返回的結果 使用=,>,> =,< =和<,CONTAINS和CONTAINS KEY限制 上使用濾波非索引列。

因此,只要您將其添加ALLOW FILTERING即可。

select * from events where insert_timestamp >= '2016-03-01 08:27:22+0000' ALLOW FILTERING; 

您在問題中提到的鏈接具有timestamp列作爲集羣鍵。因此它在那裏工作。

作爲每註釋RangeQuery on secondary index is not alllowed upto 2.2.x version

供參考: 當卡桑德拉必須執行二次索引查詢時,它會接觸的所有節點,以檢查位於每個節點上的二級索引的一部分。 因此,它被認爲是卡桑德拉的反模式,像時間戳那樣擁有高基數列的索引。 您應該考慮更改您的數據模型以適合您的查詢。

+0

我執行'ALLOW FILTERING'版本作爲你的。但我仍然遇到同樣的錯誤。 –

+0

我編輯了我的答案。請看看 –

5

任何想法爲什麼查詢與時間戳條件不起作用?我的查詢語法有什麼問題嗎?

原生Cassandra二級索引被限制爲=謂詞。爲了使不平等謂詞需要添加允許過濾的,但將執行全集羣掃描 :-(

如果你能負擔得起等待幾個星期,卡桑德拉3.4將與新SASI發佈二級索引對於範圍查詢更有效:https://github.com/apache/cassandra/blob/trunk/doc/SASI.md

+0

這個答案非常有用,我的朋友誰不能upvote是要我upvote來表示感謝:) –

0

cassandra中的索引與關係數據庫中的索引完全不同,其中一個區別在於cassandra索引中的範圍查詢是完全不允許的。通常,範圍查詢僅適用於集羣密鑰(如果使用ByteOrderPartitioner,它也可以與分區密鑰一起使用,但並不常見),這意味着您必須仔細設計您的columnfamilies以適合您的潛在查詢模式。已經有many discussions in StackOverflow for the same topic

要了解什麼時候使用Cassandra的指數(它是專爲非常特殊的情況下)和它的侷限性,this是一個不錯的職位,

0

使用cequel ORM

now = DateTime.now 
    today = DateTime.new(now.year, now.month, now.day, 0, 0, 0, now.zone) 
    tommorrow = today + (60 * 60 * 24); 
    MyObject.allow_filtering!.where("done_date" => today..tommorrow).select("*") 

已經爲我工作。