Search code examples
gremlinjanusgraphgremlin-server

Add a property to an already existing vertex in java


I am trying to add a property to an already existing vertex on a remote gremlin server.

Here's how the vertex is created:

String LABEL = "label";
String NAME = "name";
String ID = "id";
String TEST = "test";

GryoMessageSerializerV3d0 serializer = new GryoMessageSerializerV3d0(GryoMapper.build().addRegistry(TinkerIoRegistryV3d0.instance()));
cluster = Cluster.build().addContactPoint("localhost").port(8182).serializer(serializer).create();
client = cluster.connect();
Graph graph = EmptyGraph.instance();
GraphTraversalSource g = JanusGraphFactory.open("/Users/user/Documents/stage/tinkerPop/Project/janusgraph-eval/janusgraph-0.3.1/conf/gremlin-server/janusgraph-cql-es-server.properties").traversal().withRemote(DriverRemoteConnection.using(cluster));
final Bindings b = Bindings.instance();
Vertex v1 = g.addV(b.of(LABEL, "transaction")).property(NAME, b.of(NAME, nameOfVertex1)).property(ID, b.of(ID, id1)).next();

Doing this:

g.V(v1).property(VertexProperty.Cardinality.single, TEST, b.of(TEST, "test")).next();

or this:

GraphTraversal v1 = g.addV(b.of(LABEL, "transaction")).property(NAME, b.of(NAME, nameOfVertex1)).property(ID, b.of(ID, id1));
v1.property(VertexProperty.Cardinality.single, TEST, b.of(TEST, "test")).next();

gives me the following error:

java.util.concurrent.CompletionException: org.apache.tinkerpop.gremlin.driver.exception.ResponseException: The type of given name is not a key: test

I looked at the tinkerpop documentation and on various tutorials/websites, but I only found a way to add properties properties (cf How to add property to vertex property in java?)

Do you know how to add a new property to an already existing vertex?


Solution

  • I found that using a GremlinPipeline solves the problem.

    GremlinPipeline pipe = new GremlinPipeline();
    pipe.start(g.V(id).property("name", "value").next());
    

    adds the property as desired