You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by J Ramesh Kumar <ra...@gmail.com> on 2013/12/02 11:10:43 UTC

Re: Sample Trigger Code to get inserted value

Finally I got it working...

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().

- Ramesh


On Wed, Nov 27, 2013 at 5:10 PM, J Ramesh Kumar <ra...@gmail.com>wrote:

> Hi,
>
> 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);
>
> 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.
>
> Regards,
> Ramesh
>