如何使用Spring Data Cassandra从Cassandra时间戳类型列中选择java.util.Date值?

时间:2018-07-09 12:30:08

标签: timestamp where spring-data-cassandra

实体类:

 @Table("user")
    public class UserDAO {

    @PrimaryKey
    private UserKey userKey;

    protected UserDAO () {
        super();
    }

    public UserDAO (UserKey userKey, short version) {
        super();
        this. userKey = userKey;
        this.version = version;
    }
    private short version;

    public UserKey getUserKey () {
        return responseTimeKey;
    }

    public void setUserKey(UserKey responseTimeKey) {
        this. userKey = userKey;
    }



    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        …
        return builder.toString();
    }
}

组合键类:

@PrimaryKeyClass
public class UserKey implements Serializable {

    protected UserKey () {
        super();
    }

    public UserKey (String name, Date date) {
        super();
        this.name = name;       
        this.date = date;

    }


    @PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private String name;
    @PrimaryKeyColumn(ordinal = 1, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
    @CassandraType(type = Name.DATE)
    private Date date;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public int hashCode() {
        …
    }

    @Override
    public boolean equals(Object obj) {
        …
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        …
        return builder.toString();
    }
}

存储库界面:

public interface UserRepository extends CassandraRepository<UserDAO, UserKey> {
    List<UserDAO> findByUserKeyNameAndUserKeyDate(String name, Date date);

    List<UserDAO> findAll();
}
  • 日期是java.util.Date
  • Spring Boot:v2.08
  • Spring Data Cassandra:v2.0.8
  • Cassandra:[cqlsh 5.0.1 |卡桑德拉3.11.2 | CQL规范3.4.4 | 原生协议v4]

我正在使用application.properties创建与Cassandra的自动连接。 当我从控制器调用userRepository.findAll()时,我将获得所有带有预期Date的值。

[
{
    "userKey": {
        "name": "abc",
        "date": "2018-05-21T00:00:00.000+0000"
    },
    "version": 1
},
{
    "userKey": {
        "name": "bcd",
        "date": "2018-05-21T00:00:00.000+0000"
    },
    "version": 1
},
{
    "userKey": {
        "name": "def",
        "date": "2018-05-21T00:00:00.000+0000"
    },
    "version": 1
}
]

但是当我调用userRepository.findByUserKeyNameAndUserKeyDate()时,在传递字符串名称和java.util.Date日期作为参数的同时,触发以下查询并返回零结果。

日志:

Created query [SELECT * FROM user WHERE name='abc' AND date=1526841000000;]

cqlsh 中,我得到了

的结果
1.  SELECT * FROM user WHERE name='abc' AND date=toTimestamp('2018-05-21');
2.  SELECT * FROM user WHERE name ='abc' AND date='2018-05-21'

创建表查询:

CREATE TABLE mykeyspace.user (
    name text,
    date timestamp,
    version int,
    PRIMARY KEY (name, date)
) WITH CLUSTERING ORDER BY (date DESC)

表格条目:

name    | date                            | version
---------+---------------------------------+---------
    abc | 2018-05-21 00:00:00.000000+0000 |       1
    bcd | 2018-05-21 00:00:00.000000+0000 |       1
    def | 2018-05-21 00:00:00.000000+0000 |       1

我尝试使用java.sql.Timestamp-相同的结果; com.datastax.driver.core.LocatDate-findAll()不能将java.util.Date转换为LocalDate,而findByUserKeyNameAndUserKeyDate()可以得到相同的结果。

请提供有关如何按日期查询以及我在这里缺少什么的帮助?

0 个答案:

没有答案