2012-10-17 54 views
0

我在CQL3控制檯中創建一個表(沒有單一的主鍵成分是獨一無二的,他們一起將):卡桑德拉/赫:添加計數器上的複合主鍵

CREATE TABLE aggregate_logs (
    bpid varchar, 
    jid int, 
    month int, 
    year int, 
    value counter, 
PRIMARY KEY (bpid, jid, month, year)); 

這才得以更新和查詢使用:

UPDATE aggregate_logs SET value = value + 1 WHERE bpid='1' and jid=1 and month=1 and year=2000; 

這符合預期。我想要做的赫克託相同的更新(Scala中):

val aggregateMutator:Mutator[Composite] = HFactory.createMutator(keyspace, compositeSerializer) 

    val compKey = new Composite() 
    compKey.addComponent(bpid, stringSerializer) 
    compKey.addComponent(new Integer(jid), intSerializer) 
    compKey.addComponent(new Integer(month), intSerializer) 
    compKey.addComponent(new Integer(year), intSerializer) 

    aggregateMutator.incrementCounter(compKey, LogsAggregateFamily, "value", 1) 

,但我得到一個錯誤與消息:

...HInvalidRequestException: InvalidRequestException(why:String didn't validate.) 

運行查詢直接從赫克託有:

val query = new me.prettyprint.cassandra.model.CqlQuery(keyspace, compositeSerializer, stringSerializer, new IntegerSerializer()) 
query.setQuery("UPDATE aggregate_logs SET value = value + 1 WHERE 'bpid'=1 and jid=1 and month=1 and year=2000") 
query.execute() 

它給我的錯誤:

InvalidRequestException(why:line 1:59 mismatched input 'and' expecting EOF) 

我似乎還沒有其他例子在複合主鍵下使用計數器。它甚至有可能嗎?

回答

0

它使用是絕對有可能直接CQL(兩者通過CQLSH和C++,至少):

cqlsh:goh_master> describe table daily_caps;
CREATE TABLE daily_caps ( caps_type ascii, id ascii, value counter, PRIMARY KEY (caps_type, id)) WITH COMPACT STORAGE AND comment='' AND
caching='KEYS_ONLY' AND read_repair_chance=0.100000 AND
gc_grace_seconds=864000 AND replicate_on_write='true' AND
compaction_strategy_class='SizeTieredCompactionStrategy' AND
compression_parameters:sstable_compression='SnappyCompressor';

cqlsh:goh_master> update daily_caps set value=value +1 where caps_type='xp' and id ='myid';

cqlsh:goh_master> select * from daily_caps;

caps_type | id | value

-----------+------+-------

xp | myid | 1