Search code examples
javagremlintinkerpop3amazon-neptune

gremlin convert string propery to numeric property


Currently I have a graph having timestamp as string property

g.V().order().by('timestamp', '10')

This causes issues while sorting as string sorting is different from numeric sorting.

String sorting  : 1, 10, 2
Numeric sorting : 1, 2, 10

Is there any way to convert all the timestamp properties to Long(Numeric) in gremlin or how to query gremlin to use numeric sort. Open for suggesstions.

If i use Order.desc with timestamp property it is throwing null pointer exception but Order.decr works fine. Any idea? Thanks in advance.


Solution

  • If you want to convert all of the strings to integers the simplest way is going to be to do it in the application along the lines of:

    1. Get the property using Gremlin
    2. Convert it to an integer in your application
    3. Use Gremlin to write the value back and replace the prior one (be sure to use the Cardinality.single keyword).

    An alternative way would be to export your graph to CSV, update the CSV and reload it. Depending on the size of your graph that could be a better option.

    The other way would be to use in-line code (lambdas) but if you are using Amazon Neptune that is not an option as they are not permitted.

    As to Oder.decr and Order.desc both should work unless your Neptune engine version or Gremlin Client version are back level by quite a long way.