gremlin python - add multiple but an unknown number of properties to a vertex

后端 未结 1 1806
攒了一身酷
攒了一身酷 2021-01-13 15:21

I want to add more than one property to a vertex, but from the outset do not explicitly know what those properties might be. For example, say for one person to be added as v

相关标签:
1条回答
  • 2021-01-13 15:56

    You can inject your maps/dictionaries into the traversal, create a vertex for each dictionary and then go through all dictionary/map-entries and set them as properties. In Gremlin that looks like this:

    g.inject(persons).unfold().as('person').
      addV('person').as('v').
      sideEffect(select('person').unfold().as('kv').
                 select('v').
                   property(select('kv').by(keys), select('kv').by(values))).
      iterate()
    

    Example:

    gremlin> persons = [["id":1,"first_name":"bob","age":25,"height": 177]
    ......1>           ,["id":2,"first_name":"joe","surname":"bloggs",
                           "occupation":"lawyer","birthday":"12 September"]]
    ==>[id:1,first_name:bob,age:25,height:177]
    ==>[id:2,first_name:joe,surname:bloggs,occupation:lawyer,birthday:12 September]
    
    gremlin> g = TinkerGraph.open().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> g.inject(persons).unfold().as('person').
    ......1>   addV('person').as('v').
    ......2>   sideEffect(select('person').unfold().as('kv').
    ......3>              select('v').
    ......4>                property(select('kv').by(keys), select('kv').by(values))).
    ......5>    valueMap()
    ==>[id:[1],first_name:[bob],age:[25],height:[177]]
    ==>[birthday:[12 September],occupation:[lawyer],surname:[bloggs],id:[2],first_name:[joe]]
    
    0 讨论(0)
提交回复
热议问题