3
votes

I need your help on extract column names and values in the trigger augment method.

Table Def :

           create table dy_data (
                                     id timeuuid,
                                     data_key text,
                                     time timestamp,
                                    data text,primary key((id,data_key),time)
                                    ) with clustering order by (time desc);

Trigger Code :

public class ArchiveTrigger implements ITrigger {
  public Collection<RowMutation> augment(ByteBuffer key, ColumnFamily cf) {
        try {
            // Below loop only has 2 columns ( one is "data" and another one may be "time" but i am not sure, because i cannot get value.           
            for (Column cell : cf) {
                //Got Exception if I try to get column name
                String name = ByteBufferUtil.string(cell.name());
                //Got only "data" column value and empty value for another column may be "time". If I try ByteBufferUtil.toLong(cell.value()) it throws exception
                String value = ByteBufferUtil.string(cell.value());
                log(" name = " + name);
                log(" value = " + value);
            }
        } catch (Exception e) {
            logger.warn("Exception ", e);
        }
        return null;
    }
}

I tried my best to search sample code in google. But failed. Please help me with sample code. Thanks in advance.

2
What's the insert that triggers it? (I assume you are only inserting data_key/data and time). - Alex Popescu
No. I inserted all the data [ insert into dy_data(id,data_key,time,data) values( now(),'TX_BYTES:H',1385447412778,'data') ; ] - rameshj

2 Answers

3
votes

To get 'id' and 'data_key' you'll have to split the key (ByteBuffer key, the first argument to augment). 'time' will be in the first part of cell.name() - you'd still need to split it. 'data' will be the cell.value(), no need to do any splitting.

4
votes

Thanks iamaleksey for you help. Your replies helps me a lot.

Below are the code snippet which will be useful for trigger users,

public Collection<RowMutation> augment(ByteBuffer key, ColumnFamily cf) {
        try {
            ByteBuffer id_bb = CompositeType.extractComponent(key, 0);
            UUID id=TimeUUIDType.instance.compose(id_bb);

            ByteBuffer data_key_bb = CompositeType.extractComponent(key, 1);
            String data_key=UTF8Type.instance.compose(data_key_bb);


            Iterator col_itr=cf.iterator();

            Column ts_col=(Column)col_itr.next();
            ByteBuffer time_bb=CompositeType.extractComponent(ts_col.name(),0);
            long time=(TimestampType.instance.compose(time_bb)).getTime();


            Column data_bb=(Column)col_itr.next();
            String data=UTF8Type.instance.compose(data_bb.value());

            log(" id --> "+id.toString());
            log(" data_key-->"+data_key);
            log(" time == "+time);
            log(" data == "+data);
        } catch (Exception e) {
            logger.warn("Exception ", e);
        }
        return null;
    }

PS: Since I know my table format, I hardcoded the column comparator type. If we want to write generic trigger code, we can use cf.getComponentComparator()/getKeyValidator().