【问题标题】:Cassandra Trigger卡桑德拉触发器
【发布时间】:2017-10-18 01:07:39
【问题描述】:

我是 Cassandra 的新手,使用 Cassandra 3.10 并且有类似的表格

create table db1.table1 (id text, trip_id text, event_time timestamp, mileage double, primary key(id, event_time));
create table db1.table2 (id text, trip_id text, start_time timestamp, mileage double, primary key(id, start_time));

我需要将 table1 中的数据传输到 table2,通过 trip_id 聚合并汇总里程并更新 table2 中的数据

我写了一个触发函数来获取列名和值

public Collection<Mutation> augment(Partition partition) {
    HashMap map = new HashMap();
    CFMetaData cfm = partition.metadata();
    String tableName = cfm.cfName;
    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();              
            while(cells.hasNext()){
                Cell cell = cells.next();
                map.put(cell.column().name.toString(), cell.value().array());
                ...
            }
        }
    } catch (Exception e) {

    }


    ...
}

但是我怎样才能得到主键和主键的值呢?如果这些都无法获取,我该如何使用触发功能来完成这项工作?

【问题讨论】:

    标签: java cassandra nosql


    【解决方案1】:

    是的,可以获取主键和值

    获取分区键列和值使用:

    List<ColumnDefinition> partitionKeyColumns = cfm.partitionKeyColumns();
    ByteBuffer partitionKeyValues = partition.partitionKey().getKey();
    

    获取聚类键列和值:

    List<ColumnDefinition> clusteringKeyColumns = cfm.clusteringColumns();
    ByteBuffer[] clusteringKeyValues = clt.getRawValues();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-19
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      相关资源
      最近更新 更多