Question

I'm have a cassandra table like this:

keyspace_name| columnfamily_name | column_name | component_index |
-------------+-------------------+-------------+-----------------+
          aw |      test         |  as_of_date |               0 |
          aw |      test         |        data |               1 |
          aw |      test         |   record_id |            null |
          aw |      test         | upload_time |               1 |

And I'm won't to create trigger that will print(slf4j for example) rows that whould be inserted in next format:

key = key1
column_name1=value1
column_name2=value2
...
column_namen=valuen

Is it possible to get column name in trigger? I try example from the internet, but it prints incorrect data.

public Collection<RowMutation> augment(ByteBuffer key, ColumnFamily update) {
    String localKey = new String(key.array(), Charset.forName("UTF-8"));
    logger.info("key={}.", localKey);

    for (Column cell : update) {
        try {
            String name = ByteBufferUtil.string(cell.name());
            logger.info("name={}.", name);

            String value = ByteBufferUtil.string(cell.value());
            logger.info("value={}.", value);               
        } catch (Exception e) {
            logger.info("Exception={}.", e.getMessage());
        }
    }

As I understand, i am must convert cell.value() to specific data type like this:

Date date = TimestampType.instance.compose(cell.value());

But I don't know, how to detect field type and i am don't understand why i can't get column name using ByteBufferUtil.string(cell.name()).

Was it helpful?

Solution

To properly format cellname and values you must use the CFMetaData. The correct version of the code should be:

public Collection<Mutation> augment(ByteBuffer key, ColumnFamily update)
{
    CFMetaData cfm = update.metadata();
    String localKey = cfm.getKeyValidator().getString(key);
    logger.info("key={}.", localKey);

    for (Cell cell : update) 
    {
       try 
       {
           String name = cfm.comparator.getString(cell.name());
           logger.info("name={}.", name);

           String value = cfm.getValueValidator(cell.name()).getString(cell.value());
           logger.info("value={}.", value);               
       } catch (Exception e) {
           logger.info("Exception={}.", e.getMessage());
       }
   }
   return Collections.emptyList();

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top