1
votes

I use Cassandra 2.1.9 and have table like

create table "Keyspace1"."Standard4" ( id blob, user_name blob, data blob, primary key(id, user_name));

and I follow the post in Cassandra Sample Trigger Code to get inserted value and do trigger code like


    public class InvertedIndex implements ITrigger
    {
       private static final Logger logger = LoggerFactory.getLogger(InvertedIndex.class);

       public Collection augment(ByteBuffer key, ColumnFamily update)
       {
         CFMetaData cfm = update.metadata();

         ByteBuffer id_bb = key;
         String id_Value = new String(id_bb.array());

         Iterator col_itr=update.iterator();
         Cell username_col=(Cell)col_itr.next();
         ByteBuffer username_bb=CompositeType.extractComponent(username_col.name().collectionElement(),0);
         String username_Value = new String(username_bb.array());

         Cell data_col=(Cell)col_itr.next();
         ByteBuffer data_bb=BytesType.instance.compose(data_col.value());
         String data_Value = new String(data_bb.array());

         logger.info(" id --> "+id_Value);
         logger.info(" username-->"+username_Value);
         logger.info(" data ---> "+data_Value);

         return null;
       }
    }

I tried insert into "Keyspace1"."Standard4" (id, user_name, data) values (textAsBlob('id1'), textAsBlob('user_name1'), textAsBlob('data1'));

and got run time exception in ByteBuffer username_bb=CompositeType.extractComponent(username_col.name().collectionElement(),0);


    Caused by: java.lang.NullPointerException: null
            at org.apache.cassandra.db.marshal.CompositeType.extractComponent(CompositeType.java:191) ~[apache-cassandra-2.1.9.jar:2.1.9]
            at org.apache.cassandra.triggers.InvertedIndex.augment(InvertedIndex.java:52) ~[na:na]
            at org.apache.cassandra.triggers.TriggerExecutor.executeInternal(TriggerExecutor.java:223) ~[apache-cassandra-2.1.9.jar:2.1.9]
            ... 17 common frames omitted

Can anybody tell me how to correct?

1

1 Answers

1
votes

You are trying to show all the inserted column name and value right ?
Here is the code:

@Override
public Collection<Mutation> augment(ByteBuffer key, ColumnFamily update) {
    CFMetaData cfm = update.metadata();
    System.out.println("key => " + ByteBufferUtil.toInt(key));
    for (Cell cell : update) {
        if (cell.value().remaining() > 0) {
            try {
                String name = cfm.comparator.getString(cell.name());
                String value = cfm.getValueValidator(cell.name()).getString(cell.value());
                System.out.println("Column Name => " + name + " Value => " + value);
            } catch (Exception e) {
                System.out.println("Exception : " + e.getMessage());
            }
        }
    }
    return null;
}