You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Siddharth Verma <ve...@snapdeal.com> on 2016/05/26 06:34:29 UTC

Get clustering column in Custom cassandra trigger

hi,
I am creating a trigger in cassandra
-----------------------------------------------------------------------------------------------------------------------
public class GenericAuditTrigger implements ITrigger
{

    private static SimpleDateFormat dateFormatter = new SimpleDateFormat
("yyyy/MM/dd");

    public Collection<Mutation> augment(Partition update)
    {
        String auditKeyspace = "test";
        String auditTable = "audit";

        RowUpdateBuilder audit = new
RowUpdateBuilder(Schema.instance.getCFMetaData(auditKeyspace, auditTable),
                FBUtilities.timestampMicros(),
                UUIDGen.getTimeUUID())
                .clustering(dateFormatter.format(new
Date()),update.metadata().ksName,update.metadata().cfName,UUID.randomUUID());


audit.add("primary_key",update.metadata().getKeyValidator().getString(update.partitionKey().getKey()));

        UnfilteredRowIterator unfilteredRowIterator =
update.unfilteredIterator();
        StringBuilder next=new StringBuilder();
        while(unfilteredRowIterator.hasNext()){
            next.append(unfilteredRowIterator.next().toString()+"\001");
        }

        audit.add("values",
next.length()==0?null:next.deleteCharAt(next.length()-1).toString()+";"+update.columns().toString());

        return Collections.singletonList(audit.build());
    }
}

-----------------------------------------------------------------------------------------------------------------------
CREATE TABLE test.test (pk1 text, pk2 text, ck1 text, ck2 text, v1 text, v2
text, PRIMARY KEY((pk1,pk2),ck1,ck2);
-----------------------------------------------------------------------------------------------------------------------
CREATE TABLE test.audit (
    timeuuid timeuuid,
    date text,
    keyspace_name text,
    table_name text,
    uuid UUID,
    primary_key text,
    values text,
    PRIMARY KEY (timeuuid, date, keyspace_name, table_name, uuid));
-----------------------------------------------------------------------------------------------------------------------



*How to get clustering column values in trigger?*insert into test(pk1 , pk2
, ck1 , ck2 , v1 , v2 ) VALUES ('pk1','pk2','ck1','ck2_del','v1','v2');

select * from audit;

timeuuid              | 0d117390-227e-11e6-9d80-dd871f2f22d2
date                    | 2016/05/25
keyspace_name  | test
table_name         | test
uuid                    | df274fc0-4362-42b1-a3bf-0030f8d2062f
primary_key        | pk1:pk2
values                | [[v1=v1 ts=1464184100769315], [v2=v2
ts=1464184100769315]]


How to audit ck1 and ck2 also?

Thanks,
Siddharth Verma

Re: Get clustering column in Custom cassandra trigger

Posted by Tyler Hobbs <ty...@datastax.com>.
Try:

unfilteredRowIterator.next().clustering().toString(update.metadata())

To get the raw values, you can use:

unfilteredRowIterator.next().clustering().getRawValues()

On Thu, May 26, 2016 at 7:25 AM, Siddharth Verma <
verma.siddharth@snapdeal.com> wrote:

> Hi Sam,
> Sorry, I couldn't understand.
>
> I am already using
> UnfilteredRowIterator unfilteredRowIterator
> =partition.unfilteredIterator();
>
> while(unfilteredRowIterator.hasNext()){
>     next.append(unfilteredRowIterator.next().toString()+"\001");
> }
>
> Is there another way to access it?
>
>


-- 
Tyler Hobbs
DataStax <http://datastax.com/>

Re: Get clustering column in Custom cassandra trigger

Posted by Siddharth Verma <ve...@snapdeal.com>.
Hi Sam,
Sorry, I couldn't understand.

I am already using
UnfilteredRowIterator unfilteredRowIterator =partition.unfilteredIterator();

while(unfilteredRowIterator.hasNext()){
    next.append(unfilteredRowIterator.next().toString()+"\001");
}

Is there another way to access it?

Re: Get clustering column in Custom cassandra trigger

Posted by Sam Tunnicliffe <sa...@beobal.com>.
If you just want the string representations you can just use
Unfiltered::clustering to get the Clustering instance for each Unfiltered,
then call its toString(CFMetadata), passing update.metadata().



On Thu, May 26, 2016 at 12:01 PM, Siddharth Verma <
verma.siddharth@snapdeal.com> wrote:

> Tried the following as well. Still no result.
>
> update.metadata().clusteringColumns().toString()  -> get clustering column
> names
> update.columns().toString()                       -> gets no primary key
> colulmns
> update.partitionKey().toString()                  -> gets token range
>
> Any help would be appreciated.
>
> Thanks
> Siddharth Verma
>

Re: Get clustering column in Custom cassandra trigger

Posted by Siddharth Verma <ve...@snapdeal.com>.
Tried the following as well. Still no result.

update.metadata().clusteringColumns().toString()  -> get clustering column
names
update.columns().toString()                       -> gets no primary key
colulmns
update.partitionKey().toString()                  -> gets token range

Any help would be appreciated.

Thanks
Siddharth Verma