2012-10-17 56 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