【问题标题】:How to compare two timestamp columns in a Gremlin Java query如何比较 Gremlin Java 查询中的两个时间戳列
【发布时间】:2020-03-16 07:45:41
【问题描述】:

有没有办法在 Gremlin 遍历中做类似的事情? 我本以为这很明显,但似乎我错了。 我有一个包含两个日期(都是时间戳)的表,我想只选择一个大于另一个的记录。比如:

has('date_one', P.gt('date_two'))
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: timestamp with time zone > character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.  

那么第二个参数不会被翻译成列'date_two'的值。

基于答案 1,完整的请求变为:

g.V().hasLabel('File').where(or (
__.out('has_Ref1').hasNot('date_one'), 
__.out('has_Ref1').as('s1', 's2').where('s1', gt('s2')).by('date_two').by('date_one')))
.as('file').out('has_Ref1').as('ref1').out('has_Content').as('data').select('file','ref1','data')
But in this case: A where()-traversal must have at least a start or end label (i.e. variable): [OrStep([[VertexStep(OUT,[has_Ref1],vertex), NotStep([PropertiesStep([date_one],value)])], [VertexStep(OUT,[has_Ref1],vertex)@[s1, s2], WherePredicateStep(s1,gt(s2),[value(date_two), value(date_one)])]])]

我猜 or 子句的第二个参数必须是布尔值。然后,如果我尝试添加 '.hasNext()',则会出现以下异常:

g.V().hasLabel('File').where(or (
__.out('has_Ref1').hasNot('date_one'), 
__.out('has_Ref1').as('s1', 's2').where('s1', gt('s2')).by('date_two').by('date_one').hasNext()))
.as('file').out('has_Ref1').as('ref1').out('has_Content').as('data').select('file','ref1','data')
 groovy.lang.MissingMethodException: No signature of method: static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.or() is applicable for argument types: (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal...) values: [[VertexStep(OUT,[has_Ref1],vertex), NotStep([PropertiesStep([date_one],value)])], ...]

【问题讨论】:

  • 该错误表明您的“真实”查询与您所写的查询不同。我怀疑asg.modifiedAt 应该是modifiedAt?另外,您确定 all Ref 节点上存在 both 值吗?也许最好在比较之前检查值是否存在。
  • 我不确定所有值都存在。条件是“如果 date_one 为 null 或者如果 date_two > date_one 则选择
  • 所以有可能定义了 date_one,但是 date_two 为 null 会失败。也许检查 date_two 也不为空。 asg 前缀呢?顺便问一下,您使用的是哪个数据库服务器?
  • date_two 永远不会为空。您可以忘记前缀,这不是这里的问题。问题只是“如果 ref1.date_one 为 null 或 ref1.date_one

标签: java postgresql timestamp gremlin


【解决方案1】:

has() 步骤并不能完全按照这种方式工作,因为P 处理提供给它的常量值并且不将该值视为属性键。在这种情况下,您可能希望使用where() 步骤,以便您可以利用traversal induced values

gremlin> g.addV('person').property('d1',1).property('d2',2).
......1>   addV('person').property('d1',2).property('d2',1).iterate()
gremlin> g.V().as('x','y').where('x', gt('y')).by('d1').by('d2').elementMap()
==>[id:3,label:person,d1:2,d2:1]

【讨论】:

  • 谢谢。我没有得到我们可以与不同的值 .by('d1').by('d2') 进行比较。我仍然有问题,可能是因为条件还在 where 子句中:".where(or (" + ".out('has_Ref').hasNot('date_one'), " + ".out('has_Ref').as('s1', 's2').where('s1', gt('s2')).by('date_two').by('date_one')) )";
  • 我不太确定我是否再关注您的问题。您可能需要编辑您的问题以包含一些示例数据和更多您的实际遍历。
【解决方案2】:

如果data_one 大于date_two,你为什么不问问 Postgres

SELECT '2019-11-20'::timestamp > '2019-11-01'::timestamp;
 ?column? 
----------
 t
(1 row)

Time: 10.246 ms

这为您提供了一个布尔值

【讨论】:

  • 你是对的。我直接在pgAdmin中测试了它。但我必须使用的上下文是 Gremlin 的查询。
【解决方案3】:

我假设您正在谈论单个表中的列。 我主要使用 gremlin 与 graphDB 合作,但我认为它仍然适合。

尽你所能获得关注的表格 g.V().has(...).has()...

然后直接使用project 或使用as & select 将相同的数据投影两次 ...as('named1', 'named2').select('named1')

它在流程中创建两个相同数据的副本,然后您可以在不同的基础上比较它们,例如 ...where('named1', gt('named2')).by('value1').by('value2')

上面的英文字面意思是只给我带来那些named1的value1大于named2的value2的选定内容 如果你只给一个by,那么它会比较相同的属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-07
    • 2021-04-25
    • 2020-08-18
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2015-01-02
    相关资源
    最近更新 更多