复合blob键上的cassandra触发器

时间:2015-10-27 01:30:54

标签: cassandra

我使用Cassandra 2.1.9并拥有类似

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

我按照Cassandra Sample Trigger Code to get inserted value中的帖子操作,然后触发

等代码

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

我尝试插入“Keyspace1”。“Standard4”(id,user_name,data)值(textAsBlob('id1'),textAsBlob('user_name1'),textAsBlob('data1'));

并在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

有人能告诉我如何纠正吗?

1 个答案:

答案 0 :(得分:1)

您是否正在尝试显示所有插入的列名称和值?
这是代码:

@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;
}