Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get both edge and vertex properties? #270

Closed
kyeljmd opened this issue Dec 16, 2018 · 4 comments
Closed

How to get both edge and vertex properties? #270

kyeljmd opened this issue Dec 16, 2018 · 4 comments

Comments

@kyeljmd
Copy link

kyeljmd commented Dec 16, 2018

After looking at the library I could not seem to find an example on How to get both edge and vertex properties in one query? is this possible with the current implementation?

@mpollmeier
Copy link
Owner

you could use as/select as demonstrated in https://github.com/mpollmeier/gremlin-scala/blob/master/gremlin-scala/src/test/scala/gremlin/scala/SelectSpec.scala#L77

Note though that select.by doesn't work yet, there's an open PR that almost works: #241

@kyeljmd
Copy link
Author

kyeljmd commented Dec 18, 2018

@mpollmeier I tried to follow one of the examples on the SelectSpec. So I came up with something like this.

val itm = g.V.has(KeyValue(TripModel.ID, id)).as("a").outE().as("e").select.head

Whenever I try to print out the value
println(itm)

I always end up with a value like this.

e[{~label=includes, ~out_vertex={~label=trip, tripId=465760a0-073d-46a5-ab1e-debdb86de5b2}, ~in_vertex={~label=business, businessId=cf8c84c6-0fa4-4482-839d-19d9fa8ceb52}, ~local_id=1b214ce0-0141-11e9-b27f-6d2c86545d91}][{~label=trip, tripId=465760a0-073d-46a5-ab1e-debdb86de5b2}-includes->{~label=business, businessId=cf8c84c6-0fa4-4482-839d-19d9fa8ceb52}]

I have also tried something similar to one of the test spec which is this one.

val itm = g.V.has(KeyValue(TripModel.ID, id)).as("a").outE().as("e").select.head._1

The resulting code from above throws this exception

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ClassCastException: org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge cannot be cast to scala.Tuple2]] at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:251) at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:178) at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:363) at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:361) at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:413) at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37) at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60) at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55) at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91) at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12) Caused by: java.lang.ClassCastException: org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge cannot be cast to scala.Tuple2 at service.TripService.getTrip(TripService.scala:41) at controllers.trip.TripController.$anonfun$getTrip$1(TripController.scala:21) at play.api.mvc.ActionBuilder.$anonfun$apply$11(Action.scala:363) at scala.Function1.$anonfun$andThen$1(Function1.scala:52) at play.api.mvc.ActionBuilderImpl.invokeBlock(Action.scala:482) at play.api.mvc.ActionBuilderImpl.invokeBlock(Action.scala:480) at play.api.mvc.ActionBuilder$$anon$9.invokeBlock(Action.scala:331) at play.api.mvc.ActionBuilder$$anon$9.invokeBlock(Action.scala:326) at play.api.mvc.ActionBuilder$$anon$2.apply(Action.scala:419) at play.api.mvc.Action.$anonfun$apply$2(Action.scala:96)

Is there anything I have been doing wrong?

@mpollmeier
Copy link
Owner

This works with Tinkergraph (haven't tested with a remote graph):

import gremlin.scala._
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory

implicit val graph = TinkerFactory.createModern.asScala
val g = graph.traversal

val vLabel = StepLabel[Vertex]()
val propLabel1 = StepLabel[java.util.Map[String, Object]]()
val propLabel2 = StepLabel[java.util.Map[String, Object]]()
val trav = g.V.as(vLabel).valueMap().as(propLabel1).select(vLabel).outE.valueMap().as(propLabel2)
trav.select((propLabel1, propLabel2)).toList
// List[(java.util.Map[String,Object], java.util.Map[String,Object])] = List(({name=[marko], age=[29]},{weight=0.4}), ({name=[marko], age=[29]},{weight=0.5}), ({name=[marko], age=[29]},{weight=1.0}), ({name=[josh], age=[32]},{weight=1.0}), ({name=[josh], age=[32]},{weight=0.4}), ({name=[peter], age=[35]},{weight=0.2}))

Otherwise I guess it's better to run two queries (one for vertices, one for edges) and combine at the client.

@kyeljmd
Copy link
Author

kyeljmd commented Jan 18, 2019

Thanks @mpollmeier We have tried that approach but it's quite hard to the marshalling for it. We ended up performing another query just to retrieve the other properties that we have needed.

@kyeljmd kyeljmd closed this as completed Jan 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants