I want to add/update vertex properties in through the following function to janusgraph with Gremlin.Net version=3.4.6; JanusGraph.Net version=0.2.2
public class DataManager
{
private readonly IGremlinClient client;
private readonly GraphTraversalSource g;
public DataManager()
{
this.client = JanusGraphClientBuilder
.BuildClientForServer(new GremlinServer("localhost", 8182))
.Create();
this.g = AnonymousTraversalSource.Traversal().WithRemote(
new DriverRemoteConnection(this.client));
}
public async Task EditVertexProperty(VertexDto vertexDto)
{
var traversal = this.g.V(vertexDto.Id);
if (!string.IsNullOrWhiteSpace(vertexDto.Label))
{
traversal = traversal.HasLabel(vertexDto.Label);
}
if (!traversal.HasNext())
{
throw new Exception("xxxxxxx");
}
foreach (var property in vertexDto.Properties)
{
if (property.IsList)
{
traversal = traversal.Property(Cardinality.List, property.PropertyKey, property.PropertyValue);
}
else
{
traversal = traversal.Property(Cardinality.Single, property.PropertyKey, property.PropertyValue);
}
}
await traversal.Promise(v => v.Iterate()).ConfigureAwait(false);
}
}
public class VertexDto
{
public string Id { get; set; }
public string Label { get; set; }
public List<Property> Properties { get; set; }
}
public class Property
{
public string PropertyKey { get; set; }
public string PropertyValue { get; set; }
public bool IsList { get; set; }
}
when i try to add or update a vertex property such as,
{
"id": 1234,
"properties":[
{
"propertyKey": "name",
"propertyValue": "sb"
}
]
}
but nothing has changed and it not throw exception. And i try in gremlin-server with g.V(1234).property("name", "sb").iterate() it worked. first i think traversal is stop when called HasNext(), but this doesn't seem to be the case.
What should i do. Thank for your help.
The way to work with a traversal is by building it up iteratively first by concatenating the steps that you want to execute (like V()
, has()
and so on) and then to terminate the traversal with a terminal step like iterate()
which will execute the traversal.
You however use two terminal steps in your example which doesn't work. First you execute HasNext()
to verify that the vertex exists and then you try to modify its properties which you then want to execute via Iterate()
.
The traversal was however already evaluated and its Bytecode was sent to the server when you executed HasNext()
. It is afterwards not possible any more to modulate the traversal object.
This becomes more clear when you try to do the same in the Gremlin Console:
gremlin> t = g.V().has('name','peter'); []
gremlin> t.hasNext()
==>true
gremlin> t.property('test','test').iterate()
The traversal strategies are complete and the traversal can no longer be modulated
So, Gremlin-Java throws an exception to make it clear that this is not possible. Gremlin.NET unfortunately does not throw an exception and just ignores any added steps to the traversal after it has been executed. It would of course be better if Gremlin.NET would also throw an exception instead to make this more clear. I created a ticket for this with the TinkerPop project: TINKERPOP-2614.
So, if you want to check first whether the vertex exists before you modify its properties, then you have to create two different traversals.