Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using triggers in cassandra

I tried using the example https://github.com/apache/cassandra/tree/trunk/examples/triggers

I took help and wrote my own trigger. But i am getting UnsupportedOperationException over UnsortedColumns.getColumn(UnsortedColumns.java:105). This is the only active example on the web. Rest others are deprecated by this example. How do i fix this ?

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.db.RowMutation;
import org.apache.cassandra.triggers.ITrigger;

public class FirstTrigger implements ITrigger {

    @Override
    public Collection<RowMutation> augment(ByteBuffer key, ColumnFamily update) {
        List<RowMutation> mutations = new ArrayList<RowMutation>();
        for (ByteBuffer name : update.getColumnNames()) {
            RowMutation mutation = new RowMutation("mykeyspace", update.getColumn(name).value());
            mutation.add("trigger_log", name, key, System.currentTimeMillis());
            mutations.add(mutation);
        }
        return mutations;
    }

}

The schema to store log events is :

use mykeyspace;   
create table if not exists TRIGGER_LOG (
transaction_id int,
log varchar,
PRIMARY KEY (transaction_id));

My error is

ERROR [Thrift:1] 2013-10-28 03:53:53,120 ProcessFunction.java (line 41) Internal error processing execute_prepared_cql3_query
java.lang.RuntimeException: Exception while creating trigger on CF with ID: 08f73f2f-c4c4-3a23-b2ae-3fdc92559012
    at org.apache.cassandra.triggers.TriggerExecutor.execute(TriggerExecutor.java:131)
    at org.apache.cassandra.triggers.TriggerExecutor.execute(TriggerExecutor.java:73)
    at org.apache.cassandra.service.StorageProxy.mutateWithTriggers(StorageProxy.java:535)
    at org.apache.cassandra.cql3.statements.ModificationStatement.executeWithoutCondition(ModificationStatement.java:360)
    at org.apache.cassandra.cql3.statements.ModificationStatement.execute(ModificationStatement.java:344)
    at org.apache.cassandra.cql3.QueryProcessor.processStatement(QueryProcessor.java:101)
    at org.apache.cassandra.cql3.QueryProcessor.processPrepared(QueryProcessor.java:235)
    at org.apache.cassandra.thrift.CassandraServer.execute_prepared_cql3_query(CassandraServer.java:2045)
    at org.apache.cassandra.thrift.Cassandra$Processor$execute_prepared_cql3_query.getResult(Cassandra.java:4480)
    at org.apache.cassandra.thrift.Cassandra$Processor$execute_prepared_cql3_query.getResult(Cassandra.java:4464)
    at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:39)
    at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:39)
    at org.apache.cassandra.thrift.CustomTThreadPoolServer$WorkerProcess.run(CustomTThreadPoolServer.java:194)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsupportedOperationException
    at org.apache.cassandra.db.UnsortedColumns.getColumn(UnsortedColumns.java:105)
    at com.symantec.paranoid.trigger.FirstTrigger.augment(FirstTrigger.java:18)
    at org.apache.cassandra.triggers.TriggerExecutor.execute(TriggerExecutor.java:123)
    ... 15 more
like image 738
S Kr Avatar asked Oct 28 '13 11:10

S Kr


People also ask

Can Cassandra do joins?

No joins. You cannot perform joins in Cassandra. If you have designed a data model and find that you need something like a join, you'll have to either do the work on the client side, or create a denormalized second table that represents the join results for you.

What are triggers in Cassandra?

The trigger defined on a table fires before a requested DML statement occurs to ensures the atomicity of the transaction. Place the custom trigger code (JAR) in the triggers directory on every node. The custom JAR loads at startup. The location of triggers directory depends on the installation: Cassandra 2.2 and 3.


1 Answers

Instead of iterating over cell names, iterate over the cells themselves.

for (Column cell : update)
{
    // stuff
}

The example has been corrected.

like image 171
iamaleksey Avatar answered Sep 19 '22 23:09

iamaleksey