有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用cassandra触发器的java

我有一张像这样的卡桑德拉桌子:

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 |

我不会创建触发器来打印(例如slf4j)将以下一种格式插入的行:

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

是否可以在触发器中获取列名? 我尝试从互联网上获取示例,但它打印的数据不正确

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

据我所知,我必须转换成细胞。value()到如下特定数据类型:

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

但我不知道如何检测字段类型,我不明白为什么不能使用ByteBufferUtil获取列名。字符串(cell.name())


共 (1) 个答案

  1. # 1 楼答案

    要正确设置cellname和值的格式,必须使用CFMetaData。代码的正确版本应为:

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

    }