Example of creating triggers in Cassandra and does this support only for Java?

后端 未结 1 515
醉酒成梦
醉酒成梦 2021-01-07 01:38

Wanted to check about Triggers feature in Cassandra. Can someone please provide an example for creating Trigger.

From this blog, http://www.datastax.com/dev/blog/wha

相关标签:
1条回答
  • 2021-01-07 01:59

    Cassandra 3.0

    You can use this and it will get you everything in the insert as a json

    public class HelloWorld implements ITrigger
    {
        private static final Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    
        public Collection<Mutation> augment(Partition partition)
        {
            String tableName = partition.metadata().cfName;
            logger.info("Table: " + tableName);
    
            JSONObject obj = new JSONObject();
            obj.put("message_id", partition.metadata().getKeyValidator().getString(partition.partitionKey().getKey()));
    
            try {
                UnfilteredRowIterator it = partition.unfilteredIterator();
                while (it.hasNext()) {
                    Unfiltered un = it.next();
                    Clustering clt = (Clustering) un.clustering();  
                    Iterator<Cell> cells = partition.getRow(clt).cells().iterator();
                    Iterator<ColumnDefinition> columns = partition.getRow(clt).columns().iterator();
    
                    while(columns.hasNext()){
                        ColumnDefinition columnDef = columns.next();
                        Cell cell = cells.next();
                        String data = new String(cell.value().array()); // If cell type is text
                        obj.put(columnDef.toString(), data);
                    }
                }
            } catch (Exception e) {
    
            }
            logger.debug(obj.toString());
    
            return Collections.emptyList();
        }
    }
    
    0 讨论(0)
提交回复
热议问题