• spring jpa cassandra:查询日期timestamp 时间戳出现的一些问题


    以下出现的问题,是基于版本

    <dependency>
       <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
          <version>2.3.4.RELEASE</version>
      </dependency>
    <dependency>
         <groupId>org.springframework.data</groupId>
         <artifactId>spring-data-cassandra</artifactId>
         <version>2.2.8.RELEASE</version>
     </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    环境要求:

    创建表语句sql

    CREATE TABLE recommend_table (
        id bigint,
        date_time timestamp,
        value text,
        PRIMARY KEY (id)
    )WITH caching={'keys':'ALL', 'rows_per_partition':'NONE'}
     AND compaction={'class':'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold':'32', 'min_threshold':'4'}
     AND compression={'chunk_length_in_kb':'64', 'class':'org.apache.cassandra.io.compress.LZ4Compressor'}
     AND dclocal_read_repair_chance=0.1
     AND speculative_retry='99PERCENTILE';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    bean 对象

    @Data
    @Table("recommend_table")
    @AllArgsConstructor
    @NoArgsConstructor
    public class INDArrayValue {
        @PrimaryKey
        private Long id;
        @Column("value")
        private String value;
        @Column("date_time")
        private Date dateTime;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其中主要的就是 字段 date_time类型是 timestamp, 实体类中 dateTime对应的是 java.util.Date 类型

    1:插入数据的问题,时区的问题

    如果是jpa.save(entity) 的话,会相差8个小时的时间,应该是东八区的影响
    日期通过这种方式格式化操作:

    			String date = "20220725";
                Date formatDat = null;
                try {
                    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
                    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));
                    formatDat = dateFormat.parse(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    通过设置 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));可以设置GMT+1,或者2,可以看看有数据库中有什么变化

    2:查询数据的问题

    SELECT * FROM recommend_table where date_time >= '2022-07-25Z' and date_time <= '2022-07-25Z'  limit 10 ALLOW FILTERING;
    
    • 1

    日期时间查询有一个UTC的问题,在查询的时候需要添加一个Z
    java - 处理 Cassandra 时间戳
    在这里插入图片描述

    问题1:Reason: Invalid parameter index! You seem to have declared too little query method parameters!

    出现问题代码原因:

        @Query(value="SELECT * FROM recommend_table where date_time >=  ?1  and date_time <=  ?2  limit 10 ALLOW FILTERING;",allowFiltering = true)
        List<INDArrayValue> queryINDArrayValueByDate(Date startDate, Date endDate);
    
    • 1
    • 2

    按理说jpa,在传递参数的时候,使用的是 ?1?2 分别表示的是第一个参数和第二个参数
    但是jpa cassandra 在传递参数的时候,确是得使用 ?0?1 才可以 。其中Date,是java.util.Date

    问题2:Expected 8 or 0 byte long for date (10)

    Expected 8 or 0 byte long for date (10); nested exception is com.datastax.driver.core.exceptions.InvalidQueryException: Expected 8 or 0 byte long for date (10)

    出现的原因是

        @Query(value="SELECT * FROM recommend_table where date_time >=  ?0  and date_time <=  ?1  limit 10 ALLOW FILTERING;",allowFiltering = true)
        List<INDArrayValue> queryINDArrayValueByDate(String startDate, String endDate);
    
    • 1
    • 2

    调用方法:

     return indArrayValueRepository.queryINDArrayValueByDate("2022-07-25","2022-07-25");
    
    • 1

    参数使用 String ,会抛出以上的异常信息。

    使用jpa正常的时间戳请求

    public interface INDArrayValueRepository extends CassandraRepository<INDArrayValue,Long> {
        @Query(value="SELECT * FROM recommend_table where date_time >=  ?0  and date_time <=  ?1  limit 10 ALLOW FILTERING;",allowFiltering = true)
        List<INDArrayValue> queryINDArrayValueByDate(Date startDate, Date endDate);
    }
    
    • 1
    • 2
    • 3
    • 4
        private static Date getDate(String date) {
            Date formatDat = null;
            try {
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));
                formatDat = dateFormat.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return formatDat;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    日期格式化调用方式

    		String startDate = "2022-07-25";
    		String endDate = "2022-07-26";
    		Date formatDat1 = getDate(startDate);
    		Date formatDat2 = getDate(endDate);
    		System.out.println(formatDat1+"\t" + formatDat2);
    		indArrayValueContext.queryINDArrayValueByDate(formatDat1, formatDat2);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    至此,解决了以上的问题

  • 相关阅读:
    海豚调度器分布式部署
    Java项目:ssm赛事打分系统
    解决报错之org.aspectj.lang不存在
    详细创建Prism架构wpf项目
    vue2升级vue3:vue2 vue-i18n 升级到vue3搭配VueI18n v9
    Edge的使用心得与深度探索
    融云 | 企业通讯录的设计与实现
    数据结构复习之图论
    conda虚拟环境安装pytorch(gpu版本)纪实
    leetcode/字符串中的所有变位词(s1字符串的某个排列是s2的子串)的左索引
  • 原文地址:https://blog.csdn.net/qq_20667511/article/details/126387757