正确处理Gremlin中的日期运算 [英] Proper handling of date operations in Gremlin
问题描述
我正在将AWS Neptune Gremlin与 gremlin_python
一起使用。
I am using AWS Neptune Gremlin with gremlin_python
.
我在属性中的日期根据需要存储为datetime在 Neptune规范中。
My date in property is stored as datetime as required in Neptune specs.
我是使用Python这样的代码创建的:
properties_dict ['my_date'] = datetime.fromtimestamp(my_date, timezone.utc)
,然后构造具有以下属性的顶点:
I created it using Python code like this:
properties_dict['my_date'] = datetime.fromtimestamp(my_date, timezone.utc)
and then constructed the Vertex with properties:
for prop in properties:
query += """.property("%s", "%s")"""%(prop, properties[prop])
稍后,当与构造的图形进行交互时,我只能通过精确的字符串匹配查询找到顶点,如下所示:
gV()。hasLabel('Object')。has( my_date, 2017-12-01 00:00:00 + 00:00)。valueMap(True).limit( 3).toList()
Later when interacting with the constructed graph, I am only able to find the vertices by an exact string matching query like the following:
g.V().hasLabel('Object').has("my_date", "2017-12-01 00:00:00+00:00").valueMap(True).limit(3).toList()
在Gremlin中处理日期或日期时间的最佳方法是什么?
如何我会进行范围查询,例如给我所有日期为2017年的顶点 ?
What's the best way for dealing with date or datetime in Gremlin?
How can I do range queries such as "give me all Vertices that have date in year 2017"?
推荐答案
个人而言,我更喜欢将日期/时间值存储为自时代以来的天/秒/毫秒。这绝对可以在任何Graph DB上使用,并使范围查询更加简单。此外,转换到历元后的天数或秒数应该是几乎任何语言的简单方法调用。
Personally, I prefer to store date/time values as days/seconds/milliseconds since epoch. This will definitely work on any Graph DB and makes range queries much simpler. Also, the conversion to days or seconds since epoch and back should be a simple method call in pretty much any language.
因此,当您创建属性字典时,您可以通过将代码更改为以下代码来简化代码:
So, when you create your properties dictionary, you could simplify your code by changing it to:
properties_dict['my_date'] = my_date
...作为 my_date
应该代表自纪元以来的秒数。范围查询将非常简单:
... as my_date
should represent the number of seconds since epoch. And a range query would be as simple as:
g.V().has("Object", "my_date", P.between(startTimestamp, endTimestamp)).
limit(3).valueMap(True)
这篇关于正确处理Gremlin中的日期运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!