Thirumal
Thirumal

Reputation: 9646

What is the correct way to store date/timestamp in Gremlin based database like Neptune?

What is the correct way to store date/time stamp in a gremlin-based database like Amazon Neptune?

When I try the following in python

current_timestamp = datetime.datetime.now()
g.E('d').property('otp_verified_on', current_timestamp).next()

it gives me output, something similar to

'otp_verified_on': datetime.datetime(2021, 11, 23, 9, 47, 30, 173000)

so I type cast to string

current_timestamp = datetime.datetime.now()
g.E('d').property('otp_verified_on', str(current_timestamp)).next()

to get the following result

'otp_verified_on': '2021-11-23 08:27:45.867543'

My queries are based on date comparison operation gte, lte`, etc...

Which is the better/correct way to implement?

Upvotes: 1

Views: 762

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14391

Neptune will convert Python dates into an appropriate internal form and you can do evaluations directly on the dates. Here is a set of examples. You do not need to (and should not) cast to strings. An alternative is to store epoch offset integers but in the case of Neptune, using the native Python dates will be slightly more efficient in terms of how the computations are performed. You should check the documentation for each database you may use but in the case of Neptune using the native Python datetime is the best way to go.

>>> import datetime

>>> g.addV('test').property('ts',datetime.datetime.now()).next()
v[eebec326-001f-577b-0893-bbd0ddf5e271]

>>> g.addV('test').property('ts',datetime.datetime.now()).next()
v[3ebec326-0628-8b3a-5d4e-9d7292075072]

>>> g.V().hasLabel('test').valueMap(True).toList()
[{<T.id: 1>: 'eebec326-001f-577b-0893-bbd0ddf5e271', <T.label: 4>: 'test', 'ts': [datetime.datetime(2021, 12, 4, 10, 34, 27, 448000)]}, 
{<T.id: 1>: '3ebec326-0628-8b3a-5d4e-9d7292075072', <T.label: 4>: 'test', 'ts': [datetime.datetime(2021, 12, 4, 10, 34, 30, 589000)]}]

>>> g.V().hasLabel('test').has('ts',lt(datetime.datetime.now())).toList()
[v[eebec326-001f-577b-0893-bbd0ddf5e271], v[3ebec326-0628-8b3a-5d4e-9d7292075072]]

>>> g.V().hasLabel('test').has('ts',gt(datetime.datetime.now())).toList()
[]  

Upvotes: 1

Related Questions