Search code examples
javagremlinvertextinkerpopamazon-neptune

Add a HashMap of Properties in Tinkerpop


I have a Map of Properties for Example:

map[0] = {
    ("name";"lux");
    ("lang";"german")
} 
map[1]= {
    ("name";"lux")
}

As you can see map[1] has no property "lang". Now I want to add a Vertex without hard Coding the keys.

Is there a way how I can do this in one Statement without looping the Map and add each property one by one.

Something like:

Graph.addV("label").property(T.id, getID()).property(map,key,value);


Solution

  • I used the basic Gremlin Console and TinkerGraph to produce this example but it should be no problem to express this in Gremlin Java.

    nodes = [
        ["name": "Kim",    "breed": "Beagle"],
        ["name": "Max",    "breed": "Mixed"],
        ["name": "Toby",   "breed": "Golden Retriever"]]    
    
    gremlin> g.inject(nodes).unfold().as("nodes").
    ......1>   addV("test").as("new_node").
    ......2>   sideEffect(select('nodes').unfold().as('kv').
    ......3>              select('new_node').
    ......4>              property( select('kv').by(Column.keys),
    ......5>                        select('kv').by(Column.values))).
    ......6>          id().toList()    
    ==>9
    ==>12
    ==>15               
    
    gremlin> g.V().valueMap().with(WithOptions.tokens)
    ==>[id:9,label:test,name:[Kim],breed:[Beagle]]
    ==>[id:12,label:test,name:[Max],breed:[Mixed]]
    ==>[id:15,label:test,name:[Toby],breed:[Golden Retriever]]