Insert or update vertex using Map

506 views
Skip to first unread message

Amiya

unread,
May 4, 2020, 4:21:30 PM5/4/20
to Gremlin-users
Hi Stephen,

Following http://stephen.genoprime.com/snippet/2020/03/28/snippet-7.html, insert of single vertex and batch of vertex works.
I am able to write edge creation between dynamic vertex as well. Below query works well.

l = [
[from: 'marko', to: 'josh', properties: [ weight: 9]]
];

g.inject(l).
unfold().as('edge').
addE('knows').
from(V().has('name', select('edge').select('from'))).
to(V().has('name', select('edge').select('to'))).
as('e').
sideEffect(select('edge').select('properties').
unfold().as('kv').
select('e').
property(select('kv').by(keys), select('kv').by(values)))

Then I tried to extend the query for insert/update vertex using coalesce, but the query did not work as expected.
When I try to debug I do not understand why filtering does not happen.

gremlin> l = [ 'josh']
gremlin> g.inject(l).unfold().as('n').V().has('name', select('n')).valueMap()
==>{name=[josh], age=[32], country=[usa]}
==>{name=[vadas], age=[27], country=[usa]}
==>{name=[marko], age=[29], country=[usa]}  // why all 3 vertices in result

gremlin> l = ['josh', 'marko']
==>josh
==>marko

gremlin> g.inject(l).unfold().as('n').V().has('name', select('n')).valueMap()
==>{name=[josh], age=[32], country=[usa]}
==>{name=[vadas], age=[27], country=[usa]}
==>{name=[marko], age=[29], country=[usa]}
==>{name=[josh], age=[32], country=[usa]}
==>{name=[vadas], age=[27], country=[usa]}
==>{name=[marko], age=[29], country=[usa]} // now all vertex repeat for each value in list ???


gremlin> g.inject(l).unfold().map{V().has('name', it.get()).next()}.valueMap()
gremlin> //no result


//I can get result if I start traversal from anonymous traversal and then use g.V() inside map() 
gremlin> inject(l).unfold().map{g.V().has('name', it.get()).next()}.valueMap()
==>{name=[josh], age=[32], country=[usa]}
==>{name=[marko], age=[29], country=[usa]}

gremlin> l = ['josh', 'amiya']

gremlin> inject(l).unfold().as('n').map{g.V().has('name', it.get()).fold().
......1>   coalesce(unfold(), addV('person').property('name', it.get())).next()}.valueMap()
==>{name=[josh], age=[32], country=[usa]}
==>{name=[amiya]}

gremlin> l = [[name:'josh',age:29,country:'usa'], [name:'foo',age:24,country:'usa']];
==>{name=josh, age=29, country=usa}
==>{name=foo, age=24, country=usa}


gremlin> inject(l).unfold().as('properties').map{g.V().has('name', it.get().get('name')).fold().
......1>   coalesce(unfold(), addV('person')).next()}.as('vertex').
......2> sideEffect(select('properties').
......3>               unfold().as('kv').
......4>               select('vertex').
......5>               property(select('kv').by(Column.keys), select('kv').by(Column.values)))
==>v[8192]
==>v[16384]


gremlin> g.V().valueMap(true)
==>{id=8192, label=person, name=[josh], age=[29], country=[usa]}
==>{id=12288, label=person, name=[vadas], age=[27], country=[usa]}
==>{id=16384, label=person, name=[foo], age=[24], country=[usa]}
==>{id=4312, label=person, name=[marko], age=[29], country=[usa]}
==>{id=20632, label=person, name=[amiya]}

 Though at last I get the the result, I have 2 concern
  • it involves lambda -- not sugestible
  • it involves g.V() in middle of step traversal -- so it would be treated as a single traversal or it is similar to the brute force way (vertices.each { v -> ....addV();}) 

Any better way to achieve this?

TIA,
Amiya

Stephen Mallette

unread,
May 6, 2020, 6:57:16 AM5/6/20
to gremli...@googlegroups.com
The has(String,Traversal) overload doesn't do what you think it does.


gremlin> g.inject(['josh']).unfold().as('n').V().has('name', where(eq('n')))
==>v[4]



--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/b257d118-4e27-40ef-921f-87308b54da70%40googlegroups.com.

Amiya

unread,
May 6, 2020, 12:42:40 PM5/6/20
to Gremlin-users
Ahh, I see.

Robert already mentioned query for this use case itself.

Combining both version of query.
l = [
  [name:'josh',age:29,country:'usa'],
  [name:'bar',age:24,country:'usa']];
 
g.inject(l).
 unfold().as('properties').
 select('name').as('pName').
 coalesce(V().has('name', where(eq('pName'))),
     addV('person')).as('vertex').
 sideEffect(select('properties').
              unfold().as('kv').
              select('vertex').
              property(select('kv').by(Column.keys), select('kv').by(Column.values)))

It works perfectly.

Thanks,
Amiya 
To unsubscribe from this group and stop receiving emails from it, send an email to gremli...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages