时间戳或日期列查询问题cassandra 4 (astradb)

Timestamp or date column query issue cassandra 4 (astradb)

我在从 springboot 应用程序查询 cassandra (astradb) 时遇到问题。

以下是我使用的详细信息:

'org.springframework.boot' version '2.6.8'

implementation 'org.springframework.boot:spring-boot-starter-data-cassandra'



//cassandra specific driver
    implementation 'com.datastax.astra:astra-spring-boot-starter:0.3.0'
    implementation 'com.datastax.oss:java-driver-core:4.14.1'
    implementation 'com.datastax.oss:java-driver-query-builder:4.14.1'
    implementation 'com.datastax.oss:java-driver-mapper-runtime:4.14.1'

为了检索数据,我正在使用 Cassandra Java jpa。

这是我的存储库:

  @Query(value = "select * from engine_torque_by_last_date " +
            "where vin_number = :vinNumber " +
            "and organization_id in :organizationId and " +
            "stats_date = totimestamp(:dateString)")
    List<EngineTorqueByLastDate> findByVinNumberAndOrganizationIdAndStatsDate(String vinNumber, Integer organizationId, String dateString);

现在在 Pojo 中,我将属性定义为时间戳。

错误如下:

{
  "title": "Internal Server Error",
  "status": 500,
  "detail": "Query; CQL [select * from engine_torque_by_last_date where vin_number = ? and organization_id in ? and stats_date = totimestamp(?)]; Ambiguous call to function totimestamp (can be matched by following signatures: system.totimestamp : (timeuuid) -> timestamp, system.totimestamp : (date) -> timestamp): use type casts to disambiguate; nested exception is com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Ambiguous call to function totimestamp (can be matched by following signatures: system.totimestamp : (timeuuid) -> timestamp, system.totimestamp : (date) -> timestamp): use type casts to disambiguate",
  "cause": {
    "title": "Internal Server Error",
    "status": 500,
    "detail": "Ambiguous call to function totimestamp (can be matched by following signatures: system.totimestamp : (timeuuid) -> timestamp, system.totimestamp : (date) -> timestamp): use type casts to disambiguate"
  }
}

CREATE TABLE IF NOT EXISTS autonostixdq.engine_torque_by_last_date (
                                                                       id UUID,
                                                                       engine_torque text,
                                                                       vin_number text,
                                                                       last_updated timestamp,
                                                                       organization_id int,
                                                                       odometer int,
                                                                       stats_date timestamp,
                                                                       miles double,
                                                                       hours double,
                                                                       engine_runtime int,
                                                                       key_starts int,
                                                                       threshold text,
                                                                       PRIMARY KEY ((vin_number, organization_id), stats_date, odometer));

api 将日期作为字符串 '2022-05-27' 然后尝试两者。仅传递字符串“2022-05-27”。尝试将其设为“2022-05-27 00:00:00.000000+0000”。早些时候我在 cassandra 中将 stats_date 字段保留为 Date 类型并尝试传递 LocalTime 对象。

如何解决这个问题?

查看 table stats_date 的架构是一个时间戳。

正在查看 Spring 数据 Cassandra 映射 here java.util.Date 是此 CQL 类型的相关 Java 类型。

我建议使用 java 代码中的旧 java class SimpleDateFormatdataString 参数转换为 java.util.Date之前并使用以下内容更改存储库代码:

@Query(value = "select * from engine_torque_by_last_date " +
        "where vin_number = :vinNumber " +
        "and organization_id in :organizationId and " +
        "stats_date = :dateString")
List<EngineTorqueByLastDate> findByVinNumberAndOrganizationIdAndStatsDate(String vinNumber, Integer organizationId, Date dateString);

您可能完全摆脱 @Query,这是一个标准查询,您应该使用 Spring 数据中的约定。尝试将函数重命名为:

findByKeyVinNumberAndKeyOrganizationIdAndKeyStatsDate