【问题标题】:Comparing LocalDate with date part of LocalDateTime in apache ignite将 LocalDate 与 apache ignite 中 LocalDateTime 的日期部分进行比较
【发布时间】:2017-01-06 02:51:53
【问题描述】:

我在 ignite 缓存中有一些记录,我想检索当天的所有记录。为此,我需要将缓存对象的 LocalDateTime 类型字段与 Localdate 对象,即 LocalDate.now() 进行比较。我如何编写查询来执行此操作。在 oracle TODATE(date, format) 中做同样的事情,但这个函数在 H2 中不存在。 缓存字段日期时间:2016-08-30T05:31 日期实例:2016-08-30 SQL 会像 String sql = "select * from cacheName where date='convert(datetime) to date'"; H2有可能吗?

【问题讨论】:

    标签: date datetime h2 ignite


    【解决方案1】:

    您可以为此使用自定义 SQL 函数:https://ignite.apache.org/releases/mobile/org/apache/ignite/cache/query/annotations/QuerySqlFunction.html

    例如,如果您的值类如下所示:

    public class MyValue {
        @QuerySqlField
        private LocalDateTime time;
    
        public MyValue(LocalDateTime time) {
            this.time = time;
        }
    }
    

    你可以像这样创建一个函数:

    public static class MyFunctions {
        @QuerySqlFunction
        public static String toDate(LocalDateTime time) {
            return time.format(DateTimeFormatter.ISO_LOCAL_DATE);
        }
    }
    

    并在这样的配置中提供它:

    cacheCfg.setSqlFunctionClasses(MyFunctions.class);
    

    查询将如下所示:

    select * from MyValue where toDate(time) = '2016-08-30'
    

    【讨论】:

    • 您好,感谢您的回复。是否可以避免“@QuerySqlFunction”注释并使用 QueryEntity 等 xml 为“@QuerySqlField”提供此注释?
    • 不,这是不可能的。但为什么这对你来说是个问题?
    • 我把它整理出来了,不是什么大问题,我们只是想在一个地方点燃依赖关系,这个模型对象在模块之外。但移动了项目以使其正常工作。感谢您再次回复。
    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    相关资源
    最近更新 更多