2017-05-12 1 views
0

J'utilise Cassandra 3.10 et j'ai besoin de calculer combien de modifications de données j'ai. Pour résoudre le problème, je vais utiliser la gâchette cassandra, mais le résultat des déclencheurs travail doivent être MutationComment faire pour convertir CounterMutation en Cassandra ITrigger

public interface ITrigger { 
    public Collection<Mutation> augment(Partition update); 
} 

Mais je CounterMutation,

@Override 
public Collection<Mutation> augment(Partition update) { 
    String keyspaceName = update.metadata().ksName; 
    //CFMetaData metadata = Schema.instance.getCFMetaData(keyspaceName, cfName); 

    long timestamp = System.currentTimeMillis(); 
    String cfName = "user_product_count_cf"; 

    CFMetaData counterCfMetadata = Schema.instance.getCFMetaData(keyspaceName, cfName); 
    ByteBuffer key = toByteBuffer("test-user-key");  
    PartitionUpdate.SimpleBuilder builder = PartitionUpdate.simpleBuilder(counterCfMetadata, key); 

    ByteBuffer columnName = toByteBuffer("test-counter-column"); 
    ByteBuffer countValue = CounterContext.instance().createLocal(1); 
    builder.timestamp(timestamp).row(Clustering.make(columnName)).add("value", value); 

    Mutation mutation = builder.buildAsMutation(); 
    //TODO this line does not work 
    //return Collections.singletonList(mutation); 

    CounterMutation cMutation = new CounterMutation(mutation, ConsistencyLevel.ONE); 
    //I do not understand what I have to do next. 

    //Now I use next line and it is work, but I don't sure that it is good practice. 
    return Collections.singletonList(cMutation.applyCounterMutation()); 
} 

alors comment convertir CounterMutation à Mutation?

Répondre

0

Vous pouvez utiliser QueryProcessor classe

Exemple:

@Override 
public Collection<Mutation> augment(Partition update) { 
    String keyspaceName = update.metadata().ksName; 
    String cfName = "user_product_count_cf"; 

    QueryProcessor.process(
      QueryBuilder.update(keyspaceName,cfName).with(incr("test-counter-column",1)).where(eq("test-user-key", bindMarker())).toString(), 
      ConsistencyLevel.LOCAL_QUORUM, 
      Arrays.asList(key) // Put the partition key value here 
    ); 

    return Collections.EMPTY_LIST; 
} 
+0

I insérer la date à user_event_cf, et que vous souhaitez créer ou nombre user_product_count_cf. Je pensais que je devais créer une mutation. L'utilisation de cql dans le trigger est-elle une bonne pratique? –

+0

Vérifiez le journal système Cassandra lance l'exception –

+0

J'ai ajouté quelques commentaires au code, ce code fonctionne bien. –