Search code examples
gremlintinkerpop3amazon-neptune

Proper handling of date operations in Gremlin


I am using AWS Neptune Gremlin with gremlin_python.

My date in property is stored as datetime as required in Neptune specs.

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])

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()

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"?


Solution

  • 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
    

    ... 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)