I want to set the property for vertex and edge as a list value. I'm using janusgraph. Gremlin-python
-
1Have you looked at List and Set cardinality? Also you can only specify "multi properties" on Vertices and not on Edges. https://tinkerpop.apache.org/docs/current/reference/#vertex-properties – rod-emerson Apr 27 '20 at 12:52
-
Just worked with gremlin on aws neptune and `list` properties appear to be default behavior when adding multiple properties, and `single` is used to avoid `list` behavior when updating an existing property – Timothy L.J. Stewart Sep 20 '22 at 06:56
3 Answers
Here is an example that uses gremlin-python to Create a vertex with 3 values stored as a Set using the property key mySet
. Note that when using gremlin-python, some Gremlin steps have an underscore after their name to avoid collisions with Python steps of the same name.
result = (
g.addV('test').
property(Cardinality.set_,"mySet",1).
property(Cardinality.set_,"mySet",2).
property(Cardinality.set_,"mySet",3).
valueMap().
next()
)
print(result)
{'mySet': [1, 2, 3]}

- 14,674
- 2
- 16
- 38
-
-
Are you asking about the Python console or the Gremlin console? – Kelvin Lawrence Apr 30 '20 at 03:16
In JanusGraph, it's recommended to create schema upfront. The LIST cardinality means you want an array valued key.
In the gremlin console you can do:
mgmt = graph.openManagement(); mgmt.makePropertyKey('myProperty').dataType(String).cardinality(LIST).make(); mgmt.commit();
Then you chain property functions in gremlin:
g.addV().property('myProperty', 'val1).property('myProperty','val2')
If you want to create schema dynamically, you can do this in one gremlin call. You provide a new property key that has never been used before (existing keys will have cardinality SINGLE by default and won't accept multiple values). You must instruct gremlin about the cardinality of all your newly inserted values, like this:
g.addV().property(list,'myNewProperty', 'val1').property(list,'myNewProperty','val2')
Note how I had to add the list keyword each time.
If you are doing this via gremlin-python, the second option should work directly as if native code. The first option requires you to send the schema commands via a client connection:
from gremlin_python.driver.client import Client
client = Client('ws://urlToServer:8182/gremlin','g')
console_command_string = """mgmt = graph.openManagement(); mgmt.makePropertyKey('myProperty').dataType(String).cardinality(LIST).make(); mgmt.commit();"""
client.submit(console_command_string)
client.close()

- 1,074
- 1
- 8
- 17
-
Just worked with gremlin on aws neptune and `list` properties appear to be default behavior when adding multiple properties, and `single` is used to avoid `list` behavior when updating an existing property – Timothy L.J. Stewart Sep 20 '22 at 06:53
By setting the property cardinality to set or list, you can attach multiple values to a property key.
Find the below java snippet.
janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "cricket");
janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "football");
janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "tennis");
Reference link.

- 3,658
- 1
- 36
- 57