Adding multiple properties to a vertex using gremlin-go
Hello Community,
I have a question regarding how multiple properties can be added to a vertex using gremlin-go.
I did something like this
prop := map[string]interface{}{
"name": "Daniel",
"age": "37",
}
g.GetTraversal().Inject(prop).Unfold().As("M").AddV("Person").As("V").Select("M").Unfold().As("KV").Select("V").Property(
g.GetTraversal().V().HasLabel("Person").Select("KV").By(gremlingo.Column.Keys),
g.GetTraversal().V().HasLabel("Person").Select("KV").By(gremlingo.Column.Values),
).ToList()
This basically creates a new Person node with each property i.e. One person with name as Danier and other person with age as 37 whereas I want one person with both or more such properties to be created. Please let me know if there's any better way using gremlin-go.
I did try using gremlin and it works, so I need specific help here with Go
Solution:Jump to solution
to add all properties from map to same vertex can be used something like
`t := g.AddV("Person")
for k, v := range prop {
t = t.Property(k,v)
}...
4 Replies
Solution
to add all properties from map to same vertex can be used something like
t := g.AddV("Person")
for k, v := range prop {
t = t.Property(k,v)
}
result, err := t.Next()
Thanks,
I did this:-
g.GetTraversal().WithSideEffect("properties", prop).AddV("Person").As("Vertex").SideEffect(
g.GetTraversal().V().Select("properties").Unfold().As("KV").Select("Vertex").Property(
g.GetTraversal().V().Select("KV").By(gremlingo.Column.Keys),
g.GetTraversal().V().Select("KV").By(gremlingo.Column.Values),
),
).Next()
and it works.
Next, I am trying to add multiple vertexes with multiple propeties.
I want to add thousands of such vertexes with better performance so we can scale as well or is this overcomplicating?
Do you need the properties of all vertices to be the same or different?
Usually, when inserting, it is recommended to divide vertices into portions, the size of portion depends on the server. For example for Neptune https://docs.aws.amazon.com/neptune/latest/userguide/best-practices-gremlin-java-batch-add.html
For thousands of elements it will work, but the best solution will depend on which server is used
Bulk add vertices and edges in batches - Amazon Neptune
Every query to the Neptune DB runs in the scope of a single transaction, unless you use a session. This means that if you need to insert a lot of data using gremlin queries, batching them together in a batch size of 50-100 improves performance by reducing the number of transactions created for the load.
All of the vertices may or may not have similar properties, indeed I will need to batch them. But I was thinking of a way in which I convert the struct to a Map and pass that as an argument
The below code works for a single Map, my question is
How can we pass an array of maps/dicts having diff set of properties to be inserted to db using gremlin in go?
g.GetTraversal().WithSideEffect("properties", prop).AddV("Person").As("Vertex").SideEffect(
g.GetTraversal().V().Select("properties").Unfold().As("KV").Select("Vertex").Property(
g.GetTraversal().V().Select("KV").By(gremlingo.Column.Keys),
g.GetTraversal().V().Select("KV").By(gremlingo.Column.Values),
),
).Next()
Thanks, I got it.