Snow Dog
Snow Dog

Reputation: 1

Cassandra Trigger

I am new to Cassandra and use Cassandra 3.10 and have table like

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

I need to transfer data from table1 to table2 aggregated by trip_id and sum on mileage and update data in table2

I have written a trigger function to get column name and value

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) {

    }


    ...
}

But how can I get Primary key and the value of Primary key? If those are not gettable, how can I use trigger function to do the job?

Upvotes: 0

Views: 184

Answers (1)

Ashraful Islam
Ashraful Islam

Reputation: 12830

Yes, It is possible to get primary key and value

To get partition keys column and value use :

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

To get clustering keys column and value :

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

Upvotes: 1

Related Questions