Cassandra / Hector:在复合主键上添加计数器

时间:2012-10-17 18:17:16

标签: cassandra hector cql

我在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; 

这可以按预期工作。我想在Hector中进行相同的更新(在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.)

直接从hector运行查询:

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)

我似乎没有在复合主键下使用计数器的任何其他示例。它甚至可能吗?

2 个答案:

答案 0 :(得分:0)

绝对可以直接使用cql(至少通过CQLSH和C ++):

  

cqlsh:goh_master>描述表daily_caps;
   CREATE TABLE daily_caps    (caps_type ascii,id ascii,value counter,PRIMARY KEY    (caps_type,id))具有紧凑的存储和评论=''和    caching ='KEYS_ONLY'和read_repair_chance = 0.100000 AND
   gc_grace_seconds = 864000 AND replicate_on_write ='true'和
   compaction_strategy_class ='SizeTieredCompactionStrategy'和
   compression_parameters:sstable_compression = 'SnappyCompressor';

     

cqlsh:goh_master>更新daily_caps set value = value +1其中caps_type ='xp'和id ='myid';

     

cqlsh:goh_master> select * from daily_caps;

     

caps_type | id |值

     

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

     

xp | myid | 1

答案 1 :(得分:0)

CQL3和thrift API不兼容。因此,使用CQL3创建列系列并使用Hector或其他基于thrift的客户端访问它将无法正常工作。有关更多信息,请参阅:

https://issues.apache.org/jira/browse/CASSANDRA-4377