Search code examples
gremlintinkerpopamazon-neptune

How do we convert string to number in a gremlin step?


I have a graph which has vertex E1 with property "price" and "name" which are storing String values. I need to calculate sum of the column "price" grouped by "name". I am writing the below query using Java:

g.withSideEffect("Neptune#repeatMode","BFS")
.V().hasLabel("E1").group()
.by("name").by(values("price").unfold().sum())
.unfold()
.project("rowName","data")
.by(select(keys).properties(MandatoryCustomerAttributes.firstName.name()).value())

I am getting this error:

{
"requestId": "38b781ce-fc02-4f7d-a71e-476dfd1925ce",
"code": "UnsupportedOperationException",
"detailedMessage": "java.lang.String cannot be cast to java.lang.Number"

}

Please help me in converting the String to any number format so that I can do some mathematical operations.


Solution

  • At this time, you can not do such a conversion with Gremlin steps unless you use a lambda step (which is not always possible depending on the graph database you are using and since you tagged this question with Neptune, you definitely can't take that approach - it also isn't advisable). You would need to store your data natively as a number or do the conversion and related mathematical calculation within your application. There is possibility that Gremlin will address this limitation in 3.7.0 as part of the various primitive operations aimed at String.