使用Cassandra触发器基于更新删除行

时间:2019-08-12 13:16:01

标签: cassandra-3.0

其中一列更新为某个值,然后我需要使用Cassandra触发器删除整行。

例如列Severity = 5被更新为Severity =0。然后,我能够获取行和行也正在更新的ID。现在我需要删除该行

package org.apache.cassandra.triggers;

import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.sql.RowSetEvent;

import org.apache.cassandra.config.CFMetaData;
import org.apache.cassandra.config.ColumnDefinition;
import org.apache.cassandra.config.Schema;
import org.apache.cassandra.db.Clustering;
import org.apache.cassandra.db.DecoratedKey;
import org.apache.cassandra.db.DeletionTime;
import org.apache.cassandra.db.Mutation;
import org.apache.cassandra.db.partitions.Partition;
import org.apache.cassandra.db.partitions.PartitionUpdate;
import org.apache.cassandra.db.rows.Cell;


import org.apache.cassandra.db.rows.Unfiltered;
import org.apache.cassandra.db.rows.UnfilteredRowIterator;
import org.apache.cassandra.io.util.FileUtils;
import org.apache.cassandra.triggers.ITrigger;
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.UUIDGen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class App implements ITrigger
{
    private Properties properties = loadProperties();
    private static final Logger logger = LoggerFactory.getLogger(App.class);

    public Collection<Mutation> augment(Partition update)
    {
        String auditKeyspace = properties.getProperty("keyspace");
        String auditTable = properties.getProperty("table");
        ArrayList<String> ids = new ArrayList<String>();

        CFMetaData metadata = Schema.instance.getCFMetaData(auditKeyspace, auditTable);

        String tableName = update.metadata().cfName;
        logger.info("Table: " + tableName);


        logger.info("info::"+update.toString());
        logger.info("msggg::"+update.columns());

        UnfilteredRowIterator it = update.unfilteredIterator();

        try {            
            while (it.hasNext()) {
                Unfiltered un = it.next();
                Clustering clt = (Clustering) un.clustering();  
                Iterator<Cell> cls = update.getRow(clt).cells().iterator();
                Iterator<ColumnDefinition> columns = update.getRow(clt).columns().iterator();
                //logger.info("column details::"+columns.toString());
                logger.info("-----------------CELLS-----------------");

                while(columns.hasNext()){
                    ColumnDefinition columnDef = columns.next();
                    Cell cell = cls.next();
           //           logger.info("inside while2222: cell="+cell);
                    String colName = columnDef.name.toString();
                    logger.info("---------Now printing colnames:colName="+colName);

                    if(colName.contains("status")) {
                        //int status = new Integer(cell.value().getInt());  //If cell type is int
                        int status = new Integer(cell.value().getInt(0)); 
                        logger.info("inside if --------- status =");
                        if(status== 15) {
                            logger.info("True");
                        }

                       }
                    else if(colName.contains("rule")) {
                        String data = new String(cell.value().array()); // If cell type is text              
                        logger.info("--------- data ="+data);

                       }


                    }


                    //message.put(columnDef.toString(), data);


            }
        } catch (Exception e) {
          logger.info("Error["+e+"]");
        }


        return Collections.emptyList();
        //return Collections.singletonList(audit.buildAsMutation());
    }

    private static Properties loadProperties()
    {
        Properties properties = new Properties();
        InputStream stream = App.class.getClassLoader().getResourceAsStream("App.properties");
        try
        {
            properties.load(stream);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        finally
        {
            FileUtils.closeQuietly(stream);
        }
        return properties;
    }
}

0 个答案:

没有答案