How to add set as vertex property in java gremlin

1,004 views
Skip to first unread message

Praneeth Dogiparthi

unread,
Feb 13, 2019, 2:34:24 PM2/13/19
to Gremlin-users
When i trying to add set as vertex property it is showing me error.

                         Set<String> set1=new HashSet<>();

set1.add("o1");

set2.add("o2");

set3.add("o3");

Set<String> set2=new HashSet();

set2.add("h1");

set2.add("h2");

g.addV("label").property(T.id,"id").next();


g.V("id").property(Cardinality.set, "set1",set1)

.property(Cardinality.single, "text", "hi")

.property(Cardinality.set, "set2",set2)

.next();

when this code


org.apache.tinkerpop.gremlin.driver.exception.ResponseException: {"requestId":"f92691e1-1f28-42ed-8fb0-2b4cf3cb48c1","code":"UnsupportedOperationException","detailedMessage":"Unsupported property value type: java.util.LinkedHashSet"}




if i loop it works

g.V("id").property(Cardinality.set"set1","o1").next();

g.V("id").property(Cardinality.set"set1","o2").next();


when trying to add multiple times it works .


Is there any way with out looping i mean adding the entire set at once.





Daniel Kuppitz

unread,
Feb 13, 2019, 9:24:57 PM2/13/19
to gremli...@googlegroups.com
Multi-valued properties are different from List and/or Set properties.

Here's how you can do it:

gremlin> set1 = ["o1","o2","o3"] as Set
==>o1
==>o2
==>o3
gremlin> set2 = ["h1","h2"] as Set
==>h1
==>h2

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.inject(["set1": set1, "set2": set2]).as("props").
           addV("label").
             property(id, "id").
             property(single, "text", "hi").as("v").
           select("props").unfold().as("prop").
           select(values).unfold().as("val").
           select("v").
             property(set, select("prop").by(keys), select("val")).
           iterate()

gremlin> g.V().valueMap(true)
==>[id:id,label:label,set2:[h1,h2],set1:[o1,o2,o3],text:[hi]]

The snippet uses TinkerGraph, but the same code should work for pretty much any graph provider that supports multi-valued (set) properties.

Cheers,
Daniel


--
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-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/7ace49ff-adf3-4bef-bef1-b66fa5cd2324%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Praneeth Dogiparthi

unread,
Feb 14, 2019, 12:04:28 AM2/14/19
to Gremlin-users



I tried to add properties in loop as mentioned in above question but when i tried to get vertex with all properties like this

g.V().has("id").properties().group().by(key()).by(value()).next();

the output is
{ "text": "hi" ,"set1":"o2","set2":"h1"}

it is not displaying all properties but when i use 
g.V().has("id").valueMap(true).next();
it is displaying all properties

when i use propject 

g.V("id").project("text", "set1","set2").by("text").by("set1").by("set2").next();


Here is the error


Multiple properties exist for the provided key, use Vertex.properties(set1).


Can you help me i need to display all properties in the json format. 


required output:


{ "text": "hi" ,"set1":["o1","o2","o3"],"set2":["h1","h2","h3"]}


Daniel Kuppitz

unread,
Feb 14, 2019, 1:11:01 AM2/14/19
to gremli...@googlegroups.com
The easiest would be:

gremlin> g.V().valueMap()
==>[set2:[h1,h2],set1:[o1,o2,o3],text:[hi]]

But I guess you don't want to get single-valued properties wrapped in lists, so project() will be your best choice:

gremlin> g.V().project("text","set1","set2").
                 by("text").
                 by(values("set1").fold()).
                 by(values("set2").fold())
==>[text:hi,set1:[o1,o2,o3],set2:[h1,h2]]

Cheers,
Daniel


Praneeth Dogiparthi

unread,
Feb 14, 2019, 4:25:14 AM2/14/19
to Gremlin-users
Thank you,

It worked like a charm.

Some of the vertices may not have that property set1 and when i tried to  by(values("set1").fold()). it throws me error. Can you help me how to handle those kind of situations. My query should be applicable for all vertices having those property and not.

Daniel Kuppitz

unread,
Feb 14, 2019, 9:15:29 AM2/14/19
to gremli...@googlegroups.com
by(values("set1").fold()) really shouldn't throw an error if the property isn't present. A missing text property could cause some trouble, but values(prop).fold() should yield an empty list if the property doesn't exist.

What's the error message you get and what's your graph provider?

Cheers,
Daniel


Praneeth Dogiparthi

unread,
Feb 14, 2019, 10:18:01 AM2/14/19
to Gremlin-users
sorry my bad  wrong question 

here is the one . gremlin> g.V().project("text","set1","set2").
                 by("text").
                 by(values("set1").fold()).
                 by(values("set2").fold())
what if there is no property "text" and the error is no property associated with vertex bla bla. Iam using neptune as my databse. 

Thanos Giannakis

unread,
Feb 14, 2019, 11:38:15 AM2/14/19
to Gremlin-users
You could use the coalesce step.
gremlin> g.V().project("text","set1","set2").
                                by(coalesce(values('text'),constant('NULL'))).
Reply all
Reply to author
Forward
0 new messages