简体   繁体   English

Cassandra 3.x触发列值

[英]Cassandra 3.x trigger column values

I'm using the 3.4 Cassandra trigger API, that introduced the modified ITrigger interface, example: https://github.com/apache/cassandra/blob/trunk/examples/triggers/src/org/apache/cassandra/triggers/AuditTrigger.java 我正在使用3.4 Cassandra触发器API,该API引入了修改后的ITrigger接口,例如: https : //github.com/apache/cassandra/blob/trunk/examples/triggers/src/org/apache/cassandra/triggers/AuditTrigger的.java

My question is what is the way to extract the column values from Partition object for insert/update statements? 我的问题是从分区对象中为插入/更新语句提取列值的方法是什么? If so how can I do this? 如果可以,我该怎么做?

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

Some code snippet would be useful. 一些代码片段会很有用。

Try this !!! 尝试这个 !!!

public Collection<Mutation> augment(Partition update) {
    try {
        UnfilteredRowIterator it = update.unfilteredIterator();
        while (it.hasNext()) {
            Unfiltered un = it.next();
            Clustering clt = (Clustering) un.clustering();  
            Iterator<Cell> cls = update.getRow(clt).cells().iterator();
            while(cls.hasNext()){
                Cell cell = cls.next();
                 String data = new String(cell.value().array()); // If cell type is text
            }
        }
    } catch (Exception e) {
      ...
    }
    return null;
}

I use this code for static columns 我将此代码用于静态列

@Override
public Collection<Mutation> augment(Partition update) {
    String keyspaceName = update.metadata().ksName;
    //for static columns
    Row dataRow = update.getRow(Clustering.STATIC_CLUSTERING);
    for (Cell cell : dataRow.cells()) {
        ColumnDefinition cDefinition = cell.column();
        String colName = cDefinition.name.toString();
        ByteBuffer value = cell.value();
    }
    return Collections.EMPTY_LIST;
}

The colName and value variables are data of cell. colName和value变量是单元格的数据。

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();
    }        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM