找不到所请求操作的编解码器:[timestamp< - > java.util.UUID中]

时间:2016-11-23 06:58:16

标签: java cassandra datastax-java-driver

我在Cassandra有一张桌子,我试图迭代并从中提取东西。我正在使用datastax java driver 3.1.0。

CREATE TABLE test (
  client_id int,
  process_id text,
  last_modified_date timestamp,
  PRIMARY KEY ((client_id), process_id)
)

以下是代码:

private List<MetaHolder> getMetaHolder() {
    List<MetaHolder> metaHolder = new ArrayList<>();
    String sql = "select * from test where client_id=1";
    try {
      BoundStatement bs = Cache.getInstance().getStatement(sql);
      bs.setConsistencyLevel(ConsistencyLevel.QUORUM);

      ResultSet res = session.execute(bs);
      Iterator<Row> rows = res.iterator();
      while (rows.hasNext()) {
        Row row = rows.next();
        String processId = row.getString("PROCESS_ID");
        // this line throws exception
        UUID lastModifiedDate = row.getUUID("LAST_MODIFIED_DATE");
        MetaHolder metadata =
            new MetaHolder(processId, lastModifiedDate);
        metaHolder.add(metadata);
      }
    } catch (Exception ex) {
      LOGGER.logError("error= ", ExceptionUtils.getStackTrace(ex));
    }
    return metaHolder;
}

以下是例外情况:

error= com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [timestamp <-> java.util.UUID]

有没有办法可以通过创建编解码器来使用JodaTime而不是在我的代码中使用UUID?

1 个答案:

答案 0 :(得分:1)

更改以下代码

UUID lastModifiedDate = row.getUUID("LAST_MODIFIED_DATE");

Date lastModifiedDate = row.getTimestamp("LAST_MODIFIED_DATE");

你可以从日期获得joda DateTime

DateTime lastModifiedDateTime = new DateTime(lastModifiedDate);