How can I get a DetachedVertex rather than ReferenceVertex using remote Graph (JanusGraph)

570 views
Skip to first unread message

XIAYANG LEE

unread,
Nov 28, 2017, 2:02:08 AM11/28/17
to Gremlin-users
Hi,

I am using the following environment to retrieve a remote Graph:

    "com.michaelpollmeier" %% "gremlin-scala" % "3.2.5.0",
"org.janusgraph" % "janusgraph-core" % "0.1.1",
"org.janusgraph" % "janusgraph-cassandra" % "0.1.1",
"org.apache.tinkerpop" % "gremlin-driver" % "3.2.5",

val scalaGraph: ScalaGraph = EmptyGraph.instance().asScala().configure(_.withRemote(DriverRemoteConnection.using(cluster, "g"))
val g
: GraphTraversalSource = EmptyGraph.instance().traversal().withRemote(DriverRemoteConnection.using(cluster, "g"))



However, the vertex that I created on the using either scalaGraph or g are all RefereceVertex, which has very limited infor. when I am want to retrieve properties(where I have to call g.V().valueMap(true).toList())

I happened to see an example using DSE gremlin-scala, and found the vertex type in the example is DetachedVertex where has all infor. like label property within a Vertex. so wondering is Sth. I missed here if I want to get the DetachedVertex from the Remote Graph, just want to have the properties on the vertex when working on the Remote Graph, does this relevant to the serializer
val janusGraphMapper = GryoMapper.build.addRegistry(JanusGraphIoRegistry.getInstance())
val cluster
: Cluster = Cluster.build
 
.addContactPoint("[MyRemoteGraph Host]")
 
.port(80)
 
.serializer(new GryoMessageSerializerV1d0(janusGraphMapper))
 
.create


Cannot find docs around this DetachedVertex and ReferenceVertex, so hopefully can find some help here.
Thanks a lot in advanced.
Alex


Stephen Mallette

unread,
Nov 29, 2017, 10:38:35 AM11/29/17
to Gremlin-users
There is no way to get "detached" graph elements using remote traversals (they might still return as "detached" if you send Gremlin scripts, but I imagine even that will eventually change). Generally speaking, you should avoid returning graph elements (Vertex, Edge, VertexProperty) at all from your traversals. In SQL you would generally not do:

SELECT * FROM table

Returning a graph element is quite similar - consider the problem you might have if you were to return a Vertex that had one or more large multi-properties on it. That would make the traversal expensive. In addition, serialization costs for a graph element with all properties is more expensive than more generic containers like Map and List. Long story, shortened up: The general recommendation is to be explicit in what data you want returned in your Gremlin.


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/10ae6064-ee5c-4d61-993a-6e8c6019543c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

XIAYANG LEE

unread,
Nov 29, 2017, 11:42:02 PM11/29/17
to Gremlin-users
Thanks Stephen,

I get your point, so what is the best practise to get a particular property of a element then? Say I want to retrieve a "LastModified" property of all outgoing Edges. Sth. like this?

val t = g.V(v1).outE().valueMap(ture, "LastModified").toList()

this gives me t of type: util.List[util.Map[AnyRef, Nothing]]

Thanks,
Alex
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Stephen Mallette

unread,
Nov 30, 2017, 6:53:26 AM11/30/17
to Gremlin-users
using valueMap() with the field names is one approach. you did spell "true" wrong in your example...not sure if that's the problem with the output you expect. Note that it works fine in java/groovy:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).outE().valueMap(true,'weight')
==>[label:created,weight:0.4,id:9]
==>[label:knows,weight:0.5,id:7]
==>[label:knows,weight:1.0,id:8]

You have other options besides valueMap() - also take a look at project() and select() as ways to extract the specific data you need from graph elements.



To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/031a8f19-a19e-4496-89b9-70048a2cb15b%40googlegroups.com.
Message has been deleted
Message has been deleted

Robert Dale

unread,
Nov 30, 2017, 7:22:20 PM11/30/17
to gremli...@googlegroups.com
gremlin> g.V().has("uuid", "aaa").repeat(__.as('person').flatMap(__.outE().as('ref').where('ref',eq('person')).by('LastReferenceUpdate').inV())).emit().path().by(valueMap('uuid'))
==>[[uuid:[aaa]],[uuid:[bbb]]]
==>[[uuid:[aaa]],[uuid:[ccc]]]
==>[[uuid:[aaa]],[uuid:[ccc]],[uuid:[fff]]]


Robert Dale

On Thu, Nov 30, 2017 at 7:12 PM, Alex LI <zhal...@gmail.com> wrote:
Thanks Stephen,

Sorry, that was a typo. I guess there is a bit of tuning when writing code between Gremlin Console and Application (Java/Scala) so I was not able to get the value from the valueMap() in Application, and select() works perfectly fine here, thanks for the trick.

Also, I am struggling to write a query to have a conditional traversal in Gremlin, say I need to return the path of "vertexA" only when outgoing Edge property "prop" matches same property value of its outgoing vertex.
For example, Graph:
gremlin> graph = TinkerGraph.open()
gremlin> g = graph.traversal(standard())
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g
                   
.addV("person").property("prop", 500).property("uuid", "aaa").as("A")
                    .addV("person").property("prop", 300).property("uuid", "bbb").as("B")
                   
.addV("person").property("prop", 450).property("uuid", "ccc").as("C")
                   
.addV("person").property("prop", 0).property("uuid", "ddd").as("D")
                   
.addV("person").property("prop", 0).property("uuid", "eee").as("E")
                   
.addV("person").property("prop", 0).property("uuid", "fff").as("F")
                   
.addV("person").property("prop", 400).property("uuid", "ggg").as("G")

                   
.addE("ref").from("A").to("B").property("prop", 500)
                   
.addE("ref").from("B").to("D").property("prop", 200)
                   
.addE("ref").from("A").to("C").property("prop", 500)
                   
.addE("ref").from("C").to("E").property("prop", 400)
                   
.addE("ref").from("C").to("F").property("prop", 450)
                   
.addE("ref").from("G").to("E").property("prop", 400)
                   
.addE("ref").from("G").to("F").property("prop", 400)
                   
.iterate()
gremlin> va = g.V().has("uuid", "aaa").next()

for query on va, I want to have path [A, B], [A, C], [A, C, F], while [A, C, E] is not returned as "prop" of edge between C and E is 450, which doesn't mach the value of its outV C's "prop" (450).
I came up with match() and repeat(), but cannot make it work:
            val t = g.V(vA).`match`(
                            __.as("v1").values("prop").as("vertexValue"),
                            __.as("v1").outE().as("e1").values("prop").as("edgeValue"),
                            __.as("e1").inV().as("v2"),
                            __.as("v1").repeat(__.out()).until(_.outE.count.is(JLong.valueOf(0))))
                            .select("v1", "v2").toList
and also tried:
g.V(vA)
       
.repeat(__.out())
       
.until(__.values("prop").as("p1").outE().values("prop").as("p2")
               
.where("p1", P.neq("p2")).count().is(0))
       
.path()
       
.toList
but it has error as well: java.lang.Integer incompatible with org.apache.tinkerpop.gremlin.structure.Element

Having this code in scala so a bit different syntax from Gremlin Console, wondering am I in the wrong direction as not very familiar with all other gremlin steps I can use to fit in this scenario.

Thanks for your help in advance again.
Cheers,
Alex

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
Message has been deleted

Alex LI

unread,
Nov 30, 2017, 8:21:43 PM11/30/17
to Gremlin-users
Wow, thanks a lot Robert. that works like a charm :D

So a few questions to help me better understand this:
1. how does the terminate condition inject into the flatMap step? I was using until but couldn't get it right.
2. Is the inV() at the end of the flatMap pass back to the repeat step every time it repeats? or the emit() is the value that pass back to the repeat step?
3. if I want to keep track of the edges in the path as well, is that just:
val others = g.V().has("uuid", "aaa").outE().inV()
        .repeat(__.as("person")
                    .flatMap(__.outE().as("ref")
                            .where("ref", P.eq("person"))
                            .by("LastReferenceUpdate")
                            .inV()))
        .emit()
        .path()
        .toList()

but it kind of start with the next vertex rather than va.

Thanks again for your help.
Cheers,
Alex

Robert Dale

Robert Dale

unread,
Nov 30, 2017, 8:58:35 PM11/30/17
to Gremlin-users

On Thursday, November 30, 2017 at 8:21:43 PM UTC-5, Alex LI wrote:
Wow, thanks a lot Robert. that works like a charm :D

So a few questions to help me better understand this:
1. how does the terminate condition inject into the flatMap step? I was using until but couldn't get it right.

repeat will end when its traversals have no more results.  In this case, when there are no more out edges.  Since your model is a DAG with no other stop constraint was required, we could just let repeat go until it runs out of edges to traverse. It's equivalent to until(outE().count().is(0))
 
2. Is the inV() at the end of the flatMap pass back to the repeat step every time it repeats? or the emit() is the value that pass back to the repeat step?

The inV() is passed back to the beginning of the repeat. emit() is used to pass things from the repeat to downstream.
 
3. if I want to keep track of the edges in the path as well, is that just:

No, flatMap() was used to hide the edge from the path.  Since you want the edge, it's simpler...
g.V().has("uuid", "aaa").
   repeat(__.as('person').outE().where(eq('person')).by('LastReferenceUpdate').inV()).
   emit().path().by(valueMap(true,'uuid'))

Alex LI

unread,
Nov 30, 2017, 9:26:45 PM11/30/17
to Gremlin-users
Now I see why use the flatMap :D
Thanks a lot for all your help, Stephen and Robert.

Cheers,
Alex

Antonio

unread,
Jan 4, 2019, 11:04:04 AM1/4/19
to Gremlin-users
Hi all,
I'm trying to get a Vertex list using remote graph (using java) but it returns a ReferenceVertex without properties...
So following this discussion I get that I have to use valueMap()

But using it I get a Map with values as an ArrayList<E>, so I have to add logic to get the value as shown below:

Query:
g.V().hasLabel("aLabel").valueMap(true, "name").toList();

Result:
name=[my name]


Where I'm wrong?

Thanks in advance

Antonio

unread,
Jan 15, 2019, 2:49:50 PM1/15/19
to Gremlin-users

Any suggestion?

I'm facing with this issue converting a value map to my entity object :(

value map always returns a map with arraylist as values wich require additional "parsing" logic
Is there an alternative?

Daniel Kuppitz

unread,
Jan 15, 2019, 3:29:54 PM1/15/19
to gremli...@googlegroups.com
In TinkerPop 3.3.x you can unfold and regroup the map entries:

gremlin> g.V().hasLabel("software").valueMap(true)
==>[label:software,name:[lop],lang:[java],id:3]
==>[label:software,name:[ripple],lang:[java],id:5]
gremlin> g.V().hasLabel("software").valueMap(true).map(unfold().group().by(keys).by(select(values).unfold()))
==>[label:software,name:lop,lang:java,id:3]
==>[label:software,name:ripple,lang:java,id:5]


Starting with TinkerPop 3.4.0 it's much simpler:

gremlin> g.V().hasLabel("software").valueMap(true).by(unfold())
==>[label:software,name:lop,lang:java,id:3]
==>[label:software,name:ripple,lang:java,id:5]

// or:

gremlin> g.V().hasLabel("software").valueMap(true).by(limit(local, 1))
==>[label:software,name:lop,lang:java,id:3]
==>[label:software,name:ripple,lang:java,id:5]

I assume that limit(local, 1) will perform slightly better but I haven't tested it.

Cheers,
Daniel


Juan David Rodriguez Ariza

unread,
Nov 6, 2020, 12:28:55 PM11/6/20
to Gremlin-users
Hi Antonio,
have u resolved this issue? instead of using valueMap()? 
Thanks.

The information contained in this e-mail may be confidential. It has been sent for the sole use of the intended recipient(s). If the reader of this message is not an intended recipient, you are hereby notified that any unauthorized review, use, disclosure, dissemination, distribution or copying of this communication, or any of its contents, is strictly prohibited. If you have received it by mistake please let us know by e-mail immediately and delete it from your system. Many thanks.

 

La información contenida en este mensaje puede ser confidencial. Ha sido enviada para el uso exclusivo del destinatario(s) previsto. Si el lector de este mensaje no fuera el destinatario previsto, por el presente queda Ud. notificado que cualquier lectura, uso, publicación, diseminación, distribución o copiado de esta comunicación o su contenido está estrictamente prohibido. En caso de que Ud. hubiera recibido este mensaje por error le agradeceremos notificarnos por e-mail inmediatamente y eliminarlo de su sistema. Muchas gracias.


Stephen Mallette

unread,
Nov 6, 2020, 12:38:06 PM11/6/20
to gremli...@googlegroups.com
you can use elementMap() on newer version of Gremlin:


or valueMap().by(unfold()) or project() or a variety of other approaches to get the data to the form you want.


Reply all
Reply to author
Forward
0 new messages